Your place is saved automatically on this browser.
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.
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.
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:
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:
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 RETLD 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 = $6000This is exactly what we observed in Odin after executing LD HL,$6000.
