0

:D I know I already posted another question earlier today about Tasm as well, but this one is about a different topic so I wanted to post a new question :D.

Basically, it seems like my function of checking if a key got pressed and then seeing if it is a WASD key just isnt working.

Code:

'''proc KeyPress ;function which gets key press from WASD and will change var 'Direction' according to it.
    push ax
    cmp [KeyPressed], 0
    jne EndKeyPress
startofCheck:
    mov ah, 0bh
    int 21h;returns AL=0 - NO KEY PRESSED, AL!=0 - KEY PRESSED.
    cmp al, 0
    je  startofCheck
;PROCESS KEY.        
    mov ah, 0
    int 16h      ;get the key.

Wait_for_Data:
    cmp al, 87;'W'
    je MovingUp
    cmp al, 65;'A'
    je MovingLeft
    cmp al, 83;'S'
    je MovingDown
    cmp al, 68;'D'
    je MovingRight
    jmp startofCheck
MovingUp:
    mov [Direction], 2d
    jmp EndKeyPress
MovingLeft:
    mov [Direction], 3d
    jmp EndKeyPress
MovingDown:
    mov [Direction], 4d
    jmp EndKeyPress
MovingRight:
    mov [Direction], 1d
    jmp EndKeyPress
EndKeyPress:
    inc [KeyPressed]
    pop ax
    ret
endp KeyPress ''' 

What i want to do is basically check if key has been pressed, and if so check if it was a WASD. depending on the key pressed, i will change the direction of the snake.

Any help or advice will be appreciated =D

  • What is not working? The code check for the presence of a char in a loop, blocking the execution. You don't need it in front of a `ax=0000h/INT 16h`, which already blocks. If you don't want to block, you need to exit from the proc if no key is pressed. – Margaret Bloom Apr 17 '21 at 14:23

1 Answers1

1

Do not mix DOS and Bios keyboard service if you need unblocked key check.
It is better to read key-scan code instead of ASCII character, so you won't have to bother with character case (if CapsLock is accidentaly ON/OFF) or with national key mappings.

Combination of Int 16/AH=01h and Int 16/AH=00h is the best for you:

startofCheck:
    MOV AH,01h
    INT 16h      ; CHECK FOR KEYSTROKE
    JZ NothingPressed
    MOV AH,00h 
    INT 16h      ; KEYBOARD - GET KEYSTROKE
    CMP AH,11h   ; W
    JE MovingUp
    CMP AH,1Eh   ; A
    JE MovingLeft
    CMP AH,1Fh   ; S
    JE MovingDown
    CMP AH,20h   ; D
    JE MovingRight
    CMP AH,01h   ; Esc
    JE Escape
    JMP EndKeyPress

Instead of WASD keys, users of your program might prefer ordinary curson arrows, which have scan codes 48h, 4Bh, 50h, 4Dh for directions Up, Left, Down, Right, respectively.

vitsoft
  • 5,515
  • 1
  • 18
  • 31
  • Using scancode may even spare the OP from having to handle Dalvik, AZERTY and other keyboard layouts (where WASD is not close together). But at the same time, it may come back to bite the OP as different model of keyboard with the same layout (but extra keys) may have different scancode for WASD. – Margaret Bloom Apr 17 '21 at 15:16
  • Thank you so much! i changed it and now it works like i wanted it to :DDD. I also accepted your recommendation and I'm using the move keys instead of WASD. I might still have wierd issues, but now atleast i can graphically test my game, so ty =D – GreenManchego1984 Apr 18 '21 at 13:21