# `cp`

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

Web page: https://asm-editor.specy.app/documentation/z80/instruction/cp

**Compare**

Category: Comparison

Compares A with a register, memory byte or immediate value, setting flags as for subtraction without changing A.

Used in: [The F register](https://asm-editor.specy.app/learn/courses/z80/introduction/the-f-register.md) (Z80 assembly), [jp, jr and the conditions](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/jp-jr-and-the-conditions.md) (Z80 assembly), [8-bit and 16-bit arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/arithmetic-logic-and-bits.md) (Z80 assembly), [Arrays, strings and ix](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/arrays-strings-and-ix.md) (Z80 assembly), [The stack, push and pop](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/the-stack-push-and-pop.md) (Z80 assembly), [Ports: in and out](https://asm-editor.specy.app/learn/courses/z80/talking-to-the-outside-world/ports-in-and-out.md) (Z80 assembly), [The screen, keyboard and mouse through ports](https://asm-editor.specy.app/learn/courses/z80/talking-to-the-outside-world/screen-keyboard-and-mouse.md) (Z80 assembly), [Interrupts: im, ei, di and halt](https://asm-editor.specy.app/learn/courses/z80/talking-to-the-outside-world/interrupts-im-ei-di-and-halt.md) (Z80 assembly), [The bigger of two numbers](https://asm-editor.specy.app/learn/courses/z80/examples/if-else.md) (Z80 assembly), [The largest element](https://asm-editor.specy.app/learn/courses/z80/examples/max-of-an-array.md) (Z80 assembly)

## Flags

| Flag | Effect |
| --- | --- |
| S | depends on the result |
| Z | depends on the result |
| H | depends on the result |
| P/V | holds the signed overflow |
| N | always 1 |
| C | depends on the result |

## Forms

- `cp r` · 1 byte · 4 cycles · Subtracts `r` from A to set the flags, but leaves A unchanged.
  - `cp b`
  - `cp c`
  - `cp d`
  - `cp e`
  - `cp h`
  - `cp l`
  - `cp a`

- `cp (hl)` · 1 byte · 7 cycles · Subtracts (HL) from A to set the flags, but leaves A unchanged.

- `cp nn` · 2 bytes · 7 cycles · Subtracts `nn` from A to set the flags, but leaves A unchanged.

- `cp (ix+dd)` · 3 bytes · 19 cycles · Subtracts the value pointed to by IX plus `dd` from A to set the flags, but leaves A unchanged.
  - `cp (ix+dd)`
  - `cp (iy+dd)`

## Pseudo-instructions

The assembler also accepts these forms, which are other ways to write Z80 instructions. Open a row to see what each form becomes.

- `cp a, r` · 1 byte · 4 cycles · Subtracts `r` from A to set the flags, but leaves A unchanged.
  - `cp a, b` → `cp b`
  - `cp a, c` → `cp c`
  - `cp a, d` → `cp d`
  - `cp a, e` → `cp e`
  - `cp a, h` → `cp h`
  - `cp a, l` → `cp l`
  - `cp a, a` → `cp a`

- `cp a, (hl)` · 1 byte · 7 cycles · Subtracts (HL) from A to set the flags, but leaves A unchanged.
  - `cp a, (hl)` → `cp (hl)`

- `cp a, nn` · 2 bytes · 7 cycles · Subtracts `nn` from A to set the flags, but leaves A unchanged.
  - `cp a, nn` → `cp nn`

- `cp a, (ix+dd)` · 3 bytes · 19 cycles · Subtracts the value pointed to by IX plus `dd` from A to set the flags, but leaves A unchanged.
  - `cp a, (ix+dd)` → `cp (ix+dd)`
  - `cp a, (iy+dd)` → `cp (iy+dd)`

## Undocumented

Zilog’s manual does not list these forms, but the Z80 runs them.

- `cp ixh/l` · 2 bytes · 8 cycles · Subtracts `ixh/l` from A to set the flags, but leaves A unchanged.
  - `cp ixh`
  - `cp ixl`
  - `cp iyh`
  - `cp iyl`

## Pseudo-instructions · Undocumented

The assembler also accepts these forms, which are other ways to write Z80 instructions. Open a row to see what each form becomes. Zilog’s manual does not list these forms, but the Z80 runs them.

- `cp a, ixh/l` · 2 bytes · 8 cycles · Subtracts `ixh/l` from A to set the flags, but leaves A unchanged.
  - `cp a, ixh` → `cp ixh`
  - `cp a, ixl` → `cp ixl`
  - `cp a, iyh` → `cp iyh`
  - `cp a, iyl` → `cp iyl`

## Operands

- `r`: An 8-bit register: `a`, `b`, `c`, `d`, `e`, `h` or `l`.
- [`(hl)`](https://asm-editor.specy.app/documentation/z80/registers/operand-hl.md): The byte in memory at the address stored in `hl`.
- [`nn`](https://asm-editor.specy.app/documentation/z80/registers/operand-nn.md): An 8-bit immediate value, `0` to `255` or `-128` to `127`, written as a number, character constant or symbol.
- [`(ix+dd)`](https://asm-editor.specy.app/documentation/z80/registers/operand-ix-dd.md): The byte in memory at `ix + dd`, where `dd` is a signed displacement from `-128` to `127`, for example `(ix+5)` or `(ix-3)`. `(iy+dd)` works the same way.
- [`ixh/l`](https://asm-editor.specy.app/documentation/z80/registers/register-ixh.md): The high or low byte of `ix`: `ixh` or `ixl`. For `iy`, use `iyh` or `iyl`.

## Example

```z80
        .org 0x8000
        ld a, 5
        ld b, 3
        cp b
        halt
```
