# `cmpxchg486`

[x86-64 Documentation](https://asm-editor.specy.app/documentation/x86.md) › [Instructions](https://asm-editor.specy.app/documentation/x86/instruction.md)

Web page: https://asm-editor.specy.app/documentation/x86/instruction/cmpxchg486

**Compare and exchange, 486 opcode** · Undocumented

Category: Atomic

Performs the cmpxchg compare-and-exchange under a non-standard opcode that some 486 processors accepted.

Early 486 processors only · accepts the `lock` prefix with a memory operand · Not available in 64-bit mode

[Intel's reference: CMPXCHG](https://intel.github.io/SDM/instruction/CMPXCHG.html)

## Flags

| Flag | Effect |
| --- | --- |
| CF | depends on the result |
| PF | depends on the result |
| AF | depends on the result |
| ZF | depends on the result |
| SF | depends on the result |
| DF | not changed |
| OF | depends on the result |

## Forms

- `cmpxchg486 r/m, reg` (8, 16, 32)
  - `cmpxchg486 r/m8, reg8`
  - `cmpxchg486 r/m16, reg16`
  - `cmpxchg486 r/m32, reg32`

## Operands

- `r/m`: A general-purpose register or memory location matching the operation’s size, for example `rax` or `qword [rbp-8]`.
- `reg`: A general-purpose register matching the operation’s size, for example `al`, `ax`, `eax` or `rax`.

## Description

`CMPXCHG` compares its destination (first) operand to the value in
`AL`, `AX` or `EAX` (depending on the operand size of the
instruction). If they are equal, it copies its source (second)
operand into the destination and sets the zero flag. Otherwise, it
clears the zero flag and copies the destination register to AL, AX or EAX.

The destination can be either a register or a memory location. The
source is a register.

`CMPXCHG` is intended to be used for atomic operations in
multitasking or multiprocessor environments. To safely update a
value in shared memory, for example, you might load the value into
`EAX`, load the updated value into `EBX`, and then execute the
instruction `LOCK CMPXCHG [value],EBX`. If `value` has not
changed since being loaded, it is updated with your desired new
value, and the zero flag is set to let you know it has worked. (The
`LOCK` prefix prevents another processor doing anything in the
middle of this operation: it guarantees atomicity.) However, if
another processor has modified the value in between your load and
your attempted store, the store does not happen, and you are
notified of the failure by a cleared zero flag, so you can go round
and try again.
