-4

I used the int 16h interrupt, and I want to check if keystroke is available or not.

So I want to check what value the Zero Flag has - how can I do it?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Yair Haimson
  • 1
  • 1
  • 3
  • 4
    I assume x86-16, is that right? `jz` or `je` (depending on your style) will check if ZF=1. `jnz`/`jne` will check ZF=0. See [our doc](https://stackoverflow.com/documentation/x86/5808/control-flow/20470/conditional-jumps#t=201702060901496425342). – Margaret Bloom Feb 06 '17 at 09:01
  • 3
    At a minimum, get a copy of the Intel x86 and/or x86_64 instruction set manual. – Frank C. Feb 06 '17 at 10:21
  • Also if you don't want to branch, but rather value 0/1 may be useful for you in following code, you can use `setz / setnz` to set 8b value to 0/1 according to ZF (instead of `jz/jnz` branching). As you really need to test ZF after `int 16h, ah=1`, this answer is NOT relevant, but it may be next thing to learn (how to determine if value is zero): http://stackoverflow.com/a/41175294/4271923 – Ped7g Feb 06 '17 at 11:22
  • Possible duplicate of [Assembly je, jns, jle which register does it use?](http://stackoverflow.com/questions/38003988/assembly-je-jns-jle-which-register-does-it-use) – Govind Parmar Feb 06 '17 at 16:02

1 Answers1

3

As you can see here, ZF=1 when there is no keystroke available, and ZF=0 when there is a keystroke available. You can use the J(N)Z instructions to branch accordingly

Using JZ:

    mov ax, 0100h
    int 16h
    jz no_key
    ; Handle case if there is a key press here
    ; AH will contain the scan code; AL will contain the ASCII code

no_key:
    ; Handle case if there is no key press here

Using JNZ:

    mov ax, 0100h
    int 16h
    jnz key_pressed
    ; Handle case if there is no key press here

key_pressed:
    ; Handle case if there is a key pressed here
    ; AH contains the scan code; AL contains the ASCII code
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85