Z80 addressing modes

Machine code was made simple. Everything it does is move data around and do basic logic/arithmetic operations. Every single CPU instruction can be mapped into those two things. Everything else in computer programming are just abstraction layers added on top of this simple mechanism. Amazing, isn’t it?

So, in order to do what they are supposed to do, instructions need operands. And instructions differ in how they get their operands. In general, instructions get an operand from one or more registers, from memory or from an external device. The way how instructions obtain theirs operands is called addressing mode. There are 10 addressing modes for the Z80: implied, immediate, extended immediate, register addressing, register indirect addressing, extended, modified page zero, relative, indexed and bit addressing.

Implied addressing mode: there is no bytes nor bits in the instruction op-codes to explicitly specify the operands, so they are implicit, as in CPL and LD SP, IY instructions. In general, instructions of this type have one or two op-codes.

Immediate addressing mode: instruction needs an explicit operand which is specified in the instruction itself (second ou third byte), and it’s always one-byte length. Example of this mode are instructions ADD A, N, XOR N and LD IXL, N, where N is a one-byte operand.

Extended immediate addressing mode: it is similar to the immediate mode but takes a two-byte operand. Instructions LD HL, NN and LD IX, NN have this kind of addressing.

Register addressing: in the op-code, there is a three-bit field to indicate one 8-bit register as the operand. RL, R and AND R are examples of this addressing mode, where R is a general purpose register (B, C, D, E, H, L and A).

Register indirect addressing: in this case, the operand itself is not in a register. Instead, a register pair contains the operand’s memory address. That’s why it is called indirect addressing. LD A, (BC) and INC (HL) are examples of this mode. Note the use of parenthesis around registers to denote the indirect addressing.

Extended addressing (or direct addressing): the instruction has a two-byte field containing the operand’s memory address. Instructions like LD A, (NN) and LD (NN), HL illustrate the direct addressing mode, where NN is a 16-bit value representing a memory address.

Modified page zero addressing: this mode is exclusive to a single instruction, RST P, which causes the CPU to jump execution to a specified location P, inside the page zero memory area. Page zero is a memory area comprised by the first 256 bytes of memory (usually ROM). P can assume only the following values: 0x0, 0x8, 0x10, 0x18, 0x20, 0x28, 0x30 and 0x38. RST P is normally used to call special ROM subroutines. (For the ZX Spectrum, see which ROM routines are available here).

Relative addressing: it is used in relative jump instructions (conditional and unconditional), in which the memory address to go to is calculated by adding an operand displacement to the PC register. The displacement value ranges from -128 to +127 (signed binary representation), allowing to address a location in memory from -126 to +129 relative to the jump instruction address (this is due to PC getting incremented by two during instruction execution). As an example, instruction JR Z, D, where D is the displacement, uses this kind of addressing mode. The relative addressing mode allows to reduce jump instructions by one byte, compared to absolute jump instructions (JP) where a 16-bit address operand is provided.

Indexed addressing: this mode allows one to use indirect addressing using the index registers IX and IY. Like the relative addressing mode, the instruction has an 8-bit displacement operand, which is added to the index register being used. AND (IX + D) and LD (IY + D), N are examples of the indexed mode, where D is an 8-bit signed displacement and N is an immediate operand.

Bit addressing: this mode is used in conjunction with previous modes to provide testing, setting and resetting any one of the 8-bits in an operand. In the instruction op-code, there’s an 8-bit field allowing to address any of the 8 bits in the operand. 0b000 addresses the least significant bit whereas 0b111 addresses the most significant bit. SET 0, B and SET 7, (HL) are example instructions of bit addressing.

As an exercise, access this page and try to identify more examples of instructions for the above Z80 addressing modes.