pong in assembly
Posted: Wed Nov 05, 2008 8:25 pm
Code: Select all
;------------ INITIALIZATION -----------;
VSeg EQU 0A000h
JMP start
OldVidMode DB ?
PaddleX DW 160
BallX DW 160
BallY DW 100
BallDX DW 1
BallDY DW -1
BallCounter DB 3
start:
; Store old video mode
MOV AX, 0F00h
INT 10h
MOV OldVidMode, AL
; Set new video mode
MOV AX, 0013h
INT 10h
CALL Main
;CALL WaitKey
; Restore old video mode
MOV AH, 00h
MOV AL, OldVidMode
INT 10h
; Terminate program
MOV AX, 4C00h
INT 21h
;----------- SUBROUTINES ----------;
WaitKey:
MOV AH, 06h
MOV DL, 0FFh
WK_loop:
INT 21h
JZ WK_loop
RET
Main:
;MOV AX, 0001h
;INT 33h
MOV AX, 000Bh
INT 33h
MainLoop:
CALL Update
CALL ClearBuf
CALL Draw
CALL Blit
; Check for a keystroke
MOV AH, 06h
MOV DL, 0FFh
INT 21h
JZ MainLoop ; No Keystroke? Jump to start
RET
ClearBuf:
MOV CX, 320 * 200
MOV ES, CS
MOV DI, OFFSET VidBuf
CLD
MOV AL, 0
REP STOSB
RET
Blit:
MOV CX, 320 * 200
MOV ES, VSeg
MOV DI, 0
MOV DS, CS
MOV SI, OFFSET VidBuf
CLD
REP MOVSB
RET
Draw:
; Draw player paddle
MOV AX, PaddleX
MOV BX, 64000 - 960
CALL DrawPaddle
; Draw Computer paddle
MOV AX, BallX
CMP AX, 16
JGE PaddleLeftGood
MOV AX, 16
PaddleLeftGood:
CMP AX, 320-16
JLE PaddleRightGood
MOV AX, 320-16
PaddleRightGood:
MOV BX, 0
CALL DrawPaddle
; Draw Ball
CALL DrawBall
RET
DrawPaddle:
MOV ES, CS
MOV DI, OFFSET VidBuf
ADD DI, AX
SUB DI, 16
ADD DI, BX
MOV CX, 32
MOV AL, 2
REP STOSB
MOV CX, 32
ADD DI, 320 - 32
REP STOSB
MOV CX, 32
ADD DI, 320 - 32
REP STOSB
RET
DrawBall:
MOV ES, CS
MOV DI, OFFSET VidBuf
MOV AX, BallY
IMUL AX, 320
ADD AX, BallX
ADD DI, AX
SUB DI, 320 + 1
MOV CX, 3
MOV AL, 3
REP STOSB
MOV CX, 3
ADD DI, 320 - 3
REP STOSB
MOV CX, 3
ADD DI, 320 - 3
REP STOSB
RET
Update:
; ------ Upate Player Paddle ------ ;
; Get Relative Mouse movement
MOV AX, 000Bh
INT 33h
ADD PaddleX, CX
CMP PaddleX, 16
JGE NoFixRight
MOV PaddleX, 16
NoFixRight:
CMP PaddleX, 320 - 16
JLE NoFixLeft
MOV PaddleX, 320 - 16
NoFixLeft:
DEC BallCounter
CMP BallCounter, 0
JZ BallNeedUpdate
RET
BallNeedUpdate:
MOV BallCounter, 3
; ------- Update Ball --------------- ;
MOV AX, BallX
ADD AX, BallDX
MOV BallX, AX
MOV AX, BallY
ADD AX, BallDY
MOV BallY, AX
CMP BallX, 1
JGE BallLeftGood
MOV BallX, 1
NEG BallDX
BallLeftGood:
CMP BallX, 320-1
JLE BallRightGood
MOV BallX, 320-1
NEG BallDX
BallRightGood:
CMP BallY, 4
JGE BallTopGood
MOV BallY, 4
NEG BallDY
BallTopGood:
CMP BallY, 200-4
JLE BallBotGood
MOV AX, BallX
SUB AX, PaddleX
CMP AX, 18
JLE NotMissedRight
CALL ResetBall
JMP BallBotGood
NotMissedRight:
CMP AX, -18
JGE DoneBall
CALL ResetBall
JMP BallBotGood
DoneBall:
MOV BallY, 200-4
NEG BallDY
BallBotGood:
RET
ResetBall:
MOV BallX, 160
MOV BallY, 100
MOV BallDX, 1
MOV BallDY, -1
RET
VidBuf DB