Search code examples
assemblykeyboardx86tasmdosbox

Getting keyboard input without echo in DOS


I am making the game "Connect Four".

The players have to enter a number between 1-4 so that the disc will drop into one of the columns. I am currently working on the first column. The problem is that you can enter any character and it will work (it only needs to work when you press '1') and I cant figure out how to fix it.

Moreover, the number appears on the left side of the screen. How do I make it so when I enter the number it won't show on the screen?

PlayerOneTurn:
    cmp [Player1Turn], 255
    je Player1Pressed1

Player1Pressed1:
    mov ah, 1
    int 21h
    cmp al, 31h
    je Player1Check1

Player1Check1:
    cmp [FirstColumnArray], 0
    inc [FirstColumnArray]
    je DrawPlayer1Disc
    cmp [FirstColumnArray + 1], 0
    inc [FirstColumnArray]
    je DrawPlayer1Disc
    cmp [FirstColumnArray + 2], 0
    inc [FirstColumnArray]
    je DrawPlayer1Disc
    cmp [FirstColumnArray + 3], 0
    inc [FirstColumnArray]
    je DrawPlayer1Loop

DrawPlayer1Loop:
    mov bh,0h
    mov cx,[Player1Draw1x]
    mov dx,[Player1Draw1y]
    mov al,[player1disccolor]
    mov ah,0ch
    int 10h
    inc [Player1Draw1x]
    cmp cx, 38h
    jl DrawPlayer1Loop

DrawPlayer1Disc: 
    mov bh, 0h
    inc [Player1Draw1y]
    mov [Player1Draw1x], 25h
    cmp dx, 09Bh
    jl DrawPlayer1Loop

When run my project looks like this:

A picture of the object


Solution

  • mov ah, 1
    int 21h
    cmp ah, 31h
    

    The DOS function that you used produces a result in the AL register!
    Use cmp al, 31h to compare for a "1" keypress.

    To not have the input echoed on the screen use DOS function 7 in stead of 1.

    mov ah, 7
    int 21h
    cmp al, 31h
    

    Player1Pressed1:
     mov ah, 1
     int 21h
     cmp al, 31h
     je Player1Check1
    Player1Check1:
    

    With this code you always execute the code at Player1Check1. You need to jump away from it when the input is not "1". Add a jmp

    Player1Pressed1:
     mov ah, 1
     int 21h
     cmp al, 31h
     je Player1Check1
     jmp ELSEWHERE_YOU_KNOW_WHERE_THIS_IS
    Player1Check1: