Search code examples
assemblyx86eflags

When does the cmp instruction modify the zf flag? [x86 MASM]


Given that "...the Zero flag (ZF) is set when the result of an arithmetic or logical operation generates a result of zero", how does the cmp instruction interact with ZF?

Given the following code snippet,

mov eax, 0
mov edx, 1
cmp eax, edx

how does cmp evaluate the two operands and what is ZF set to? If the operands were reversed (cmp edx, eax), then is ZF set to the same value as in the original statement?

If I were to compare two identical values, how does cmp evaluate the operands and what is ZF set to?

cmp eax, eax

Solution

  • how does cmp evaluate the two operands?

    Straight from the manual:

    The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction.


    If the operands were reversed (cmp edx, eax), then is ZF set to the same value as in the original statement?

    For the ZF, yes. For the other flags, not necessarily, since subtraction isn't commutative.