DYSTOPIANPIXEL

CPU registers

Meet the Z80 registers, register pairs, pointers and the programme counter.

Next Asset Lab concept artwork showing sprite patterns, palette colours and animation frames
Next Asset Lab concept artwork · Begin with Odin Dev.2k and the foundations of Z80 assembly.
TUTORIAL 1 / PART 06

What is a CPU register?

Registers are tiny storage locations inside the CPU

The processor needs somewhere to hold values while it works. Registers are small storage locations built into the CPU itself. They are not the same as ordinary RAM. They are part of the processor's working state.

The Z80 has several registers. Some are eight bits wide, and some can be paired to work with sixteen-bit values.

RegisterBeginner description
AThe accumulator. An important 8-bit register used by many arithmetic and logic instructions.
FThe flags register. Individual bits record facts about certain results.
B, CTwo 8-bit general-purpose registers; together they form BC.
D, ETwo 8-bit general-purpose registers; together they form DE.
H, LTwo 8-bit registers; together they form HL. HL is often used to hold a memory address.
IX, IY16-bit index registers, useful for addressing data structures and tables.
SPStack Pointer. Holds the address of the current top of the stack.
PCProgram Counter. Holds the address of the instruction the CPU is executing.

When would I use each register?

At first, the Z80 register names can feel like a collection of arbitrary letters. A natural beginner question is: if several registers can hold values, how do I know which one to choose?

The most important thing to understand is that most Z80 registers are not locked to a single job. You are allowed to use them in different ways. However, the Z80 instruction set makes some registers especially convenient for particular tasks, so programmers tend to develop recognisable habits and conventions.

RegisterWhen it is commonly useful
AArithmetic, comparisons, logical operations and many input/output operations. If you are calculating or testing a value, A is very often involved.
FStatus information. You normally do not use F as ordinary scratch storage. The CPU sets or clears individual flag bits after certain instructions.
BA very handy small counter. The Z80 has a DJNZ instruction that decrements B and jumps while B is not zero.
CGeneral temporary storage. C is also important in some Z80 input/output instructions, where it can help specify an I/O port.
BCA useful 16-bit counter or length. Z80 block instructions such as LDIR use BC as a byte count.
D / EGeneral 8-bit temporary storage. They are often useful when A, B, C, H and L already have more specific jobs.
DEFrequently used as a second 16-bit pointer or address. In a block copy, DE is commonly the destination address.
H / LGeneral 8-bit storage when used separately, although they are very often used together.
HLOne of the most important Z80 register pairs. HL is commonly used as the main pointer to memory. Many Z80 instructions are designed to work conveniently with the address held in HL.
IX / IY16-bit index registers. They are useful for records, structures, tables and data reached by an offset. They are powerful, but their instructions are often larger and slower than equivalent HL-based code, so we will introduce them later.
SPThe Stack Pointer. It points at the current top of the stack. It is not normally treated as a general-purpose working register.
PCThe Program Counter. It holds the address of the instruction being executed. Jumps, calls and returns change PC as part of program flow.

The table is a guide rather than a rule book. B is not permanently a counter, for example, and HL does not permanently have to contain an address. The reason these uses are common is that certain Z80 instructions make them especially convenient.

A simple mental model

While you are learning, the following rough mental model gives you somewhere sensible to start. It is not a formal rule of the processor:

RegisterUseful starting model
Avalue being calculated or tested
BCcount or length
DEsecond address / destination
HLmain address / pointer
IX/IYindexed or structured data
SPwhere is the stack?
PCwhere is the CPU executing?
Fwhat happened in the last relevant operation?

A small example

Imagine that a routine needs one value to calculate with, a loop count, and two memory addresses. A common Z80 arrangement might look like this:

    ld a,$05       ; value we want to work with
    ld b,10        ; small loop counter
    ld hl,$8000    ; main source address
    ld de,$9000    ; second address / destination

Do not worry about memorising this pattern. The purpose is to show that register choices are often made because they suit the instructions and the job at hand.

Why this matters for Next Asset Lab

Later in the project, Next Asset Lab will define its own calling conventions: which registers routines use for parameters, which registers may be changed, which must be preserved, and where return values are placed. That will make much more sense once you have seen the common natural uses of the Z80 registers first.

Registers contain values

It is useful to be precise with language. We will normally say that a register contains a value. For example, after LD A,$42, A contains $42. After LD HL,$6000, HL contains $6000.

The value in a register can be interpreted differently depending on how we use it. HL might contain the number $6000, but if our code uses HL to access memory, we call that value a pointer because we are treating it as an address.

A closer look at PC

PC is different from an ordinary working register because it controls instruction flow. Suppose the CPU is about to execute our later instruction LD A,$42 at address $6000.

$6000: 3E 42      LD A,$42
$6002: 21 00 60   LD HL,$6000
$6005: C9         RET

LD A,$42 occupies two bytes, so once those bytes have been fetched, execution continues at $6002. LD HL,$6000 occupies three bytes, so the following instruction begins at $6005. You do not normally calculate PC by hand; this example simply shows how instruction bytes and addresses fit together.

Register pairs

Some 8-bit registers can be viewed as pairs. For example, H and L can be treated separately, or together as the 16-bit register pair HL.

H = $60
L = $00

HL = $6000

This is exactly what we observed in Odin after executing LD HL,$6000.