A-Level Computer Science / Unit 4: CPU Operation and Low-Level Processing

4.2.5 Two-Pass Assembler

πŸ”’ Lesson slides are available to signed-in users. Sign in

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.

Core purpose: Pass 1 finds and records addresses. Pass 2 uses the recorded addresses to resolve labels and produce object code.

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.
LabelAddressMeaning of the entry
WRAP_UP864The 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 instructionSymbol-table lookupResolved 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.

Order matters: Pass 2 depends on the symbol table produced in Pass 1. Without that address information, a forward reference cannot be translated correctly.

4. Compare the passes

Assembler actionPassReason
Read through the source programBothEach pass processes the source for a different purpose.
Allocate instruction and data addressesPass 1The assembler needs to know where items are located.
Record each label and its addressPass 1These entries form the symbol table.
Remove comments or white spacePass 1Prepare source for processing
Look up a label used as an operandPass 2The symbol table supplies the address.
Translate instructions and generate object codePass 2Operands can now be resolved during translation.
Short summary: Pass 1 gathers address information; Pass 2 resolves operands and produces object code.

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.

TWO-PASS ASSEMBLER WALKTHROUGH

Pass 1: scan the source

Ready Β· Pass 1

Assembly source

  • 860JMP WRAP_UP
  • 861IN
  • 862ADD #29
  • 863OUT
  • 864WRAP_UP: END

Symbol table

LabelAddress
No labels recorded yet.
Current task

Pass 1 assigns addresses and records label definitions. It does not produce final object code.

Pass 2 translation

Start Pass 2 after Pass 1 is complete.

Pass 1

Select Next step to scan the source line by line.

Pass 1 and Pass 2 both read the source. Only Pass 2 resolves label operands and generates object code.

5. Avoid these common mix-ups

Mix-upCorrect explanation
Pass 1 generates the final object codePass 1 collects addresses and creates the symbol table; Pass 2 generates object code.
The symbol table stores instructionsIt stores label names and their addresses.
A forward reference is an undefined labelThe label is defined later in the source; the earlier instruction refers to it first.
Pass 2 ignores symbolic operandsPass 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.

  1. What does Pass 1 record in the symbol table?
    Suggested response

    Each defined label and the address where that label is located.

  2. A source instruction uses JMP WRAP_UP before the program defines WRAP_UP. What is this called?
    Suggested response

    A forward reference: the instruction uses a label before its definition appears.

  3. The symbol table contains WRAP_UP β†’ 864. What operand address can Pass 2 use for JMP WRAP_UP?
    Suggested response

    Pass 2 looks up WRAP_UP and uses address 864.

  4. Which pass generates the program's object code?
    Suggested response

    Pass 2, after it resolves any label operands using the symbol table.

  5. 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.