Search code examples
assemblytasm

Tasm KeyPress does not seem to be working


: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


Solution

  • 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.