Assembler design: one-pass and two-pass
Compare one-pass and two-pass assembler designs: definitions, algorithms, handling of forward references and literals, advantages/disadvantages, step-by-step traces and exam-style notes.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
An assembler converts human-readable assembly language into machine code. Engineers working on system software, boot code, device drivers or linking/loaders need to design or understand assemblers to create object code, debug low-level code and support linking and relocation. Choosing one-pass or two-pass design affects assembler speed, memory use, and how forward references and relocation are handled.
Key ideas
Start with simple definitions:
- Assembler: a program that translates each assembly-language instruction into the corresponding machine code (object code). It also processes assembler directives (pseudo-ops) and builds tables needed by linkers/loaders.
- Mnemonic / opcode: the symbolic name of a machine instruction (e.g., ADD). The opcode encodes the operation in binary in the instruction word.
- Operand: the value(s) or address(es) the instruction works on (e.g., R1, LABEL, =5).
- Symbol: a name (label) assigned to an address or constant by the programmer (e.g., LOOP:). The assembler keeps a symbol table mapping symbol → address (and attributes).
- Location counter (LOCCTR): a value the assembler maintains to assign addresses to subsequent instructions and storage. It starts at the program origin and advances by instruction or directive sizes.
- Forward reference: a reference to a symbol that is defined later in the source. Example:
JMP TARGETwhereTARGETis defined below. - Literal: a constant written in the source (e.g., =X'05'). Assemblers may place these in a literal pool and assign addresses when needed.
- Relocation: the process of adjusting addresses when a program module is placed at a different memory location than assumed. Object formats carry relocation information (e.g., modification records in SIC/XE, relocation entries in ELF) that mark which bytes/fields need adjustment.
Two classical designs:
Two-pass assembler (most common in textbooks):
- Pass 1: Scan source sequentially to compute addresses (LOCCTR) and build the symbol table. Write an intermediate file recording for each line: label, opcode, operand, LOCCTR, and any other data (literal references). Do not generate final machine code yet.
- Pass 2: Read the intermediate file, translate each instruction to object code using the symbol table (now complete). Produce object file with proper relocation records.
Exact invariants: After Pass 1, every symbol defined in the program must be in the symbol table with its address. Pass 2 relies on that to resolve all references.
Assumptions: fixed instruction sizes or ability to compute size from opcode/directive during Pass 1.
One-pass assembler:
- Single left-to-right pass. As each line is read, generate object code if possible. For forward references, the assembler must record unresolved references in a forward reference list (or use backpatching): insert placeholders into the output or the intermediate object and later patch them when symbol addresses become known.
- Often one-pass assemblers use a literal pool, deferred resolution records, or emit relocation entries for unresolved external symbols.
Trade-off: lower I/O and often faster because only one scan over source; more complex bookkeeping and may require additional in-memory forward-reference/literal tables (while avoiding an intermediate file) or more complex object formats.
How forward references are handled (precise mechanisms):
- In two-pass: simply resolved in Pass 2 from symbol table entries; no backpatching needed.
- In one-pass: use a forward reference table (FRT) listing (symbol, location(s) to patch). When symbol definition appears, assembler visits that list and patches generated object or records relocations to be applied by the loader.
Pseudo-ops and their usual semantics (definitions):
- START: sets the starting address; initializes LOCCTR. Syntax:
LABEL START addressorSTART address. - END: marks end of source and (often) gives first executable instruction address.
- ORG: set LOCCTR to a specified value (change address origin mid-source).
- EQU: define a symbol to a value (constant expression). If the expression is absolute, the symbol is absolute; if it is relocatable, the symbol inherits relocatable attributes (and section) and affects relocation.
- RESW/RESB/DS: reserve words/bytes (advance LOCCTR by given size but don't emit object code immediately).
- BYTE/WORD/DC: allocate and/or initialize storage bytes/words and emit corresponding object code or directives.
- BASE: set the base register value used for base-relative addressing during object code generation; does not change LOCCTR.
- LTORG: place the current literal pool at this point; assign addresses to all pending literals and advance LOCCTR accordingly.
Diagrams to draw (describe exactly):
- For two-pass flow: Draw two large boxes left-to-right labeled "Pass 1" and "Pass 2". From source file arrow into Pass 1. From Pass 1 draw arrows to symbol table (box), and an intermediate file (box). From intermediate file and symbol table arrows into Pass 2. From Pass 2 draw arrow to object file. Label each arrow with actions: "compute LOCCTR, record labels" for Pass 1; "resolve addresses, generate object code" for Pass 2.
- For symbol table layout: draw a table with columns: Symbol | Address | Length/Type | Relocatable? | ReferenceList (for one-pass). Show sample rows (LOOP | 0012 | code | R | [refs: 0004, 000A]).
Notes on assumptions and where I am unsure: I have assumed a simple fixed-size instruction set for worked examples (typical academic model like SIC or hypothetical 3-byte fixed instruction). Check that your exam syllabus uses such a model or requires variable-length ISA handling. Verify exact pseudo-op names and sizes used in your course's assembler model. In the worked example, opcodes shown (e.g., STL=14, LDB=68) are SIC values, but the encoding illustrated (OPCODE | ADDRESS) is a simplified absolute format that ignores SIC/SIC-XE nixbpe flags and base/PC-relative displacement; the BASE directive appears only to illustrate a directive and does not affect the simplified encoding here.
Formulas
LOCCTR = start_address — LOCCTR initialization at START directive. start_address in bytes.
LOCCTR = LOCCTR + instruction_length — after placing an instruction, advance LOCCTR by its length. instruction_length in bytes.
address(label) = LOCCTR — at the point the label is defined (Pass 1 in a two-pass assembler).
address(literal) is assigned when the literal pool is placed (e.g., at LTORG or END), not at first reference.
OUT = OPCODE | ADDRESS — object code assembly for simple fixed format instruction where | means concatenation of opcode field and address field. OPCODE: machine code bits; ADDRESS: resolved symbol address. Applies to fixed-format RISC-like examples.
Syntax to remember (source line): LABEL OPCODE OPERAND ; comment — LABEL optional, OPCODE is mnemonic or directive, OPERAND may be symbol, literal or expression.
Worked examples
Example 1 — Two-pass trace (fixed 3-byte instruction length model; all instructions are 3 bytes; BYTE is 1 byte, WORD is 3 bytes). Source (write this on the exam):
COPY START 1000(start at hex 1000)FIRST STL RETADR(STL is 3 bytes)LDB #LENGTH(LDB immediate, 3 bytes)BASE LENGTH(directive)RETADR RESW 1(reserve 1 word = 3 bytes)LENGTH WORD 3(one word = 3 bytes)END FIRST
Pass 1 (compute LOCCTR and symbol table):
- Line1: START 1000 → LOCCTR = 1000
- Line2: FIRST label at 1000. Instruction size 3 → LOCCTR becomes 1003
- Line3: no label; LDB at 1003 → LOCCTR = 1006
- Line4: BASE directive → LOCCTR unchanged (still 1006)
- Line5: RETADR label at 1006; RESW 1 → LOCCTR = 1009 (1006 + 3)
- Line6: LENGTH label at 1009; WORD → LOCCTR = 100C (1009 + 3)
- Line7: END → end. Symbol table: FIRST=1000, RETADR=1006, LENGTH=1009.
Pass 2 (generate object code):
- For
FIRST STL RETADR: OPCODE(STL)=14 (example) → object =14 1006(show hex concatenation) - For
LDB #LENGTH: immediate addressing, generate opcode and address field with LENGTH=1009 →68 1009 - For RETADR reserved: no object bytes emitted; loader/reserved location left as zero or undefined.
Final answer: list object records with addresses and bytes. (In exam, show each instruction's object code and where symbol addresses were taken: FIRST=1000, RETADR=1006, LENGTH=1009.)
Example 2 — One-pass with forward reference and backpatching
Source (simple):
START 0000JMP LABEL; forwardNOP; 1 byteLABEL MOV R1,R2; defines LABELEND
Assume JMP and MOV are each 2 bytes, NOP 1 byte.
One-pass actions step-by-step:
- LOCCTR=0000
- Line2:
JMP LABEL— symbol LABEL not yet defined. Emit JMP with placeholder address (00). Record in forward-list: (LABEL → patch at address 0000+1 if address field starts at offset 1). Advance LOCCTR by 2 → 0002. - Line3:
NOPat 0002. Advance LOCCTR to 0003. - Line4:
LABELdefined at 0003. Add LABEL→0003 to symbol table. Check forward-list for LABEL: found one entry at output offset 0001 (or object location 0000+1). Patch the placeholder in the emitted object to 0003. - Emitted final object: JMP 0003; NOP; MOV ...
State what to draw: a timeline of addresses 0000..0004, show emitted bytes with placeholder and patched bytes, and the forward reference list used for backpatch.
Common mistakes
- Using instruction size from pass 2 during pass 1 (must compute size in pass 1 to set addresses).
- Not recording or patching forward references in one-pass; writing final objects with zero addresses.
- Confusing
ORGandSTARTsemantics: ORG changes LOCCTR mid-source; START sets program base address at beginning. - Ignoring size of data directives (BYTE, WORD, RESW) when advancing LOCCTR.
- Assuming all assemblers can resolve external symbols without relocation records — the loader usually resolves externals.
For your exam
Question types:
- Definitions (define one-pass assembler, forward reference, intermediate file).
- Short notes (advantages/disadvantages of one-pass vs two-pass).
- Derivations/algorithms (write Pass 1 and Pass 2 pseudocode; draw flowchart of two-pass assembler).
- Numericals/traces (given a small program, produce symbol table after Pass 1 and object code after Pass 2; or show backpatching in one-pass).
- Programs (implement a simple assembler fragment in pseudocode to build a symbol table or perform backpatching).
- Diagrams (draw symbol table layout, intermediate file fields).
What a full-marks answer includes:
- Clear definitions of terms used.
- Correct initialization and step-by-step LOCCTR updates with units (addresses in hex/dec as given).
- Correct symbol table entries and intermediate-file representation for two-pass problems.
- For one-pass questions, explicit forward reference list entries and backpatch steps, or explanation of literal pool handling.
- Correct explanation of trade-offs (time, memory, implementation complexity, support for forward references and relocation).
Quick check
- What does the intermediate file produced by Pass 1 typically contain?
- Give one strong advantage and one disadvantage of a one-pass assembler.
- What is a forward reference and how does a two-pass assembler solve it?
Answers: 1) For each source line: LOCCTR, label, opcode, operand(s), and any literals or comments (enough to let Pass 2 generate object code). 2) Advantage: single scan → faster and less I/O; Disadvantage: requires backpatching / complex bookkeeping for forward refs and literals. 3) A forward reference is use of a symbol before its definition; two-pass solves it by building the symbol table in Pass 1 and resolving references in Pass 2.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?