Z80 assembly: Loading data

Load one byte from memory, move it through registers, and write it to a new address.

LDr,nr,r′A,(HL)(HL),A

LD means Load. It copies a value. These forms do not change flags. LD r,n takes 7 T-states. LD r,r′ takes 4. The two memory forms take 7.

Source byte
Run speed

What happened

Program listing

AddressMachine codeLabelInstructionComment

The source byte starts at 010Dh, immediately after the 13-byte code block. The program copies it to 010Eh. The highlighted instruction is at PC.

Registers, flags and memory

Flags

A register stores a small value inside the CPU. HL is a register pair. The value in HL is a memory address. PC means Program Counter. A T-state is one CPU clock period. This page ignores undocumented X and Y bits.

Source HL points here

Destination HL points here before writing

Execution trace

#AddressInstructionResultT-states

Another way to copy one byte

; direct transfer through DE
LD HL,source
LD DE,dest
LD A,(HL)
LD (DE),A
HALT
ProgramCode sizeTime
This lesson’s register path13 bytes53 T-states
Direct transfer through DE9 bytes38 T-states

Both examples include address setup and HALT. The direct path needs DE, an extra register pair for the destination address.

Simplification: memory is a flat 64 KB array. Interrupts and input/output devices are not modeled.

Mnemonic glossary

Program mnemonics and directives
NameStands forWhat it does here
LDLoadCopies a value into a register or memory.
HALTHaltStops this lesson’s CPU. Interrupts are not modeled.
ORGOriginAssembler directive, not a CPU instruction. Sets the code address.
DBDefine ByteAssembler directive, not a CPU instruction. Places the source byte in memory.
DSDefine SpaceAssembler directive, not a CPU instruction. Reserves the destination byte.