Search code examples
assemblyx86intelinstruction-setcompare-and-swap

How does CMPXCHG affect FLAGS register?


Intel official documentation says: "The ZF flag is set if the values in the destination operand and register AL, AX, or EAX are equal; otherwise it is cleared. The CF, PF, AF, SF, and OF flags are set according to the results of the comparison operation." However in art of assembly language by Randall Hyde it says that only ZF flag is affected.

And indeed in DosBOX only ZF flag gets affected. Did Intel change something for newer processors?


Solution

  • The documented behavior of CMPXCHG, ever since the 80486 when the instruction was first introduced, has always been to set flags according to the result of the comparison. See the 80486 Programmer's Reference Manual (large PDF).

    So it seems that this is an error in Hyde's book, and a bug in DOSBox.

    As fuz notes, CMPXCHG8B and CMPXCHG16B are different and they only set ZF, leaving the other flags unaffected. So it's possible that Hyde and/or the DOSBox implementers mistakenly assumed CMPXCHG was the same.