By the end of this lesson you will be able to:
Each assembly instruction is translated by the assembler into a binary word that the CPU can execute. A typical 16‑bit word consists of an opcode field (identifying the operation) and an operand field (specifying a register, a constant or a memory address).
| Assembly | Binary machine code (16‑bit example) | Explanation |
|---|---|---|
LD 10 | 0001 0000 0000 1010 | Opcode 0001 = LD; operand 0000 0000 1010 = address 10 (direct). |
ADD 11 | 0010 0000 0000 1011 | Opcode 0010 = ADD; operand = address 11. |
ST 12 | 0011 0000 0000 1100 | Opcode 0011 = ST; operand = address 12. |
HLT | 1111 0000 0000 0000 | Opcode 1111 = HLT; operand field unused. |
Cambridge expects you to know that an assembler works in two distinct passes.
The source program is scanned line‑by‑line. Every label is recorded together with the address of the instruction that follows it. No object code is generated yet.
The source is scanned a second time. Using the symbol table, each instruction is translated into binary, and forward references (e.g. jumps to a label defined later) are resolved.
Example symbol table (Pass 1)
LABEL ADDRESS
START 0
LOOP 4
END 9
During Pass 2 the assembler replaces JMP LOOP with the binary representation of the address 4 (or a PC‑relative offset, see Section 3).
Source → Pass 1 → Symbol table → Pass 2 → Object code → Loader → Executable
The Cambridge syllabus requires you to recognise five modes. For each mode a short example is given; the examples use the same simple CPU as in the tables above.
| Mode | How the operand is interpreted | Example instruction | Result (ACC after execution) |
|---|---|---|---|
| Immediate | The operand is a constant value. | LD #5 ; ACC ← 5 | 5 |
| Direct | The operand is the exact memory address. | LD 20 ; ACC ← M[20] | M[20] |
| Indirect | The operand is a pointer; the address stored at that location is used. | LD @30 ; ACC ← M[ M[30] ] | M[ M[30] ] |
| Indexed | Effective address = contents of an index register (X) + displacement. | LD 5(X) ; ACC ← M[ X + 5 ] | M[ X+5 ] |
| PC‑relative (relative) | The operand is a signed offset added to the current PC to obtain the target address. Used for branches. | JMP #‑3 ; PC ← PC + (‑3) | PC moves three instructions back. |
; Program starts at address 0
START: LD #1 ; ACC ← 1
ADD #2 ; ACC ← 3
JMP #‑2 ; branch back to ADD (offset = –2)
HLT ; never reached
When the JMP #‑2 is fetched, the PC (which points to the JMP instruction) is incremented to the next address (3) and then the signed offset –2 is added, so the new PC becomes 1 – the address of the ADD instruction.
Only the registers that appear in the Cambridge specification are listed, together with the status flags that are used by branch instructions.
| Register | Purpose |
|---|---|
| PC (Program Counter) | Holds the address of the next instruction to be fetched. |
| IR (Instruction Register) | Temporarily stores the fetched instruction. |
| ACC (Accumulator) | Main arithmetic/logic register. |
| MAR (Memory Address Register) | Holds the address of the memory location to be accessed. |
| MDR (Memory Data Register) | Transfers data between memory and the CPU. |
| ALU (Arithmetic‑Logic Unit) | Performs arithmetic and logical operations on data from the ACC or other registers. |
| X (Index register) | Used for indexed addressing; holds a small integer that is added to a displacement. |
| Flags (Zero, Carry, Sign, Overflow) | Set by the ALU after an operation; consulted by conditional branch instructions (JZ, JC, etc.). |
For Paper 2 and Paper 4 candidates must be able to identify the category to which an instruction belongs. The categories are listed below together with a typical opcode and a short example that uses one of the five addressing modes.
| Category | Typical opcodes | Example (addressing mode) |
|---|---|---|
| Data‑movement | LD, ST | LD 20 ; direct |
| Arithmetic | ADD, SUB, MUL, DIV | ADD #3 ; immediate |
| Logical | AND, OR, NOT, XOR | AND @15 ; indirect |
| Input/Output | IN, OUT | IN 0 ; read from input port 0 |
| Compare / Test | CMP, TST | CMP 25 ; direct (sets flags) |
| Jump / Branch | JMP, JZ, JNZ, JC, JNC | JZ #2 ; PC‑relative |
| Sub‑routine | CALL, RET | CALL 100 ; direct address of sub‑routine |
| Control | HLT, NOP | HLT |
The program adds the contents of two memory locations (M[10] and M[11]) and stores the result in M[12].
; Simple addition – direct addressing
LD 10 ; ACC ← M[10]
ADD 11 ; ACC ← ACC + M[11]
ST 12 ; M[12] ← ACC
HLT ; stop
| Address | Content (decimal) |
|---|---|
| 10 | 7 |
| 11 | 5 |
| 12 | 0 |
| Step | PC | IR (fetched instruction) | ACC | Flags (Z C S V) | Memory change |
|---|---|---|---|---|---|
| 0 | 0 | — | 0 | 0 0 0 0 | Initial state |
| 1 | 1 | LD 10 | 7 | 0 0 0 0 | — |
| 2 | 2 | ADD 11 | 12 | 0 0 0 0 | — |
| 3 | 3 | ST 12 | 12 | 0 0 0 0 | M[12] ← 12 |
| 4 | 4 | HLT | 12 | 0 0 0 0 | Program stops |
ST (it stores ACC → memory, not the opposite).Initial memory: M[20]=3, M[21]=4, M[22]=0.
LD 20
SUB 21
ST 22
HLT
Provide a trace table similar to the one in Section 6.2 and state the final value stored in M[22].
Initial memory and registers (decimal):
| Address | Content |
|---|---|
| 30 | 40 |
| 40 | 8 |
| 41 | 2 |
| 42 | 3 |
| 50 | 0 |
Index register X is preset to 1.
LD #5 ; immediate – ACC = 5
ADD @30 ; indirect – address = M[30] (=40); ACC ← ACC + M[40] (=8)
SUB 41(X) ; indexed – effective address = 41 + X = 42; ACC ← ACC – M[42] (=3)
ST 50 ; store result in M[50]
HLT
Complete a trace table showing PC, IR, ACC, flags and any memory changes. What value is finally stored in M[50]?
Assume the following program starts at address 0. All registers are cleared initially.
START: LD #0 ; ACC ← 0
LOOP: ADD #1 ; ACC ← ACC + 1
CMP #5 ; set Zero flag when ACC = 5
JNZ #‑2 ; if not zero, branch back two instructions (to ADD)
HLT
Trace the program until it halts. Record the value of ACC after each iteration and indicate how the PC‑relative offset changes the PC.
PC ──► Instruction Memory ──► IR ──► Decoder
│ │
▼ ▼
MAR ◄───► Address Bus ◄───► Control Unit
│ │
▼ ▼
MDR ◄───► Data Bus ◄──────► ACC / ALU
│ │
▼ ▼
X (Index Register) ──► Flags Register
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.