cmpxchg8b

Compare and exchange 8 bytes

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 latertakes lock

Intel's reference: CMPXCHG8B/CMPXCHG16B

CFPFAFZFSFDFOF –––●–––
  • ● set from the result
  • – not changed

Forms

  • cmpxchg8b

Operands

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

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.

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