Your place is saved automatically on this browser.
Before learning number systems or registers, it helps to have a simple picture of what the processor is doing. The Z80 is the CPU — the central processing unit. It repeatedly reads instructions from memory and carries them out.
The fetch–decode–execute cycle
A useful beginner model is a three-step cycle: fetch an instruction from memory, decode what that instruction means, then execute it. After that, the CPU continues with the next instruction unless the current instruction changes the flow.
1. FETCH Read the next instruction byte or bytes from memory
2. DECODE Work out which Z80 instruction those bytes represent
3. EXECUTE Perform the operation
4. REPEATThis happens extremely quickly, but the same basic idea applies whether the instruction is loading a register, adding two values, jumping somewhere else in the program or returning from a routine.
The CPU does not read our assembly source
When we type LD A,$42, the Z80 does not see the letters L and D. Odin, the assembler, translates our readable source into machine-code bytes. Those bytes are placed in memory. The CPU fetches and executes the bytes.
The Program Counter: where is the CPU executing?
The CPU needs to remember where it is in the program. The 16-bit PC register — the Program Counter — contains the address of the next instruction the processor is working through. Normally the CPU advances PC automatically as instruction bytes are consumed.
PC = $6000 fetch instruction at $6000
instruction occupies some number of bytes
PC advances to the address of the following instructionLater in this tutorial we will see our own program assembled at $6000. At that point this idea becomes concrete: $6000 is not merely a number on the screen; it is an address from which the processor can fetch instruction bytes.
