# `cmpxchg8b`

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

**Compare and exchange 8 bytes**

Category: Atomic

Compares the 8-byte value in memory with edx:eax; if equal, stores ecx:ebx there and sets ZF, otherwise loads it into edx:eax.

Pentium 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

- `cmpxchg8b mem64`

## Operands

- `mem64`: A 64-bit value in memory, for example `qword [rbx+8]`.

## Description

If they are equal, it sets the zero flag and
stores `ECX:EBX` into the memory area. If they are unequal, it
clears the zero flag and stores the memory contents into `EDX:EAX`.

`CMPXCHG8B` can be used with the `LOCK` prefix, to allow atomic
execution. This is useful in multi-processor and multi-tasking
environments.

## Example

```x86
global _start

section .data
value: dq 7

section .text
_start:
        mov rax, 12
        mov rbx, 5
        mov rcx, 3
        mov rsi, 9

        cmpxchg8b [value]

        mov rax, 60         ; syscall 60: exit
        xor rdi, rdi
        syscall
```
