Z80 assembly: How a CPU runs code
Follow the Program Counter as the CPU fetches, decodes and executes four instructions.
The CPU reads the byte at PC, understands its instruction, and performs it. Then PC points to the next instruction. This example uses LD and HALT. There is no new opcode in this lesson.
What happened
Program listing
| Address | Machine code | Label | Instruction | Comment |
|---|
The program starts at 0100h. Its three instructions and HALT occupy 8 bytes. DB places 42h immediately after the code at 0108h.
Registers, flags and memory
Flags
A register stores a small value inside the CPU. PC means Program Counter. A T-state is one CPU clock period. A flag records a result. LD and HALT do not change flags in this example. This page ignores undocumented X and Y bits.
Sample data memory value at 0108h
Execution trace
| # | Address | Instruction | PC after | T-states |
|---|
Assembly and machine code
Assembly uses short names called mnemonics. The bytes in the second column are the machine code that the CPU fetches.
| Program part | Bytes | Code size | CPU time |
|---|---|---|---|
| Three LD instructions | 3E 05 06 03 21 08 01 | 7 bytes | 24 T-states |
| HALT | 76 | 1 byte | 4 T-states |
Simplification: this lesson has one 64 KB memory and no interrupts or input/output devices.
Mnemonic glossary
Program mnemonics and directives
| Name | Stands for | What it does here |
|---|---|---|
| LD | Load | Copies a number into a register. |
| HALT | Halt | Stops this lesson’s CPU. Interrupts are not modeled. |
| ORG | Origin | Assembler directive, not an instruction. Sets the code address. |
| DB | Define Byte | Assembler directive, not an instruction. Places a byte in memory. |