Reputation: 97
I'm working on an assembly language Pong game where the left player's paddle continues to move even after the key is released. The right player's paddle works fine, stopping when the key is not pressed. Here's the relevant portion of my code:
; Left Player's Paddle Movement
proc keys_p2
pusha
mov [x_surface_p2],40
mov ah,1h
int 16h
cmp al,27 ; ESC
je endGame
cmp al,77h; Up
jne skip3_p2
sub [y0_p2],2
call the_cleaner_buffer
jmp skip4_p2
skip3_p2:
cmp al,73h ; Down
jne skip4_p2
add [y0_p2],2
call the_cleaner_buffer
jmp skip4_p2
skip4_p2:
;call the_cleaner_buffer
push [y0_p2]
pop [y_surface_p2]
CALL printPakman_p2
popa
ret
endp keys_p2
I expected the paddle to stop moving when no keys are pressed. However, it continues moving indefinitely. I tried debugging the key handling logic, but I haven't found the cause of the issue.
Upvotes: 1
Views: 43
Reputation: 39516
The KeyboardBIOS function 01h returns the status of the BIOS keyboard buffer. The status is returned through the Zero Flag. If ZF is clear then a key is available and you additionally receive a preview of this key's ASCII code in AL and scancode in AH. But if ZF is set then no key is available and you should not be interpreting the AL register like you're doing.
mov ah, 01h ; BIOS.TestKeystroke
int 16h ; -> AX ZF
jz noKey ; (*)
mov al, 00h ; BIOS.GetKeystroke
int 16h ; -> AX
(*) In case there is no key available then you're supposed to maintain the movement as it was before:
mov ah, 01h ; BIOS.TestKeystroke
int 16h ; -> AX ZF
mov al, [currentDirection_p2] ; {73h,77h}
jz noKey ; (*)
mov al, 00h ; BIOS.GetKeystroke
int 16h ; -> AX
cmp al, 27 ; ESC
je endGame
mov [currentDirection_p2], al ; {73h,77h}
noKey:
cmp al, 77h ; Up
jne skip3_p2
...
The right player's paddle works fine, stopping when the key is not pressed.
It would have been most interesting to see how the working paddle functions.
Is it that different from the other paddle?
Upvotes: 1