So far, you’ve used registers and register pairs to work with specific values and memory locations. In this lesson, we explore indexed addressing — a powerful feature of the Z80 that lets you access memory relative to a base address plus an offset.
On the Z80, true indexed addressing is supported only by the special 16-bit registers IX and IY. These registers allow the CPU to automatically add a signed offset (from -128 to +127) to the base address during memory access, for example:
LD A, (IX+3)
This instruction loads into A the byte stored at the address held in IX plus 3. This is especially useful when working with data tables, screen layouts, or structured memory.
Other registers like HL can also point to memory, but they do not support automatic offset calculation. If you want to use them in a similar way, you need to adjust the register value manually before accessing memory.
LD A, (IX+3)
This instruction means:
Load into register A the byte stored at the address: IX + 3
The offset can be any signed 8-bit value from -128 to +127.
Suppose you want to store three characters in memory and read them back later using offsets:
ORG 256
LD IX, 35900 ; Base address
LD A, 65 ; 'A'
LD (IX+0), A ; Store at base
LD (IX+1), A ; Store next byte
LD (IX+2), A ; Store third byte
You’ve just written 'A' into three consecutive memory locations.
LD A, (IX+0)
CALL PRINT ; Print first character
LD A, (IX+1)
CALL PRINT ; Print second character
LD A, (IX+2)
CALL PRINT ; Print third character
RET
PRINT: RST 8
DEFB 158
RET
This reads from memory using offsets relative to IX, and prints each character.
IX holds the base address.(IX+d)instruction accesses memory at base + d.LD A,(HL) — the CPU has to do extra work to calculate the offset.d) in a loop — d is part of the instruction, not a variable.IX or IY as a base with a fixed offsetLD r, (IX+d)or LD r, (IY+d)Write a program to:
'A', 'B', and 'C'using LD (IX+d),ALD A,(IX+d) and print each oneImagine a structure where:
Write code to access each field using indexed addressing from a base address.