Assembly vs. Machine Code
Assembly language uses mnemonics (like LD
or ADD
) that are easy for humans to read. Machine code is the raw binary sequence of bytes (the opcodes) that the CPU directly executes. Understanding the encoding helps with optimization and debugging.
Opcode Structure: The Z80’s Chessboard
Most Z80 instructions fit into a flexible, 8-bit format that can be visualized as a grid. An 8-bit opcode is often broken into three 2- or 3-bit fields:
Bits | 7-6 (x) | 5-3 (y) | 2-0 (z) | Purpose |
---|---|---|---|---|
Field | Major Group | Register/Condition | Minor Group | Defines the operation type, register, and addressing mode. |
Example: The LD r, r'
Family
The instruction LD B, C
(Load value in C into B) has the raw opcode 41H
.
- Bits 7-6 (x=01): Identifies this as a standard 8-bit load/move instruction.
- Bits 5-3 (y=000): Identifies the destination register: B.
- Bits 2-0 (z=001): Identifies the source register: C.
Prefixes: Expanding the Instruction Set
The Z80 uses specific bytes as prefixes to signal that the following byte belongs to an extended instruction set. These prefixes make Z80 instructions one or two bytes longer but unlock powerful features.
Prefix (Hex) | Purpose | Example Mnemonics |
---|---|---|
CBH |
Bit Manipulation: Accesses the specialized BIT , SET , and RES instructions. |
SET 5, A (Two bytes: CB then the instruction) |
EDH |
Extended Instructions: Accesses Block I/O, RETI , IM , and SBC HL, ... |
LDIR (Two bytes: ED then the instruction) |