CMPXCHG         Compare and Exchange                 Flags: O D I T S Z A P C
* - - - * * * * *
CMPXCHG destination,source CPU: 486+ (*)

Logic CMP destination,accumulator
if ZF = 1
destination ← source
else
accumulator ← destination
endif

CMPXCHG compares the accumulator (AL, AX, or EAX) with a
destination operand. If they are equal, the source operand is
loaded into destination; otherwise, the destination operand is
loaded into the accumulator.

This instruction is used to support semaphores.


Note
On the A-step of the 486, CMPXCHG was microcoded using the opcodes
for the, discarded, A- to B0-step 386 instructions XBTS (A6) and
IBTS (A7). Because of software conflicts with software written for
the early 386 DX, the opcodes for the 486 were changed to those
shown below starting with the B step.
Note that some 386 software won't run on older 386s and some 486
software will not run on early 486s when using this instruction.


Opcode Format
0F A6 /r CMPXCHG r/m8,r8 ; i486 pre-B step only
0F A7 /r CMPXCHG r/m16,r16 ; i486 pre-B step only
0F A7 /r CMPXCHG r/m32,r32 ; i486 pre-B step only
0F B0 /r CMPXCHG r/m8,r8
0F B1 /r CMPXCHG r/m16,r16
0F B1 /r CMPXCHG r/m32,r32


Length and timing
Operands Bytes 8088 186 286 386 486 Pentium
reg, reg 3 6 5 NP
mem, reg 3+d(0-2) 7-10 6 NP