Search code examples
assemblyx86masmirvine32eflags

ZF not set as a result of MUL instruction in assembly language


I am using masm615 assembler and textpad as an editor. I am writing 32 bit assembly program. In the program I am trying to set zero flag as a result of mul instruction but it is not working.

Can anyone tell me why the zero flag is clear while the result in eax register is zero?

    include irvine32.inc
    .data
    .code
    main proc
    xor eax,eax
    call dumpregs
    xor ebx,ebx
    call dumpregs
    mov eax,2
    call dumpregs
    mov ebx,3
    call dumpregs
    sub eax,2
    call dumpregs
    mul ebx
    call dumpregs
    exit
    main endp
    end main

Solution

  • In the program i am trying to set zero flag as a result of mul instruction

    The mul instruction doesn't define anything about whether or when the zero flag is set, cleared, or left unmodified. See Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2 page 3-594, or an HTML scrape of it:

    MUL—Unsigned Multiply

    ...

    Flags Affected

    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.

    If in doubt, always check the instruction documentation in the developers manual. Not all instructions affect the flags which one might expect, or the way one would expect.

    Undefined means different CPUs could be different. For example, Skylake sets ZF=0 regardless of output or previous value in a few test cases. It's possible but unlikely that there's some condition where that CPUs sets ZF=1. And of course other CPUs from other vendors (or emulators) could do anything they want.

    But unlike C "undefined behaviour", this is just an undefined value; it's still either a 0 or 1.