CMPXCHG
Operation
Atomically compares memory with the expected register, conditionally stores, and returns the observed value.
Assembler Syntax
CMPXCHG.{B|W|L|Q}(z)/order(o) Rn(x), Rn(d), <ea>(e)
Privilege
Unprivileged
Compares memory with the expected Rn register and conditionally stores the desired Rn register. After the operation, the expected register contains the observed old memory value. FLAGS.Z=1 means the store happened; FLAGS.Z=0 means memory was left unchanged.
The memory read, optional memory write, expected-register update, and flag update form one atomic read-modify-write operation. The encoded memory-order selector governs the complete operation on both comparison outcomes. On failure, the operation contributes the observed memory read as its ordering event: acquire orders later memory operations after that read, release orders earlier memory operations before that read, and sequential consistency places that read in the global SC order. On success, the memory write carries the same selected order to observers of that write. A failed comparison contributes no memory write and creates no release sequence.
-
If memory equals the original expected value, the desired value is stored and
FLAGS.Zis set. -
Otherwise memory is unchanged and
FLAGS.Zis cleared. -
In both cases the expected register receives the value originally read from memory, and
FLAGS.N,FLAGS.C, andFLAGS.Vare cleared.
A memory-access fault exposes none of these updates.
2.75in
Encodings
CMPXCHG.{B|W|L|Q}(z)/order(o) Rn(x), Rn(d), <ea>(e)
Format: Instruction format for CMPXCHG.{B|W|L|Q}(z)/order(o) Rn(x), Rn(d), <ea>(e)
order — allowed: 0x0..0x4 [memory_order_reserved_reclaimed]
memory — excluded: immediate [memory_operand_required]