# `sub`

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

**Subtract**

Category: Arithmetic

Subtracts the value of the first operand from the second operand and stores the result in the second operand.

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

Used in: [The M68K instruction set](https://asm-editor.specy.app/learn/courses/m68k/introduction/the-m68k-instruction-set.md) (M68K assembly), [The condition code register](https://asm-editor.specy.app/learn/courses/m68k/introduction/the-condition-code-register.md) (M68K assembly), [Loops and dbra](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/loops-and-dbra.md) (M68K assembly), [Arrays, strings and (a0)+](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/arrays-strings-and-postincrement.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), [The bigger of two numbers](https://asm-editor.specy.app/learn/courses/m68k/examples/if-else.md) (M68K assembly), [Length of a string](https://asm-editor.specy.app/learn/courses/m68k/examples/string-length.md) (M68K assembly), [A jump table](https://asm-editor.specy.app/learn/courses/m68k/examples/jump-table.md) (M68K assembly), [Flappy ASM](https://asm-editor.specy.app/learn/courses/m68k/examples/flappy-asm.md) (M68K assembly), [Sampled sound](https://asm-editor.specy.app/learn/courses/m68k/examples/sampled-sound.md) (M68K assembly)

## Flags

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

## Forms

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

## 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).
- [`#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

If the second operand is an address register, the [SUBA](https://asm-editor.specy.app/documentation/m68k/instruction/suba.md) instruction is used instead.

## Example

```m68k
move.l #$100, d0
sub.b #$01, d0 ; only affects low byte

* --- Borrow: subtracting more than available ---
move.l #$00, d1
sub.b #$01, d1 ; carry flag set (borrow)

* --- Overflow ---
move.l #$80, d2
sub.b #$01, d2 ; $7F, V flag set
```
