Kamis, 22 Juli 2010

Program Assembly

;Program Pertama Aku
;Mahantan Sinaga
IDEAL
P386
JUMPS

SEGMENT CODE
ASSUME cs:code,ds:code

ORG 100h

START:

mov ax,0013h
int 10h

mov ax,cs
mov ds,ax
mov ax,0a000h
mov es,ax

lea si,[Palette]
mov dx,3c8h
xor al,al
out dx,al
mov dx,3c9h
mov cx,189*3
repz outsb

; === Set some variables ===
mov [DeltaX],1
mov [DeltaY],1
mov [DeltaZ],1

mov [Xoff],256
mov [Yoff],256
mov [Zoff],300

MainLoop:
call MainProgram

in al,60h
cmp al,1
jne MainLoop

; === Quit to DOS ===
mov ax,0003h
int 10h
lea dx,[Credits]
mov ah,9
int 21h
mov ax,4c00h
int 21h

; === Sub-routines ===

PROC WaitVrt
mov dx,3dah
Vrt:
in al,dx
test al,8
jnz Vrt
NoVrt:
in al,dx
test al,8
jz NoVrt
ret
ENDP WaitVrt

PROC UpdateAngles
; Calculates new x,y,z angles
; to rotate around
mov ax,[XAngle]
mov bx,[YAngle]
mov cx,[ZAngle]

add ax,[DeltaX]
and ax,11111111b
mov [XAngle],ax
add bx,[DeltaY]
and bx,11111111b
mov [YAngle],bx
add cx,[DeltaZ]
and cx,11111111b
mov [ZAngle],cx
ret
ENDP UpdateAngles

PROC GetSinCos
; Needed : bx=angle (0..255)
; Returns: ax=Sin bx=Cos
push bx
shl bx,1
mov ax,[SinCos + bx]
pop bx
push ax
add bx,64
and bx,11111111b
shl bx,1
mov ax,[SinCos + bx]
mov bx,ax
pop ax
ret
ENDP GetSinCos

PROC SetRotation
; Set sine & cosine of x,y,z
mov bx,[XAngle]
call GetSinCos
mov [Xsin],ax
mov [Xcos],bx

mov bx,[Yangle]
call GetSinCos
mov [Ysin],ax
mov [Ycos],bx

mov bx,[Zangle]
call GetSinCos
mov [Zsin],ax
mov [Zcos],bx
ret
ENDP SetRotation

PROC RotatePoint
; Gets original x,y,z values
; This can be done elsewhere
movsx ax,[Cube+si]
mov [X],ax
movsx ax,[Cube+si+1]
mov [Y],ax
movsx ax,[Cube+si+2]
mov [Z],ax

; Rotate around x-axis
; YT = Y * COS(xang) - Z * SIN(xang) / 256
; ZT = Y * SIN(xang) + Z * COS(xang) / 256
; Y = YT
; Z = ZT

mov ax,[Y]
mov bx,[XCos]
imul bx
mov bp,ax
mov ax,[Z]
mov bx,[XSin]
imul bx
sub bp,ax
sar bp,8
mov [Yt],bp

mov ax,[Y]
mov bx,[XSin]
imul bx
mov bp,ax
mov ax,[Z]
mov bx,[XCos]
imul bx
add bp,ax
sar bp,8
mov [Zt],bp

mov ax,[Yt]
mov [Y],ax
mov ax,[Zt]
mov [Z],ax

; Rotate around y-axis
; XT = X * COS(yang) - Z * SIN(yang) / 256
; ZT = X * SIN(yang) + Z * COS(yang) / 256
; X = XT
; Z = ZT

mov ax,[X]
mov bx,[YCos]
imul bx
mov bp,ax
mov ax,[Z]
mov bx,[YSin]
imul bx
sub bp,ax
sar bp,8
mov [Xt],bp

mov ax,[X]
mov bx,[YSin]
imul bx
mov bp,ax
mov ax,[Z]
mov bx,[YCos]
imul bx
add bp,ax
sar bp,8
mov [Zt],bp

mov ax,[Xt]
mov [X],ax
mov ax,[Zt]
mov [Z],ax

; Rotate around z-axis
; XT = X * COS(zang) - Y * SIN(zang) / 256
; YT = X * SIN(zang) + Y * COS(zang) / 256
; X = XT
; Y = YT

mov ax,[X]
mov bx,[ZCos]
imul bx
mov bp,ax
mov ax,[Y]
mov bx,[ZSin]
imul bx
sub bp,ax
sar bp,8
mov [Xt],bp

mov ax,[X]
mov bx,[ZSin]
imul bx
mov bp,ax
mov ax,[Y]
mov bx,[ZCos]
imul bx
add bp,ax
sar bp,8
mov [Yt],bp

mov ax,[Xt]
mov [X],ax
mov ax,[Yt]
mov [Y],ax

ret
ENDP RotatePoint

PROC ShowPoint
; Calculates screenposition and
; plots the point on the screen
mov ax,[Xoff]
mov bx,[X]
imul bx
mov bx,[Z]
add bx,[Zoff]
idiv bx
add ax,[Mx]
mov bp,ax

mov ax,[Yoff]
mov bx,[Y]
imul bx
mov bx,[Z]
add bx,[Zoff]
idiv bx
add ax,[My]

mov bx,320
imul bx
add ax,bp
mov di,ax

mov ax,[Z]
add ax,100d

mov [byte ptr es:di],al
mov [Erase+si],di
ret
ENDP ShowPoint

PROC MainProgram
call UpdateAngles
call SetRotation

xor si,si
mov cx,MaxPoints
ShowLoop:
call RotatePoint
call ShowPoint
add si,3
loop ShowLoop

call WaitVrt

xor si,si
xor al,al
mov cx,MaxPoints
Deletion:
mov di,[Erase+si]
mov [byte ptr es:di],al
add si,3
loop Deletion
ret
ENDP MainProgram

; === DATA ===

Credits DB 13,10,"Code by Vulture / Outlaw Triad",13,10,"$"

Label SinCos Word ; 256 values
dw 0,6,13,19,25,31,38,44,50,56
dw 62,68,74,80,86,92,98,104,109,115
dw 121,126,132,137,142,147,152,157,162,167
dw 172,177,181,185,190,194,198,202,206,209
dw 213,216,220,223,226,229,231,234,237,239
dw 241,243,245,247,248,250,251,252,253,254
dw 255,255,256,256,256,256,256,255,255,254
dw 253,252,251,250,248,247,245,243,241,239
dw 237,234,231,229,226,223,220,216,213,209
dw 206,202,198,194,190,185,181,177,172,167
dw 162,157,152,147,142,137,132,126,121,115
dw 109,104,98,92,86,80,74,68,62,56
dw 50,44,38,31,25,19,13,6,0,-6
dw -13,-19,-25,-31,-38,-44,-50,-56,-62,-68
dw -74,-80,-86,-92,-98,-104,-109,-115,-121,-126
dw -132,-137,-142,-147,-152,-157,-162,-167,-172,-177
dw -181,-185,-190,-194,-198,-202,-206,-209,-213,-216
dw -220,-223,-226,-229,-231,-234,-237,-239,-241,-243
dw -245,-247,-248,-250,-251,-252,-253,-254,-255,-255
dw -256,-256,-256,-256,-256,-255,-255,-254,-253,-252
dw -251,-250,-248,-247,-245,-243,-241,-239,-237,-234
dw -231,-229,-226,-223,-220,-216,-213,-209,-206,-202
dw -198,-194,-190,-185,-181,-177,-172,-167,-162,-157
dw -152,-147,-142,-137,-132,-126,-121,-115,-109,-104
dw -98,-92,-86,-80,-74,-68,-62,-56,-50,-44
dw -38,-31,-25,-19,-13,-6

Label Cube Byte
c = -35
rept 5
b = -35
rept 5
a = -35
rept 5
db a,b,c
a = a + 20
endm
b = b + 20
endm
c = c + 20
endm

Label Palette Byte
db 0,0,0
d = 63
rept 63
db d,d,d
db d,d,d
db d,d,d
d = d - 1
endm

X DW ?
Y DW ?
Z DW ?

Xt DW ?
Yt DW ?
Zt DW ?

XAngle DW 0
YAngle DW 0
ZAngle DW 0

DeltaX DW ?
DeltaY DW ?
DeltaZ DW ?

Xoff DW ?
Yoff DW ?
Zoff DW ?

XSin DW ?
XCos DW ?
YSin DW ?
YCos DW ?
ZSin DW ?
ZCos DW ?

Mx DW 160
My DW 100

MaxPoints EQU 125

Erase DW MaxPoints DUP (?)

ENDS CODE
END START

Program Assembly

;Program Pertama Aku
;Mahantan Sinaga
IDEAL
P386
JUMPS

SEGMENT CODE
ASSUME cs:code,ds:code

ORG 100h

START:

mov ax,0013h
int 10h

mov ax,cs
mov ds,ax
mov ax,0a000h
mov es,ax

lea si,[Palette]
mov dx,3c8h
xor al,al
out dx,al
mov dx,3c9h
mov cx,189*3
repz outsb

; === Set some variables ===
mov [DeltaX],1
mov [DeltaY],1
mov [DeltaZ],1

mov [Xoff],256
mov [Yoff],256
mov [Zoff],300

MainLoop:
call MainProgram

in al,60h
cmp al,1
jne MainLoop

; === Quit to DOS ===
mov ax,0003h
int 10h
lea dx,[Credits]
mov ah,9
int 21h
mov ax,4c00h
int 21h

; === Sub-routines ===

PROC WaitVrt
mov dx,3dah
Vrt:
in al,dx
test al,8
jnz Vrt
NoVrt:
in al,dx
test al,8
jz NoVrt
ret
ENDP WaitVrt

PROC UpdateAngles
; Calculates new x,y,z angles
; to rotate around
mov ax,[XAngle]
mov bx,[YAngle]
mov cx,[ZAngle]

add ax,[DeltaX]
and ax,11111111b
mov [XAngle],ax
add bx,[DeltaY]
and bx,11111111b
mov [YAngle],bx
add cx,[DeltaZ]
and cx,11111111b
mov [ZAngle],cx
ret
ENDP UpdateAngles

PROC GetSinCos
; Needed : bx=angle (0..255)
; Returns: ax=Sin bx=Cos
push bx
shl bx,1
mov ax,[SinCos + bx]
pop bx
push ax
add bx,64
and bx,11111111b
shl bx,1
mov ax,[SinCos + bx]
mov bx,ax
pop ax
ret
ENDP GetSinCos

PROC SetRotation
; Set sine & cosine of x,y,z
mov bx,[XAngle]
call GetSinCos
mov [Xsin],ax
mov [Xcos],bx

mov bx,[Yangle]
call GetSinCos
mov [Ysin],ax
mov [Ycos],bx

mov bx,[Zangle]
call GetSinCos
mov [Zsin],ax
mov [Zcos],bx
ret
ENDP SetRotation

PROC RotatePoint
; Gets original x,y,z values
; This can be done elsewhere
movsx ax,[Cube+si]
mov [X],ax
movsx ax,[Cube+si+1]
mov [Y],ax
movsx ax,[Cube+si+2]
mov [Z],ax

; Rotate around x-axis
; YT = Y * COS(xang) - Z * SIN(xang) / 256
; ZT = Y * SIN(xang) + Z * COS(xang) / 256
; Y = YT
; Z = ZT

mov ax,[Y]
mov bx,[XCos]
imul bx
mov bp,ax
mov ax,[Z]
mov bx,[XSin]
imul bx
sub bp,ax
sar bp,8
mov [Yt],bp

mov ax,[Y]
mov bx,[XSin]
imul bx
mov bp,ax
mov ax,[Z]
mov bx,[XCos]
imul bx
add bp,ax
sar bp,8
mov [Zt],bp

mov ax,[Yt]
mov [Y],ax
mov ax,[Zt]
mov [Z],ax

; Rotate around y-axis
; XT = X * COS(yang) - Z * SIN(yang) / 256
; ZT = X * SIN(yang) + Z * COS(yang) / 256
; X = XT
; Z = ZT

mov ax,[X]
mov bx,[YCos]
imul bx
mov bp,ax
mov ax,[Z]
mov bx,[YSin]
imul bx
sub bp,ax
sar bp,8
mov [Xt],bp

mov ax,[X]
mov bx,[YSin]
imul bx
mov bp,ax
mov ax,[Z]
mov bx,[YCos]
imul bx
add bp,ax
sar bp,8
mov [Zt],bp

mov ax,[Xt]
mov [X],ax
mov ax,[Zt]
mov [Z],ax

; Rotate around z-axis
; XT = X * COS(zang) - Y * SIN(zang) / 256
; YT = X * SIN(zang) + Y * COS(zang) / 256
; X = XT
; Y = YT

mov ax,[X]
mov bx,[ZCos]
imul bx
mov bp,ax
mov ax,[Y]
mov bx,[ZSin]
imul bx
sub bp,ax
sar bp,8
mov [Xt],bp

mov ax,[X]
mov bx,[ZSin]
imul bx
mov bp,ax
mov ax,[Y]
mov bx,[ZCos]
imul bx
add bp,ax
sar bp,8
mov [Yt],bp

mov ax,[Xt]
mov [X],ax
mov ax,[Yt]
mov [Y],ax

ret
ENDP RotatePoint

PROC ShowPoint
; Calculates screenposition and
; plots the point on the screen
mov ax,[Xoff]
mov bx,[X]
imul bx
mov bx,[Z]
add bx,[Zoff]
idiv bx
add ax,[Mx]
mov bp,ax

mov ax,[Yoff]
mov bx,[Y]
imul bx
mov bx,[Z]
add bx,[Zoff]
idiv bx
add ax,[My]

mov bx,320
imul bx
add ax,bp
mov di,ax

mov ax,[Z]
add ax,100d

mov [byte ptr es:di],al
mov [Erase+si],di
ret
ENDP ShowPoint

PROC MainProgram
call UpdateAngles
call SetRotation

xor si,si
mov cx,MaxPoints
ShowLoop:
call RotatePoint
call ShowPoint
add si,3
loop ShowLoop

call WaitVrt

xor si,si
xor al,al
mov cx,MaxPoints
Deletion:
mov di,[Erase+si]
mov [byte ptr es:di],al
add si,3
loop Deletion
ret
ENDP MainProgram

; === DATA ===

Credits DB 13,10,"Code by Vulture / Outlaw Triad",13,10,"$"

Label SinCos Word ; 256 values
dw 0,6,13,19,25,31,38,44,50,56
dw 62,68,74,80,86,92,98,104,109,115
dw 121,126,132,137,142,147,152,157,162,167
dw 172,177,181,185,190,194,198,202,206,209
dw 213,216,220,223,226,229,231,234,237,239
dw 241,243,245,247,248,250,251,252,253,254
dw 255,255,256,256,256,256,256,255,255,254
dw 253,252,251,250,248,247,245,243,241,239
dw 237,234,231,229,226,223,220,216,213,209
dw 206,202,198,194,190,185,181,177,172,167
dw 162,157,152,147,142,137,132,126,121,115
dw 109,104,98,92,86,80,74,68,62,56
dw 50,44,38,31,25,19,13,6,0,-6
dw -13,-19,-25,-31,-38,-44,-50,-56,-62,-68
dw -74,-80,-86,-92,-98,-104,-109,-115,-121,-126
dw -132,-137,-142,-147,-152,-157,-162,-167,-172,-177
dw -181,-185,-190,-194,-198,-202,-206,-209,-213,-216
dw -220,-223,-226,-229,-231,-234,-237,-239,-241,-243
dw -245,-247,-248,-250,-251,-252,-253,-254,-255,-255
dw -256,-256,-256,-256,-256,-255,-255,-254,-253,-252
dw -251,-250,-248,-247,-245,-243,-241,-239,-237,-234
dw -231,-229,-226,-223,-220,-216,-213,-209,-206,-202
dw -198,-194,-190,-185,-181,-177,-172,-167,-162,-157
dw -152,-147,-142,-137,-132,-126,-121,-115,-109,-104
dw -98,-92,-86,-80,-74,-68,-62,-56,-50,-44
dw -38,-31,-25,-19,-13,-6

Label Cube Byte
c = -35
rept 5
b = -35
rept 5
a = -35
rept 5
db a,b,c
a = a + 20
endm
b = b + 20
endm
c = c + 20
endm

Label Palette Byte
db 0,0,0
d = 63
rept 63
db d,d,d
db d,d,d
db d,d,d
d = d - 1
endm

X DW ?
Y DW ?
Z DW ?

Xt DW ?
Yt DW ?
Zt DW ?

XAngle DW 0
YAngle DW 0
ZAngle DW 0

DeltaX DW ?
DeltaY DW ?
DeltaZ DW ?

Xoff DW ?
Yoff DW ?
Zoff DW ?

XSin DW ?
XCos DW ?
YSin DW ?
YCos DW ?
ZSin DW ?
ZCos DW ?

Mx DW 160
My DW 100

MaxPoints EQU 125

Erase DW MaxPoints DUP (?)

ENDS CODE
END START