Assembly Language Fundamentals

Intermediate
Version:
1.0

Indexed Addressing

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.

Syntax

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.

Example: Reading from Indexed Memory

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.

Reading Back the Values

       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.

How It Works

Important Notes

Summary

Exercises

Exercise 9.1 — Print Using Offsets

Write a program to:

Exercise 9.2 — Structure Lookup

Imagine a structure where:

Write code to access each field using indexed addressing from a base address.

Previous Module
Next Module