If you have ever wondered why assemblers make two passes over your code, the answer is one word: forward references. You cannot write the address of a label you have not seen yet. A typical System Software exam style question: "Show the output of Pass 1 and Pass 2 for the following program." Let us do exactly that on a small program.
Disclosure: I help build GTU Hero, linked below. The program is a practice example I wrote. The opcodes follow the common hypothetical machine used in most Indian System Programming textbooks; your textbook's numbers may differ slightly, so follow the table your faculty gave.
The opcode table we will use
Imperative (IS): STOP 00, ADD 01, SUB 02, MULT 03, MOVER 04, MOVEM 05, COMP 06, BC 07, DIV 08, READ 09, PRINT 10 Declarative (DL): DC 01, DS 02 Directives (AD): START 01, END 02 Registers: AREG 1, BREG 2 Condition codes: LT 1, LE 2, EQ 3, GT 4, GE 5, ANY 6 The source program
START 100 READ N MOVER BREG, ONE AGAIN ADD BREG, N COMP BREG, LIMIT BC LT, AGAIN MOVEM BREG, RESULT PRINT RESULT STOP N DS 1 ONE DC '1' LIMIT DC '10' RESULT DS 1 END Note line 2: READ N uses N, but N is defined only near the bottom. A one pass assembler would not know its address here. That is the forward reference problem.
Pass 1: assign addresses, build the symbol table
The location counter (LC) starts at the value given in START, then increases by the size of each instruction or declaration. Every instruction here is 1 word. DS 1 reserves 1 word, DC takes 1 word.
| LC | Label | Statement |
|---|---|---|
| (none) | START 100 | |
| 100 | READ N | |
| 101 | MOVER BREG, ONE | |
| 102 | AGAIN | ADD BREG, N |
| 103 | COMP BREG, LIMIT | |
| 104 | BC LT, AGAIN | |
| 105 | MOVEM BREG, RESULT | |
| 106 | PRINT RESULT | |
| 107 | STOP | |
| 108 | N | DS 1 |
| 109 | ONE | DC '1' |
| 110 | LIMIT | DC '10' |
| 111 | RESULT | DS 1 |
Symbol table, numbered in order of first appearance in the source:
| # | Symbol | Address |
|---|---|---|
| 1 | N | 108 |
| 2 | ONE | 109 |
| 3 | AGAIN | 102 |
| 4 | LIMIT | 110 |
| 5 | RESULT | 111 |
N and ONE get entries before AGAIN because they are referenced on lines 2 and 3, before AGAIN is defined. Their addresses get filled in only when Pass 1 reaches their definitions.
Intermediate code (Variant I)
Pass 1 also writes intermediate code so that Pass 2 does not have to re-parse text:
(AD,01) (C,100) 100 (IS,09) (S,01) 101 (IS,04) (2) (S,02) 102 (IS,01) (2) (S,01) 103 (IS,06) (2) (S,04) 104 (IS,07) (1) (S,03) 105 (IS,05) (2) (S,05) 106 (IS,10) (S,05) 107 (IS,00) 108 (DL,02) (C,1) 109 (DL,01) (C,1) 110 (DL,01) (C,10) 111 (DL,02) (C,1) (AD,02) (S,03) means "symbol number 3 from the symbol table", (C,10) is a constant. In the BC line, (1) is the condition code LT.
Pass 2: replace symbols with addresses
Now every symbol has an address, so Pass 2 just looks them up:
100) 09 0 108 101) 04 2 109 102) 01 2 108 103) 06 2 110 104) 07 1 102 105) 05 2 111 106) 10 0 111 107) 00 0 000 108) 109) 00 0 001 110) 00 0 010 111) DS lines produce no code, only reserved space. DC lines produce the constant value.
What the program does (for fun)
It reads N, starts BREG at 1 and keeps adding N while BREG is less than 10. Nothing deep, but it has a backward jump and several forward references, which is what a trace question wants.
Common mistakes in exam answers
- Starting LC at 0 instead of the
STARToperand. - Giving
DS 5only 1 word. It reserves 5. - Forgetting the literal table and pool table when the program uses literals like
='5'. (This program avoids literals to stay short; add them when your question has them.)
Practise
Rewrite this program with one literal (MOVER BREG, ='1') and an LTORG, then redo both passes. For real past questions, the System Software page at https://gtuhero.com/gtu-papers/system-software has 9 papers under code 3160715, all 70 marks, from Winter 2022 to Summer 2026.
