Skip to content
systemcoding.dev
Assembly

Z80 Registers: A, F, BC, DE, HL, IX, IY and the Shadow Set

Learn the Z80 register set: the accumulator and flags, the BC, DE and HL pairs, IX and IY, SP, the I and R registers and the shadow set swapped by EXX.

6 min read
Summarize in AI
Flat illustration of a hardware register drawn as bit fields with one field highlighted, with the Rust, C and Arm logos.

The Zilog Z80 has more than twenty registers, which is why a beginner opening its manual feels the opposite of what a 6502 programmer feels. The 6502 gives you three working registers and tells you to be clever. The Z80 gives you a dozen and asks you to learn which instruction accepts which. The structure is simple once you see it: one accumulator, six general registers that pair up, two index registers, a stack pointer, two housekeeping registers and a complete second copy of most of them. This post names each, says what the instructions expect from it, and ends with the shadow set. It follows the Zilog Z80 CPU User Manual (UM0080), and the examples use the mnemonics that sjasmplus and z80asm accept.

The register map

RegisterSizeNotes
A8Accumulator
F8Flags
B, C, D, E, H, L8 eachGeneral purpose, pairable as BC, DE, HL
IX, IY16Index registers, addressed with an offset
SP16Stack pointer
PC16Program counter
I8Interrupt vector high byte
R8Memory refresh counter
A', F', B', C', D', E', H', L'8 eachShadow set

The Z80 is little-endian like the 6502, so a 16-bit value in memory has its low byte first, and a register pair holds the high half in the first-named register: BC is B as the high byte and C as the low byte.

The accumulator A and the flags F

A is where arithmetic and logic results land. Instructions such as ADD A,B, SUB C, AND 0x0F, XOR A and CP 10 all take A as the implicit or explicit destination. Unlike the 6502, the Z80 can load A from any other 8-bit register in one instruction (LD A,B), and it can load it indirectly through BC, DE or HL (LD A,(DE)).

F holds the flags, which the Z80 reads and writes through instructions rather than directly. The documented bits, from 7 down, are S, Z, H, P/V, N and C. S is the sign, Z is zero, H is the half carry from bit 3 (used by the decimal adjust instruction DAA), P/V doubles as parity after logic operations and overflow after arithmetic, N records that the last operation was a subtraction (also for DAA), and C is the carry. To get at F as a whole you push AF and pop it into another pair:

readf.asmasm
        push af             ; A on the stack's high byte, F on the low
        pop bc              ; C now holds F, B holds A
        ld a,c              ; A = the flags byte

If the last operation was XOR A, the result is zero with no carry, so the flags register holds Z, P/V (parity of zero is even, so it is set), and nothing else: 0x44, the value of Z (bit 6, 0x40) and P/V (bit 2, 0x04). The two undocumented bits (5 and 3) copy bits of the result, which is zero here:

Text
A = 0x44

The pairs BC, DE and HL

The six general registers can be used alone as 8-bit values or together as 16-bit pairs. Which pair does which job is a convention baked into the instruction set:

PairTypical roleInstructions that favor it
HLpointer to memoryLD A,(HL), ADD A,(HL), ADD HL,rr, JP (HL), INC (HL)
DEdestination pointerLD A,(DE), EX DE,HL, block copies
BCcounterLDIR, CPIR, DJNZ (uses B), IN A,(C)

HL is the general-purpose address register. Among the main registers only HL supports direct arithmetic on memory (ADD A,(HL), INC (HL), SUB (HL)) and 16-bit addition (ADD HL,BC, ADD HL,DE, ADD HL,HL and ADD HL,SP). DE is second choice for pointers, and BC is the counter. The three are used together by the block instructions: LDIR reads from (HL), writes to (DE) and counts down BC.

A sequence that fills 16 bytes with a value shows the three pairs working together:

fill.asmasm
        ld hl,0x9000        ; HL: first address
        ld (hl),0xAA        ; store the fill value there
        ld de,0x9001        ; DE: the next address
        ld bc,15            ; BC: bytes still to fill
        ldir                ; copy (HL) to (DE), advance, repeat

Because LDIR copies from HL to DE one byte at a time and DE is one ahead, each copied byte is the one just written, so the value propagates. The result:

Text
[0x9000..0x900F] = AA (16 bytes)   BC = 0   HL = 0x900F   DE = 0x9010

IX and IY

The two index registers hold a 16-bit base address, and instructions take a signed 8-bit displacement: LD A,(IX+5) reads the byte five past IX. That fits records and stack frames. IX can point at a structure and (IX+0), (IX+1) and so on read its fields without changing a pair.

ix.asmasm
        ld ix,player        ; IX points at the structure
        ld a,(ix+0)         ; x position
        add a,(ix+2)        ; plus x velocity
        ld (ix+0),a         ; store back

player: db 10, 20, 3        ; x, y, dx

With player holding 10, 20 and 3, A becomes 13 and the first field is updated:

Text
[player] = 0D 14 03   A = 0x0D

The cost is size and speed: each instruction using IX or IY takes an extra prefix byte (DD for IX, FD for IY) and extra cycles, so a hot loop prefers HL. IX and IY can also be split into IXH, IXL, IYH and IYL, which assemblers accept, but Zilog does not document them and some Z80 clones may not behave identically, so avoid them in portable code.

SP and PC

SP is a full 16-bit register, so the stack can live anywhere, and you set it yourself with LD SP,0x0000 or any value. PUSH stores a register pair, high byte at the higher address, and decrements SP by two. PC is not readable, though a CALL to the next address and a POP of the return address reads it indirectly, a technique that position-independent code uses.

I and R

I holds the high byte of the interrupt table address used in interrupt mode 2, and R is a seven-bit counter that increments on every instruction fetch, so it serves as a cheap source of randomness. Neither is used much by a beginner, and R appears in later posts about random-number generation on machines with no timer.

The shadow set and EXX

The Z80 holds a second copy of AF, BC, DE and HL, written with a prime: AF', BC', DE' and HL'. Only one copy is active at a time, and two instructions swap them. EX AF,AF' swaps A and F with A' and F'. EXX swaps BC, DE and HL with their primes. The shadow registers have no instructions of their own, so you cannot use B' directly: you swap, use B, then swap back.

shadow.asmasm
        ld bc,0x1234
        exx                 ; BC' (the old shadow) is now active
        ld bc,0x5678        ; this writes the shadow copy
        exx                 ; back to the main set
        ; BC = 0x1234 again, BC' = 0x5678
Text
BC = 0x1234   BC' = 0x5678 (inactive)

The shadow set is worth two things. Interrupt handlers use it to save the main registers in two instructions instead of eight pushes. And a long calculation can keep a second working set in the shadows without touching memory. The price is that an interrupt handler and the main program must agree on who owns the shadows, because only one can use them at once. A handler that swaps and expects to find known values will fail if the main code left something else there.

What to remember

The register file looks big, and a short list covers most programs: use A for arithmetic, HL for memory pointers, DE as a second pointer, BC as a counter and B for DJNZ. Reach for IX when a structure needs field access, and for the shadows when speed or interrupt handling demands. On the Z80 the register is chosen by the instruction, not by taste.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.