# Z80 Instructions

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

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

Every mnemonic with its operand forms, the flags it changes, its size in bytes and its clock cycles.

68 entries, each with its own page of markdown:

## Data movement

- [`ex`](https://asm-editor.specy.app/documentation/z80/instruction/ex.md) `af, af' | (sp), hl | de, hl | …`: Swaps AF with AF', DE with HL, or the word at (SP) with HL, IX or IY.
- [`exx`](https://asm-editor.specy.app/documentation/z80/instruction/exx.md): Swaps BC, DE and HL with BC', DE' and HL', the alternate register set.
- [`ld`](https://asm-editor.specy.app/documentation/z80/instruction/ld.md) `r, r' | r, nn | r, (hl) | …`: Copies a value between registers, memory and immediate data, 8 or 16 bits at a time.

## Stack

- [`pop`](https://asm-editor.specy.app/documentation/z80/instruction/pop.md) `rr | af | ix`: Loads BC, DE, HL, AF, IX or IY from the stack, incrementing SP once for each byte.
- [`push`](https://asm-editor.specy.app/documentation/z80/instruction/push.md) `rr | ix`: Stores BC, DE, HL, AF, IX or IY on the stack, decrementing SP once for each byte.

## Arithmetic

- [`adc`](https://asm-editor.specy.app/documentation/z80/instruction/adc.md) `a, r | a, (hl) | a, nn | …`: Adds a value and the carry flag C to A, or a register pair and C to HL.
- [`add`](https://asm-editor.specy.app/documentation/z80/instruction/add.md) `a, r | a, (hl) | a, nn | …`: Adds a value to A, or a register pair to HL, IX or IY.
- [`daa`](https://asm-editor.specy.app/documentation/z80/instruction/daa.md): Adjusts A after a BCD addition or subtraction so that each nibble holds a decimal digit.
- [`dec`](https://asm-editor.specy.app/documentation/z80/instruction/dec.md) `r | (hl) | (ix+dd) | …`: Subtracts one from a register, register pair or the byte at (HL), leaving C unchanged.
- [`inc`](https://asm-editor.specy.app/documentation/z80/instruction/inc.md) `r | (hl) | (ix+dd) | …`: Adds one to a register, register pair or the byte at (HL), leaving C unchanged.
- [`neg`](https://asm-editor.specy.app/documentation/z80/instruction/neg.md): Negates A, giving its two's complement, which is the same as subtracting A from zero.
- [`sbc`](https://asm-editor.specy.app/documentation/z80/instruction/sbc.md) `a, r | a, (hl) | a, nn | …`: Subtracts a value and the carry flag C from A, or a register pair and C from HL.
- [`sub`](https://asm-editor.specy.app/documentation/z80/instruction/sub.md) `r | (hl) | nn | …`: Subtracts a register, memory byte or immediate value from A.

## Logic

- [`and`](https://asm-editor.specy.app/documentation/z80/instruction/and.md) `r | (hl) | nn | …`: Sets A to the bitwise AND of A and a register, memory byte or immediate value.
- [`bit`](https://asm-editor.specy.app/documentation/z80/instruction/bit.md) `b, r | b, (hl) | b, (ix+dd)`: Tests one bit of a register or memory byte and sets Z when that bit is zero.
- [`cpl`](https://asm-editor.specy.app/documentation/z80/instruction/cpl.md): Inverts every bit of A, giving its one's complement.
- [`or`](https://asm-editor.specy.app/documentation/z80/instruction/or.md) `r | (hl) | nn | …`: Sets A to the bitwise OR of A and a register, memory byte or immediate value.
- [`res`](https://asm-editor.specy.app/documentation/z80/instruction/res.md) `b, r | b, (hl) | b, (ix+dd)`: Clears one bit of a register or memory byte to zero, leaving the other bits unchanged.
- [`set`](https://asm-editor.specy.app/documentation/z80/instruction/set.md) `b, r | b, (hl) | b, (ix+dd)`: Sets one bit of a register or memory byte to one, leaving the other bits unchanged.
- [`xor`](https://asm-editor.specy.app/documentation/z80/instruction/xor.md) `r | (hl) | nn | …`: Sets A to the bitwise exclusive OR of A and a register, memory byte or immediate value.

## Shifts and rotates

- [`rl`](https://asm-editor.specy.app/documentation/z80/instruction/rl.md) `r | (hl) | (ix+dd)`: Rotates a register or memory byte left one bit, moving C into bit 0 and bit 7 into C.
- [`rla`](https://asm-editor.specy.app/documentation/z80/instruction/rla.md): Rotates A left one bit, moving C into bit 0 and bit 7 into C.
- [`rlc`](https://asm-editor.specy.app/documentation/z80/instruction/rlc.md) `r | (hl) | (ix+dd)`: Rotates a register or memory byte left one bit, copying bit 7 to C and to bit 0.
- [`rlca`](https://asm-editor.specy.app/documentation/z80/instruction/rlca.md): Rotates A left one bit, copying bit 7 to C and to bit 0.
- [`rld`](https://asm-editor.specy.app/documentation/z80/instruction/rld.md): Rotates the low nibble of A and both nibbles of (HL) left by one digit, leaving the high nibble of A unchanged.
- [`rr`](https://asm-editor.specy.app/documentation/z80/instruction/rr.md) `r | (hl) | (ix+dd)`: Rotates a register or memory byte right one bit, moving C into bit 7 and bit 0 into C.
- [`rra`](https://asm-editor.specy.app/documentation/z80/instruction/rra.md): Rotates A right one bit, moving C into bit 7 and bit 0 into C.
- [`rrc`](https://asm-editor.specy.app/documentation/z80/instruction/rrc.md) `r | (hl) | (ix+dd)`: Rotates a register or memory byte right one bit, copying bit 0 to C and to bit 7.
- [`rrca`](https://asm-editor.specy.app/documentation/z80/instruction/rrca.md): Rotates A right one bit, copying bit 0 to C and to bit 7.
- [`rrd`](https://asm-editor.specy.app/documentation/z80/instruction/rrd.md): Rotates the low nibble of A and both nibbles of (HL) right by one digit, leaving the high nibble of A unchanged.
- [`sla`](https://asm-editor.specy.app/documentation/z80/instruction/sla.md) `r | (hl) | (ix+dd)`: Shifts a register or memory byte left one bit, copying bit 7 to C and putting zero into bit 0.
- [`sll`](https://asm-editor.specy.app/documentation/z80/instruction/sll.md) `r | (hl) | (ix+dd), r | …` (Undocumented): Shifts a register or memory byte left one bit, copying bit 7 to C and putting one into bit 0.
- [`sra`](https://asm-editor.specy.app/documentation/z80/instruction/sra.md) `r | (hl) | (ix+dd)`: Shifts a register or memory byte right one bit, keeping bit 7 and copying bit 0 to C.
- [`srl`](https://asm-editor.specy.app/documentation/z80/instruction/srl.md) `r | (hl) | (ix+dd)`: Shifts a register or memory byte right one bit, copying bit 0 to C and putting zero into bit 7.

## Comparison

- [`cp`](https://asm-editor.specy.app/documentation/z80/instruction/cp.md) `r | (hl) | nn | …`: Compares A with a register, memory byte or immediate value, setting flags as for subtraction without changing A.

## Control flow

- [`call`](https://asm-editor.specy.app/documentation/z80/instruction/call.md) `nnnn | cc, nnnn`: Pushes the return address onto the stack and jumps to a 16-bit address, either always or only when a flag condition is met.
- [`djnz`](https://asm-editor.specy.app/documentation/z80/instruction/djnz.md) `offset`: Decrements B and jumps to the target when B is not zero.
- [`jp`](https://asm-editor.specy.app/documentation/z80/instruction/jp.md) `nnnn | cc, nnnn | (hl) | …`: Jumps to a 16-bit address, or to the address held in HL, IX or IY, either always or only when a flag condition is met.
- [`jr`](https://asm-editor.specy.app/documentation/z80/instruction/jr.md) `offset | cc, offset`: Jumps to a target up to 128 bytes away, either always or only when a flag condition is met.
- [`ret`](https://asm-editor.specy.app/documentation/z80/instruction/ret.md) `[cc]`: Pops the top stack entry into PC, either always or only when a flag condition is met.
- [`rst`](https://asm-editor.specy.app/documentation/z80/instruction/rst.md) `00 | 08 | 10 | …`: Pushes the return address onto the stack and jumps to a fixed address from 00h to 38h in steps of 8.

## Block and string

- [`cpd`](https://asm-editor.specy.app/documentation/z80/instruction/cpd.md): Compares A with the byte at (HL), then decrements HL and BC, setting P/V while BC is not zero.
- [`cpdr`](https://asm-editor.specy.app/documentation/z80/instruction/cpdr.md): Repeats CPD, scanning down memory from HL for a byte equal to A, until a match is found or BC is zero.
- [`cpi`](https://asm-editor.specy.app/documentation/z80/instruction/cpi.md): Compares A with the byte at (HL), then increments HL and decrements BC, setting P/V while BC is not zero.
- [`cpir`](https://asm-editor.specy.app/documentation/z80/instruction/cpir.md): Repeats CPI, scanning up memory from HL for a byte equal to A, until a match is found or BC is zero.
- [`ldd`](https://asm-editor.specy.app/documentation/z80/instruction/ldd.md): Copies the byte at (HL) to (DE), then decrements HL, DE and BC, setting P/V while BC is not zero.
- [`lddr`](https://asm-editor.specy.app/documentation/z80/instruction/lddr.md): Copies bytes from (HL) to (DE), decrementing HL, DE and BC, until BC is zero.
- [`ldi`](https://asm-editor.specy.app/documentation/z80/instruction/ldi.md): Copies the byte at (HL) to (DE), increments HL and DE and decrements BC, setting P/V while BC is not zero.
- [`ldir`](https://asm-editor.specy.app/documentation/z80/instruction/ldir.md): Copies BC bytes from (HL) to (DE), incrementing both, until BC is zero.

## Input/output

- [`in`](https://asm-editor.specy.app/documentation/z80/instruction/in.md) `a, (nn) | r, (c)`: Reads a byte from an I/O port into a register, with the port addressed by BC or by A and nn.
- [`ind`](https://asm-editor.specy.app/documentation/z80/instruction/ind.md): Reads a byte from the port addressed by BC into (HL), then decrements HL and B.
- [`indr`](https://asm-editor.specy.app/documentation/z80/instruction/indr.md): Repeats IND, reading port bytes into memory downward from HL until B is zero.
- [`ini`](https://asm-editor.specy.app/documentation/z80/instruction/ini.md): Reads a byte from the port addressed by BC into (HL), then increments HL and decrements B.
- [`inir`](https://asm-editor.specy.app/documentation/z80/instruction/inir.md): Repeats INI, reading port bytes into memory upward from HL until B is zero.
- [`otdr`](https://asm-editor.specy.app/documentation/z80/instruction/otdr.md): Repeats OUTD, writing bytes from memory downward from HL to the port at BC until B is zero.
- [`otir`](https://asm-editor.specy.app/documentation/z80/instruction/otir.md): Repeats OUTI, writing bytes from memory upward from HL to the port at BC until B is zero.
- [`out`](https://asm-editor.specy.app/documentation/z80/instruction/out.md) `(nn), a | (c), r`: Writes A or another register to an I/O port, addressed by BC or by A and nn.
- [`outd`](https://asm-editor.specy.app/documentation/z80/instruction/outd.md): Decrements B, writes the byte at (HL) to the port addressed by BC, then decrements HL.
- [`outi`](https://asm-editor.specy.app/documentation/z80/instruction/outi.md): Decrements B, writes the byte at (HL) to the port addressed by BC, then increments HL.

## System

- [`ccf`](https://asm-editor.specy.app/documentation/z80/instruction/ccf.md): Inverts C, copying its old value into H and clearing N.
- [`di`](https://asm-editor.specy.app/documentation/z80/instruction/di.md): Resets both interrupt flip-flops, preventing maskable interrupts from triggering.
- [`ei`](https://asm-editor.specy.app/documentation/z80/instruction/ei.md): Sets both interrupt flip-flops, allowing maskable interrupts to occur after the next instruction.
- [`halt`](https://asm-editor.specy.app/documentation/z80/instruction/halt.md): Suspends CPU operation until an interrupt or reset occurs.
- [`im`](https://asm-editor.specy.app/documentation/z80/instruction/im.md) `0 | 1 | 2`: Selects interrupt mode 0, 1 or 2, which sets how the CPU responds to maskable interrupts.
- [`nop`](https://asm-editor.specy.app/documentation/z80/instruction/nop.md): Performs no operation, leaving registers and flags unchanged.
- [`reti`](https://asm-editor.specy.app/documentation/z80/instruction/reti.md): Returns from a maskable interrupt routine by popping PC and signaling the device that the interrupt has finished.
- [`retn`](https://asm-editor.specy.app/documentation/z80/instruction/retn.md): Returns from a non-maskable interrupt routine by popping PC and copying IFF2 into IFF1.
- [`scf`](https://asm-editor.specy.app/documentation/z80/instruction/scf.md): Sets C and clears H and N.
