Skip to content

Appendices07

Appendix 7 — Addressing, Prefixes and Forms


Addressing Shapes

ShapeExampleMeaningTypical use
immediate byteld a, $2Aconstant encoded in the instructionconstants, masks, small values
immediate wordld hl, $800016-bit constant encoded in the instructionaddresses, counters, setup
registerld d, acopy between registerscheap data movement
register pairadd hl, deoperate on a 16-bit pairaddresses, word arithmetic
register indirectld a, (hl)memory at address in HLpointer-based table walk
indexed indirectld a, (ix+3)memory at IX + displacementrecords, stack frames
absolute memoryld a, ($8000)memory at a fixed 16-bit addressglobals, I/O-mapped data
relative branchjr nz, loopbranch by signed offsetshort local branches
absolute branchjp nz, targetbranch to full 16-bit addresslong-range control flow

Prefix Families

PrefixFamilyWhat it usually means
nonebaseordinary documented Z80 instruction set
CBrotate/shift/bit familyRLC, BIT, RES, SET, and friends
EDextended familyblock ops, NEG, RETI/RETN, IM, RLD/RRD, 16-bit ADC/SBC, port forms
DDIX substitutionmany HL-based forms become IX-based
FDIY substitutionmany HL-based forms become IY-based
DD CB dindexed bit/shift familyoperate on (IX+d)
FD CB dindexed bit/shift familyoperate on (IY+d)

DD and FD do not magically legalise every HL instruction.


LD Quick Table

FamilyExamplesNotes
8-bit register to registerld a, b, ld d, hcommon and fast
immediate to registerld a, $2A, ld hl, $8000constants encoded in instruction
register with (HL)ld a, (hl), ld (hl), a, ld (hl), 0main indirect byte access
register with (IX+d) / (IY+d)ld a, (ix+3), ld (iy-1), aindexed access
A with (BC) / (DE)ld a, (bc), ld (de), aonly A is allowed
absolute memoryld a, ($8000), ld ($8000), a, ld hl, ($8000)globals and fixed addresses
stack pointer loadld sp, hl, ld sp, ix, ld sp, iyspecial-case form

Illegal pattern to remember:

z80
ld ($8001), ($8000)   ; impossible

Memory-to-memory moves must go through a register.


Arithmetic, Logic, and Compare Quick Table

FamilyMain formsResult goes toNotes
ADDadd a,x, add hl,ss, add ix,pp, add iy,rrfirst operand8-bit add is accumulator-based
ADCadc a,x, adc hl,ssfirst operandincludes carry
SUBsub xAaccumulator only
SBCsbc a,x, sbc hl,ssfirst operandsubtract with carry/borrow
ANDand xAaccumulator only
ORor xAaccumulator only
XORxor xAaccumulator only
CPcp xno stored resultflags only
INCinc r, inc rr, inc (hl), inc (ix+d)operand itselfchanges the operand in place
DECdec r, dec rr, dec (hl), dec (ix+d)operand itselfoften used for loops

Rotate, Shift, and Bit Quick Table

FamilyExamplesNotes
accumulator rotatesrlca, rrca, rla, rrashort one-byte accumulator forms
general rotatesrlc r, rrc r, rl r, rr rbase CB family
shiftssla r, sra r, srl rbase CB family
bit testbit n,r, bit n,(hl)tests a bit and leaves the operand as it stands
bit clearres n,r, res n,(hl)writes back changed value
bit setset n,r, set n,(hl)writes back changed value
indexed formsbit 3,(ix+2), srl (iy-1)DD CB d / FD CB d families
classic-undocumented shiftsll r / sls rwidely used but not part of the original documented set

Control Flow, Stack, and Exchange Quick Table

FamilyExamplesNotes
absolute jumpjp target, jp nz,target, jp (hl)long-range branch
relative jumpjr target, jr z,targetshort branch only
counted branchdjnz loopB := B - 1, branch if result not zero
call/returncall fn, ret, ret zuses hardware stack
restartrst $38call to fixed low-memory vector
stackpush bc, pop hlword-sized only
exchangeex de,hl, ex af,af', exx, ex (sp),hlswaps rather than copies
interrupt statedi, ei, im 0/1/2machine control, not everyday data movement

Block Instructions At A Glance

FamilyMnemonicsWhat they do
block transferLDI, LDIR, LDD, LDDRcopy bytes between (HL) and (DE) while updating pointers/counter
block compareCPI, CPIR, CPD, CPDRcompare A against bytes in memory while updating pointers/counter
block inputINI, INIR, IND, INDRport input plus memory store
block outputOUTI, OTIR, OUTD, OTDRmemory read plus port output