Memory Management with 16-bit Pointers

The Z80 is excellent at handling 16-bit memory addresses. The most common pairs for this are HL, DE, and BC.

Moving Data to/from Memory:

Remember that parentheses () mean “the contents of the address.”

Instruction Action Analogy
LD A, (HL) Loads the byte at the address in HL into A. Reads the note at the address in your address book.
LD (DE), A Stores the byte in A into the address in DE. Writes a note at the address in your address book.

Stepping Through Memory:

To move through a string or block of data, you must increment the pointer.

    LD   HL, START_OF_STRING
    INC  HL           ; HL now points to the next byte
    INC  HL           ; HL points to the byte after that

I/O Ports: Talking to Hardware

The Z80 communicates with external hardware (like keyboard controllers or sound chips) using dedicated I/O ports, which are separate from main memory.

Input/Output Commands:

Instruction Action
IN A, (N) Reads an 8-bit value from I/O port address N into the Accumulator (A).
OUT (N), A Writes the 8-bit value in the Accumulator (A) to the I/O port address N.

Advanced Addressing: Index Registers

The Index Registers ($IX$ and $IY$) are 16-bit pointers used for advanced memory access. They are slower but more flexible than HL, allowing for an immediate 8-bit displacement or offset.

Indexed Addressing Example:

    LD   IX, DATA_STRUCTURE_BASE
    
    ; Load the 5th byte (offset +4) from the structure into A
    LD   A, (IX + 4)
    
    ; Store 0FFH into the 10th byte (offset +9)
    LD   (IX + 9), 0FFH