# `xadd`

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

**Exchange and add**

Category: Atomic

Exchanges the two operands, then stores their sum in the destination (first) operand.

486 and later · accepts the `lock` prefix with a memory operand

[Intel's reference: XADD](https://intel.github.io/SDM/instruction/XADD.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

- `xadd r/m, reg` (8, 16, 32, 64)
  - `xadd r/m8, reg8`
  - `xadd r/m16, reg16`
  - `xadd r/m32, reg32`
  - `xadd r/m64, reg64`

## 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

This instruction can be used with a `LOCK` prefix for
multi-processor synchronisation purposes.

## Example

```x86
global _start

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

        xadd rax, rbx

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