How to read key without waiting for it, assembly 8086

assembly, keyboard, keystroke, x86-16

Solution

You are just checking if there is a character available, but are not actually reading the character from the buffer. So the next time you check, it is still there.

From this page on BIOS functions http://webpages.charter.net/danrollins/techhelp/0230.HTM

INT 16H, AH=1

Info: Checks to see if a key is available in the keyboard buffer, and
      if so, returns its keycode in AX.  It DOES NOT remove the
      keystroke from the buffer.

Problem

I'm making an space invaders in Assembly 8086, for testing I'm using DOSBox. Let me show you my code: ``` ;-------------------- ;Update hero ;-------------------- update: call vsync call update_hero ; method to read keyboard call set_video call clear_screen call draw_hero jmp update ``` Now the procedure update_hero is: ``` update_hero proc mov ah, 01h int 16h cmp al, 97 je left_pressed cmp al, 100 jne none_pressed inc hero_x left_pressed: dec hero_x none_pressed: ret update_hero endp ``` As you can see, I'm trying to read 'a' or 'd' for the movement, but, it's not working, can you help me to figure out why? What I'm trying to do is read from the keyboard without waiting for it, that's why I'm using the subfunction `ah, 01h`. Cheers. Edit I checked for the interrupts here, modified the code, and now it's working: ``` update_hero proc mov ah, 01h ; checks if a key is pressed int 16h jz end_pressed ; zero = no pressed mov ah, 00h ; get the keystroke int 16h begin_pressed: cmp al, 65 je left_pressed cmp al, 97 je left_pressed cmp al, 68 je right_pressed cmp al, 100 je right_pressed cmp al, 81 je quit_pressed cmp al, 113 je quit_pressed jmp end_pressed left_pressed: sub hero_x, 2 jmp end_pressed right_pressed: add hero_x, 2 jmp end_pressed quit_pressed: jmp exit end_pressed: ret update_hero endp ```

Original source