3

My goal is to use the Int 16 instruction to be able to move up and down through a program using the arrow keys until my user decides to press the escape key. Do I read in multiple key presses using the following code in a loop and adding a terminate condition at the end or is there something that I am missing?

Mov ah,00
int 16
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Assembler246
  • 39
  • 1
  • 3
  • 1
    ["int 16,0"](http://stanislavs.org/helppc/int_16-0.html) is blocking, so it will wait for keypress. You should call it just once per "menu loop", so every key press from user is compared with all possible options. It's not very clear where you see problem, or what you are asking, plus this is "too broad"... just do some sketch drawing on paper what you have in input side, on ouput, what is happens where, and then try to write something, then spend evening in debugger, and see where it goes... then throw it out all, and try again. Simple. – Ped7g Apr 18 '17 at 14:24

1 Answers1

5
Mov ah,00
int 16

Assembly language programming needs you to be precise.
The BIOS keyboard functions are found at int 16h. That is 22 in decimal, not the 16 that you wrote! Might not seem a big deal, but it's the difference between success and failure.


MainLoop:
    mov ah, 01h    ;BIOS.TestKey
    int 16h
    jz  StuffToDoRegardlessOfAnyKey
    mov ah, 00h    ;BIOS.GetKey
    int 16h
    cmp al, 27     ;Is it ESCAPE ?
    jne ProcessOtherKey
ProcessEscapeKey:
    ...
    ...
ProcessOtherKey:
    ...
    ...
    jmp MainLoop
StuffToDoRegardlessOfAnyKey:
    ...
    ...
    jmp MainLoop

This is a skeleton program to solve the problem of navigating using the keyboard. On each iteration of the MainLoop it first tests with BIOS keyboard function 01h if there is a key pending.

  • If not it does all the stuff that needs to be done regardless of any user input. Refreshing a clock or any information on a status bar would fall in this category.
  • If a key is waiting, it is retrieved using BIOS keyboard function 00h. No waiting is involved since we know that a key is available.
    • If this key is ESC then the program falls through in ProcessEscapeKey where you can clean up and exit to DOS or whatever you need in this case.
    • For any other key it jumps to ProcessOtherKey where you normally will have to differentiate amongst all the different keys that you want to treat. UPDOWNLEFTRIGHT...
Fifoernik
  • 9,779
  • 1
  • 21
  • 27
  • `mov ah, 01h` followed by `int 16h` instructions you wrote segfaults on my laptop. (x86 64-bit architecture). – mercury0114 Aug 08 '20 at 16:13
  • @mercury0114: The interrupt 16h interface is only generally available in 16-bit Real or Virtual 86 Mode, not in 64-bit Long Mode. – ecm Dec 07 '21 at 08:06