# `cmp`

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

**Compare two operands**

Category: Comparison

Subtracts the source from the destination and sets the flags, discarding the result.

8086 and later

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

Used in: [The x86-64 instruction set](https://asm-editor.specy.app/learn/courses/x86/introduction/the-x86-instruction-set.md) (x86-64 assembly), [The flags register](https://asm-editor.specy.app/learn/courses/x86/introduction/the-flags-register.md) (x86-64 assembly), [cmp and the conditional jumps](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/cmp-and-the-conditional-jumps.md) (x86-64 assembly), [Loops](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/loops.md) (x86-64 assembly), [Arrays, strings and the string instructions](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/arrays-strings-and-the-string-instructions.md) (x86-64 assembly), [call, ret and the System V convention](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/call-ret-and-the-system-v-convention.md) (x86-64 assembly), [Floating point and the x87 stack](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/floating-point-and-the-x87-stack.md) (x86-64 assembly), [AVX and 256-bit vectors](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/avx-and-wider-vectors.md) (x86-64 assembly), [syscall and the Linux x86-64 ABI](https://asm-editor.specy.app/learn/courses/x86/talking-to-the-outside-world/syscall-and-the-linux-abi.md) (x86-64 assembly), [Ports, memory-mapped devices, and kernel access](https://asm-editor.specy.app/learn/courses/x86/talking-to-the-outside-world/ports-devices-and-the-kernel.md) (x86-64 assembly)

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

- `cmp r/m, reg` (8, 16, 32, 64)
  - `cmp r/m8, reg8`
  - `cmp r/m16, reg16`
  - `cmp r/m32, reg32`: 386 and later
  - `cmp r/m64, reg64`
- `cmp reg, r/m` (8, 16, 32, 64)
  - `cmp reg8, r/m8`
  - `cmp reg16, r/m16`
  - `cmp reg32, r/m32`: 386 and later
  - `cmp reg64, r/m64`
- `cmp r/m, imm` (8, 16, 32, 64)
  - `cmp r/m16, imm8`
  - `cmp r/m32, imm8`: 386 and later
  - `cmp r/m64, imm8`
  - `cmp r/m8, imm8`
  - `cmp r/m16, imm16`
  - `cmp r/m32, imm32`: 386 and later
  - `cmp r/m64, imm32`
- `cmp acc, imm` (8, 16, 32, 64)
  - `cmp al, imm8`
  - `cmp ax, imm16`
  - `cmp eax, imm32`: 386 and later
  - `cmp rax, imm32`

## 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`.
- `imm`: An immediate value: a constant written in the instruction as a number, a character or a label, for example `10`, `0x1f`, `'a'` or `msg`. Open a row to see the size it has in each form, such as `imm8` or `imm32`.
- `acc`: The accumulator: `al`, `ax`, `eax` or `rax`, whichever matches the operation’s size. The form accepts no other register in this position.

## Description

Use signed or unsigned conditional instructions according to how the values should be interpreted.

The first operand is a register or memory operand; the second is a register, memory operand or immediate, with at most one memory operand. Register and memory operands have the same width. Neither operand is written, so `cmp eax, ebx` leaves the upper half of `rax` as it was.

An 8-bit immediate used with a wider operand is sign-extended from −128 through 127. For a 64-bit operation, the longer immediate field is only 32 bits and is also sign-extended; there is no full 64-bit immediate form. The encoded bits determine the value used by the operation. For example, a 32-bit field of `0xffffffff` becomes `0xffffffffffffffff` (−1), not positive 4294967295. An out-of-range constant can produce a warning and truncated bits; use a register for a value that cannot be represented by these sign extensions.

NASM normally chooses the short immediate encoding when it preserves the value; a `byte` qualifier is not required. `strict` with an immediate size can prevent that shortening.

## Example

```x86
global _start

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

        cmp rax, rbx

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