# `sub`

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

**Subtract**

Category: Arithmetic

Subtracts a register, memory byte or immediate value from A.

Used in: [Registers, pairs and the shadow set](https://asm-editor.specy.app/learn/courses/z80/introduction/registers-pairs-and-the-shadow-set.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 bigger of two numbers](https://asm-editor.specy.app/learn/courses/z80/examples/if-else.md) (Z80 assembly), [Multiply and divide, with the remainder](https://asm-editor.specy.app/learn/courses/z80/examples/multiply-and-divide.md) (Z80 assembly), [A subroutine with its arguments in registers](https://asm-editor.specy.app/learn/courses/z80/examples/subroutine-with-register-arguments.md) (Z80 assembly), [Recursion: factorial and Fibonacci](https://asm-editor.specy.app/learn/courses/z80/examples/factorial-and-fibonacci.md) (Z80 assembly), [A jump table](https://asm-editor.specy.app/learn/courses/z80/examples/jump-table.md) (Z80 assembly), [Print a number in any base without help](https://asm-editor.specy.app/learn/courses/z80/examples/number-to-string.md) (Z80 assembly), [The snake game](https://asm-editor.specy.app/learn/courses/z80/examples/snake.md) (Z80 assembly)

## Flags

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

## Forms

- `sub r` · 1 byte · 4 cycles · Subtracts `r` from A.
  - `sub b`
  - `sub c`
  - `sub d`
  - `sub e`
  - `sub h`
  - `sub l`
  - `sub a`

- `sub (hl)` · 1 byte · 7 cycles · Subtracts (HL) from A.

- `sub nn` · 2 bytes · 7 cycles · Subtracts `nn` from A.

- `sub (ix+dd)` · 3 bytes · 19 cycles · Subtracts the value pointed to by IX plus `dd` from A.
  - `sub (ix+dd)`
  - `sub (iy+dd)`

## Pseudo-instructions

Other spellings the assembler accepts; each form shows the instruction it becomes.

- `sub a, r` · 1 byte · 4 cycles · Subtracts `r` from A.
  - `sub a, b` → `sub b`
  - `sub a, c` → `sub c`
  - `sub a, d` → `sub d`
  - `sub a, e` → `sub e`
  - `sub a, h` → `sub h`
  - `sub a, l` → `sub l`
  - `sub a, a` → `sub a`

- `sub a, (hl)` · 1 byte · 7 cycles · Subtracts (HL) from A.
  - `sub a, (hl)` → `sub (hl)`

- `sub a, nn` · 2 bytes · 7 cycles · Subtracts `nn` from A.
  - `sub a, nn` → `sub nn`

- `sub a, (ix+dd)` · 3 bytes · 19 cycles · Subtracts the value pointed to by IX plus `dd` from A.
  - `sub a, (ix+dd)` → `sub (ix+dd)`
  - `sub a, (iy+dd)` → `sub (iy+dd)`

## Undocumented

Left out of Zilog's manual, but the Z80 runs them, and so does this emulator.

- `sub ixh/l` · 2 bytes · 8 cycles · Subtracts `ixh/l` from A.
  - `sub ixh`
  - `sub ixl`
  - `sub iyh`
  - `sub iyl`

## Pseudo-instructions · Undocumented

Other spellings the assembler accepts; each form shows the instruction it becomes. Left out of Zilog's manual, but the Z80 runs them, and so does this emulator.

- `sub a, ixh/l` · 2 bytes · 8 cycles · Subtracts `ixh/l` from A.
  - `sub a, ixh` → `sub ixh`
  - `sub a, ixl` → `sub ixl`
  - `sub a, iyh` → `sub iyh`
  - `sub a, iyl` → `sub 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
        sub b
        halt
```
