1

I have a bit of a dilemma regarding the scenarios in which ZF is being set in NASM.

As far as I know, it's supposed to be set to 1 whenever the result of an arithmetic operation is 0, and it's being set to 0 otherwise. I saw that this does apply for ADD / SUB instructions, but it does not apply for DIV / MUL and their signed correspondents.

This is a bit of code that shows this.

bits 32

global start        

extern exit
import exit msvcrt.dll

segment data use32 class=data

segment code use32 class=code

start:
    mov ax, 128
    sar al, 7
    ; AL is now -1 signed, AH = 0
    imul ah
    ; AX = 0, however ZF is also 0

    push dword 0
    call [exit]

After googling for a few minutes, I have found on some course from a university's page that in the case of DIV / MUL instructions, the ZF is undefined.

Is this true, and if it is, why does this happen, even though after an operation whose result is 0, the ZF is not being set?

Jester
  • 56,577
  • 4
  • 81
  • 125
hiimsoba
  • 67
  • 1
  • 8
  • 4
    Yes, it is true. Consult the instruction set reference to see how each instruction affects the flags. E.g. for `MUL` it says: _"The OF and CF flags are set to 0 if the upper half of the result is 0; otherwise, they are set to 1. The SF, ZF, AF, and PF flags are undefined."_ – Jester Jan 25 '20 at 15:21
  • 1
    Why? No idea why the architects of 8086 didn't nail down the other FLAGS results of mul and imul. Or why any later CPUs like 386 or x86-64 didn't do that. Could have been useful sometimes, but they chose not to. I suggested 386 or x86-64 because those introduce new modes; code that runs in new modes doesn't need to care about old CPUs that might not set FLAGS the same way. – Peter Cordes Jan 25 '20 at 15:46

0 Answers0