# `cmp`

[M68K Documentation](https://asm-editor.specy.app/documentation/m68k.md) › [Instructions](https://asm-editor.specy.app/documentation/m68k/instruction.md)

Web page: https://asm-editor.specy.app/documentation/m68k/instruction/cmp

**Compare**

Category: Comparison

Subtracts the first operand from the second operand and sets the flags, leaving both unchanged.

Sizes .b .w .l, .w by default

Used in: [The condition code register](https://asm-editor.specy.app/learn/courses/m68k/introduction/the-condition-code-register.md) (M68K assembly), [Compare and branch](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/compare-and-branch.md) (M68K assembly), [Loops and dbra](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/loops-and-dbra.md) (M68K assembly), [The screen, keyboard and mouse through traps](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/screen-keyboard-and-mouse.md) (M68K assembly), [Interrupts and cycles](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/interrupts-and-cycles.md) (M68K assembly), [The bigger of two numbers](https://asm-editor.specy.app/learn/courses/m68k/examples/if-else.md) (M68K assembly), [The largest element](https://asm-editor.specy.app/learn/courses/m68k/examples/max-of-an-array.md) (M68K assembly), [Reverse a string in place](https://asm-editor.specy.app/learn/courses/m68k/examples/reverse-a-string.md) (M68K assembly), [Recursion: factorial and Fibonacci](https://asm-editor.specy.app/learn/courses/m68k/examples/factorial-and-fibonacci.md) (M68K assembly), [Bubble sort](https://asm-editor.specy.app/learn/courses/m68k/examples/bubble-sort.md) (M68K assembly)

## Flags

| Flag | Effect |
| --- | --- |
| X | not changed |
| N | depends on the result |
| Z | depends on the result |
| V | depends on the result |
| C | depends on the result |

## Forms

- `cmp <ea>, Rn`
  - Source: Dn, An, (An), (An)+, -(An), d(An), d(An,Xn), abs, d(PC), d(PC,Xn), #imm (all modes)
  - Destination: Dn, An
- `cmp (An)+, (An)+`
- `cmp #imm, <ea>`
  - Destination: Dn, (An), (An)+, -(An), d(An), d(An,Xn), abs (data alterable)

## 68020 only

These forms require the 68020. Select `opt p=68020` before any code or data.

- `cmp #imm, <ea>`
  - Destination: d(PC), d(PC,Xn)

## Operands

- [`<ea>`](https://asm-editor.specy.app/documentation/m68k/addressing-mode.md): An operand using one of the addressing modes listed for this form. The list determines whether it can be a register, memory or an immediate value. With `opt p=68020`, an allowed `d(An,Xn)` or `d(PC,Xn)` mode also permits the corresponding 68020 indexed and memory indirect forms. See [68020 addressing](https://asm-editor.specy.app/documentation/m68k/addressing-mode/68020.md).
- [`Rn`](https://asm-editor.specy.app/documentation/m68k/addressing-mode/direct.md): A data or address register, `d0` to `d7` or `a0` to `a7`.
- [`(An)+`](https://asm-editor.specy.app/documentation/m68k/addressing-mode/indirect-post-pre-increment.md): Memory at an address register, followed by an increase of 1, 2 or 4 bytes for a byte, word or long access. A byte access through `sp` increases it by 2 to keep it even.
- [`#imm`](https://asm-editor.specy.app/documentation/m68k/addressing-mode/immediate.md): An immediate value written after `#`: a number, character or label, such as `#10`, `#$FF` or `#'a'`. It must fit the operation’s size: byte `-128` to `255`, word `-32768` to `65535`, or long `-2147483648` to `4294967295`; signed and unsigned spellings are accepted.

## Description

An address-register destination uses [cmpa](https://asm-editor.specy.app/documentation/m68k/instruction/cmpa.md).

On the 68020, an immediate source may also be compared with PC-relative memory, as in `cmp.w #1, flag(pc)`. That form assembles as [cmpi](https://asm-editor.specy.app/documentation/m68k/instruction/cmpi.md).

## Example

```m68k
move.l #10, d0
move.l #20, d1
cmp.l d1, d0 ; compares d0 - d1 (sets flags, d0 unchanged)
sgt d2 ; d2 = $FF if d0 > d1, else $00
slt d3 ; d3 = $FF if d0 < d1, else $00
seq d4 ; d4 = $FF if d0 == d1, else $00

* --- Equal values ---
move.l #42, d5
move.l #42, d6
cmp.l d6, d5
seq d7 ; d7 = $FF
```
