# `cmpxchg16b`

[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/cmpxchg16b

**Compare and exchange 16 bytes**

Category: Atomic

Compares rdx:rax with a 16-byte memory operand; if they match, stores rcx:rbx there, otherwise loads the operand into rdx:rax.

x86-64 and later · accepts the `lock` prefix with a memory operand

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

## Flags

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

## Forms

- `cmpxchg16b mem128`

## Operands

- `mem128`: A 128-bit value in memory, for example `oword [rbx]`.

## Description

ZF is set when they matched; otherwise it is cleared and the memory value is loaded into `rdx:rax`. The operand must be aligned to 16 bytes: a misaligned address raises a general-protection fault, which a Linux program receives as a segmentation fault. With `lock` the whole exchange is atomic, which is what it is for, such as updating a pointer together with a counter.
