Two-Pass Assembler
A two-pass assembler reads assembly-language source twice. Pass 1 collects address information and builds a symbol table. Pass 2 uses that table to resolve label operands, translate instructions and produce machine-code object code.
By the end of this page, you should be able to:
- describe the purpose and order of Pass 1 and Pass 2;
- explain what the symbol table stores;
- explain how Pass 2 resolves a forward reference;
- distinguish address collection from instruction translation.
1. Why use two passes?
A forward reference happens when an instruction uses a label before that label is defined later in the source. When the assembler first reaches the instruction, it has not yet recorded the label's address.
JMP WRAP_UP appears before the later definition WRAP_UP: END.
The assembler needs the target address to translate the jump operand. It solves this by first reading the whole source to collect label addresses, then reading the source again to translate instructions.
2. Pass 1 builds the symbol table
Pass 1 reads the assembly source in order and works out where instructions and data items are located. When it finds a label definition, it records the label and its address in the symbol table.
- Read the source line by line and allocate an address to each instruction or data item.
- Record each defined label with the address where it is defined.
- Check that instruction opcodes are recognised.
- Do not generate the final object code during this pass.
| Label | Address | Meaning of the entry |
|---|---|---|
WRAP_UP | 864 | The instruction labelled WRAP_UP is located at this address. |
The symbol table stores names and addresses
A symbol table is a lookup list. It does not contain the program's translated instructions. Pass 1 creates the address information that Pass 2 will use.
3. Pass 2 resolves labels and translates
Pass 2 reads the source again. When an operand is a label, the assembler looks up that label in the symbol table and uses its address to complete the translated instruction. It then generates the machine-code object code.
| Source instruction | Symbol-table lookup | Resolved instruction |
|---|---|---|
JMP WRAP_UP |
WRAP_UP β 864 |
JMP 864, then object code is generated |
The label can be resolved even though its definition comes later in the source, because Pass 1 has already recorded its address.
4. Compare the passes
| Assembler action | Pass | Reason |
|---|---|---|
| Read through the source program | Both | Each pass processes the source for a different purpose. |
| Allocate instruction and data addresses | Pass 1 | The assembler needs to know where items are located. |
| Record each label and its address | Pass 1 | These entries form the symbol table. |
| Remove comments or white space | Pass 1 | Prepare source for processing |
| Look up a label used as an operand | Pass 2 | The symbol table supplies the address. |
| Translate instructions and generate object code | Pass 2 | Operands can now be resolved during translation. |
Interactive: run both assembler passes
Follow a short, original source program. In Pass 1, watch addresses and labels enter the symbol table. Then start Pass 2 and see how the earlier jump operand is resolved. The example uses fresh labels and addresses; the widget describes object-code generation without inventing binary encodings.
5. Avoid these common mix-ups
| Mix-up | Correct explanation |
|---|---|
| Pass 1 generates the final object code | Pass 1 collects addresses and creates the symbol table; Pass 2 generates object code. |
| The symbol table stores instructions | It stores label names and their addresses. |
| A forward reference is an undefined label | The label is defined later in the source; the earlier instruction refers to it first. |
| Pass 2 ignores symbolic operands | Pass 2 looks up label operands in the symbol table and substitutes their addresses during translation. |
6. Check your understanding
Try each question before opening the suggested response.
-
What does Pass 1 record in the symbol table?
Suggested response
Each defined label and the address where that label is located.
-
A source instruction uses
JMP WRAP_UPbefore the program definesWRAP_UP. What is this called?Suggested response
A forward reference: the instruction uses a label before its definition appears.
-
The symbol table contains
WRAP_UP β 864. What operand address can Pass 2 use forJMP WRAP_UP?Suggested response
Pass 2 looks up WRAP_UP and uses address
864. -
Which pass generates the program's object code?
Suggested response
Pass 2, after it resolves any label operands using the symbol table.
-
Why must Pass 1 happen before Pass 2?
Suggested response
Pass 1 builds the address information in the symbol table. Pass 2 needs that information to resolve label operands and translate the source into object code.
Final check
A strong explanation links the problem of a forward reference to Pass 1's symbol-table work, then explains how Pass 2 uses the recorded address to translate the instruction.