Describe the different stages of the assembly process for a two-pass assembler
4.2 Assembly Language – The Two‑Pass Assembler
Think of the assembler as a translator that turns your human‑readable assembly code into the machine’s binary language. A two‑pass assembler does this in two stages, just like a detective first gathers clues and then solves the mystery.
Pass 1 – Building the Symbol Table
- Read the source line by line. For each line, record the address where the instruction will live. This is like noting the page number for each clue.
- When you see a label (e.g.,
LOOP:), store it in the symbol table with its current address. - Ignore forward references. If a label is used before it’s defined, just remember that it’s a placeholder – the real address will come later.
- Calculate instruction sizes. Each instruction may be 1, 2, or 3 bytes. Knowing the size lets you keep the address counter correct.
At the end of Pass 1 you have a complete symbol table:
| Label | Address |
|---|---|
| START | $0000$ |
| LOOP | $0003$ |
| END | $0009$ |
Pass 2 – Generating Machine Code
- Reset the address counter. Start again at the beginning of the source.
- Translate each instruction. Replace mnemonics with opcodes and operands with binary values.
- Resolve labels. Look up each label in the symbol table to get its real address.
- Output the machine code. Write the binary (or hex) representation to the object file.
Now the assembler has produced a binary file that the CPU can execute. 🎉
Analogy: Building a LEGO City
Imagine you’re building a LEGO city:
- Pass 1 is like planning the city map. You decide where each building (label) will go and note its coordinates (address).
- Pass 2 is the actual construction. You follow the map, placing each building at the right spot.
If you forget a building’s location in Pass 1, you’ll have to rebuild it in Pass 2 – just like a missing label causes a problem.
Exam Tips – What the Mark‑scheme Looks For
Key points:
- Explain the purpose of the symbol table.
- Show how forward references are handled.
- Describe how instruction sizes affect address calculation.
- Use an example symbol table (like the one above).
Remember: clarity beats length. Use diagrams or tables where possible.
Quick Quiz – Test Your Knowledge
1️⃣ If an instruction at address $0004$ is 2 bytes long, what will be the address of the next instruction?
Answer: $0006$ (because $0004 + 2 = 0006$).
2️⃣ Why does a two‑pass assembler need to read the source twice?
Answer: The first pass collects all labels and their addresses; the second pass can then replace label references with actual addresses.
Keep practicing, and soon you’ll be assembling code faster than a cheetah on a treadmill! 🏃♂️💨
Revision
Log in to practice.