Assembly Process (Cambridge (CIE) A Level Computer Science) : Revision Note
Two-pass assembler
What is a two-pass assembler?
A two-pass assembler translates assembly language into machine code in two stages
It produces an object program that can be stored, loaded, and run later
To execute this program, another utility called a loader is needed
The assembler scans the source code twice:
In the first pass, it identifies all labels and records their memory addresses
In the second pass, it replaces those labels with the correct memory addresses in the machine code
This ensures the final machine code is accurate and ready to be executed
Example
The following program loads the value of
A
(5), adds the value ofB
(3), stores the result (8) inC
, then halts:
LOAD A
ADD B
STORE C
HALT
A: DATA 5
B: DATA 3
C: DATA 0
Pass 1:
The assembler reads each line and assigns memory addresses to labels (e.g. A, B, C), but does not translate the instructions yet
Address | Instruction | Label | Add to symbol table |
---|---|---|---|
| LOAD A | ||
| ADD B | ||
| STORE C | ||
| HALT | ||
| DATA 5 |
| A = |
| DATA 3 |
| B = |
| DATA 0 |
| C = |
Symbol table created:
Label | Address |
---|---|
A |
|
B |
|
C |
|
Pass 2:
The assembler replaces labels with their corresponding memory addresses and outputs machine code
In this example we assume the following opcodes for simplicity:
Mnemonic | Opcode |
---|---|
LOAD | 01 |
ADD | 02 |
STORE | 03 |
HALT | 00 |
DATA | - |
Now generate the object (machine) code:
Address | Assembly | Machine code | Explanation |
---|---|---|---|
| LOAD A |
| LOAD from address 04 |
| ADD B |
| ADD value at address 05 |
| STORE C |
| STORE into address 06 |
| HALT |
| Stop program |
| DATA 5 |
| Data value 5 |
| DATA 3 |
| Data value 3 |
| DATA 0 |
| Data value 0 (placeholder) |
Final object program:
01 04
02 05
03 06
00 00
00 05
00 03
00 00
Program tracing
What is a trace table?
A trace table is a table used to manually track the values of variables as a program runs
It helps to follow the flow of a program line by line, checking whether the logic works as expected
Trace tables are used to:
Understand how a program works
Debug errors in the logic
Predict outputs before running the program
Check variable values at each step
Example
An assembly program that loads the value from
A
(4) into the accumulator, adds the value fromB
(2), stores the result (6) intoC
, and halts
LOAD A
ADD B
STORE C
HALT
A: DATA 4
B: DATA 2
C: DATA 0
To trace the program, assumptions must be made:
Accumulator (ACC): Used for calculations
Memory is addressed from 00 onwards
Simple instructions:
LOAD X
→ ACC = memory[X]ADD X
→ ACC = ACC + memory[X]STORE X
→ memory[X] = ACCHALT
→ Stop
Trace table
Step | Instruction | ACC (Accumulator) | Memory[A] | Memory[B] | Memory[C] |
---|---|---|---|---|---|
0 | (Start) | 0 | 4 | 2 | 0 |
1 | LOAD A | 4 | 4 | 2 | 0 |
2 | ADD B | 6 | 4 | 2 | 0 |
3 | STORE C | 6 | 4 | 2 | 6 |
4 | HALT | 6 | 4 | 2 | 6 |
Final outcome:
ACC = 6
C = 6
The program has successfully added A and B, then stored the result in C
You've read 0 of your 5 free revision notes this week
Unlock more, it's free!
Did this page help you?