Operating Systems · UGC NET: System Software
Assemblers
Overview
An assembler is system software that translates a program written in assembly language into relocatable machine code and related object-program information.
It sits at the boundary between symbolic programming and the actual instruction set of a processor, so it is closely tied to machine architecture.
To study assemblers you must already know basic instruction formats, addressing modes, the idea of a location counter, and simple data structures such as arrays and linked lists.
UGC NET tests this topic through pass structure, symbol and literal tables, assembler directives, forward references, location-counter arithmetic, and differences between one-pass and two-pass assemblers.
Key concepts
- An assembler converts assembly-language mnemonics and symbolic operands into machine instructions. The output is normally an object module, not a final loaded program.
- Assembly-language statements are of three broad kinds: imperative statements such as
MOV,ADD,JMPthat become machine instructions; declarative statements such asDSandDCthat reserve or define data; and assembler directives or pseudo-operations such asSTART,END,ORIGIN,EQU,LTORG,EXTRN,ENTRYthat instruct the assembler itself. - One imperative assembly statement normally corresponds to one machine-code instruction. A micro-operation is smaller than a machine instruction, so “one assembly instruction = one micro-operation” is wrong.
- An assembler is machine-dependent: it must know the opcodes, instruction formats, addressing modes, register set, and instruction lengths of the target processor.
- A two-pass assembler scans the source twice.
Pass 1 separates label, mnemonic, operand, and comment fields; updates the location counter (LC); builds the symbol table; processes address-affecting directives; builds the literal table; records forward references in the forward reference table (FRT) or fix-up lists; and produces intermediate code. It does not generate final machine code. - Pass 2 reads the intermediate code, resolves symbols using the symbol table, patches forward references, assembles machine code using the mnemonic/opcode table, processes literals and base registers, and emits the object program.
- The symbol table stores each label or symbol with its value (address or
EQUvalue) and useful attributes such as relocatable/absolute flag and type. It is built in the first pass of a conventional two-pass assembler. - The mnemonic/opcode table is a permanent table mapping each mnemonic to its machine opcode, instruction format, and instruction length. The pseudo-op table maps directives to their handling routines.
- The literal table records literals such as
='5'or=C'AB'and their assigned addresses.
Literals are placed in a literal pool;LTORGassigns addresses to pending literals at the current value of LC. The pool table records the beginning of each literal pool. - A forward reference is a use of a symbol before its definition. It is recorded in an FRT/fix-up list, usually as a linked list of instruction addresses, and is resolved in pass 2.
A backward reference uses a symbol already defined and can be resolved immediately. - A one-pass assembler may leave operand fields incomplete when a forward reference is met and patch them later from a fix-up list.
A load-and-go assembler assembles and loads the object program directly into memory for execution, avoiding the overhead of writing an object program out and reading it back.
This saving is independent of whether the assembler is one-pass or two-pass. - Permanent database is an older language-processor term for a table prepared once and reused.
In the general model, the terminal table has one entry for each terminal symbol of the source language, and the identifier table stores identifier attributes; in assembler terminology, the per-program identifier table is the symbol table. For an assembler, the mnemonic/opcode table and pseudo-op table are permanent for that assembler, while symbol and literal tables are built afresh for each assembly. - A segment register table, used on segmented machines, records segment-register information such as which segment register holds a base address; it is commonly implemented as an array indexed by segment-register number.
- A symbol defined by
EQUreceives the value of an expression. If that expression contains a forward-referenced symbol, the value may not be final in pass 1 and must be handled by pass 2 or a fix-up mechanism. - Machine-independent assembler features include source-field scanning, symbol-table management, literal-table management, and directive interpretation.
Machine-dependent features include opcode lookup, instruction length, addressing-mode encoding, relocation information, and actual binary code generation.
How it works
A conventional two-pass assembler works as follows.
- Read one source line and separate it into label, mnemonic/opcode, operand, and comment fields. This is necessary because labels and operands need different processing from opcodes.
- If the label field contains a symbol, enter the symbol in the symbol table with the current LC as its value. If the symbol already exists, report a duplicate-label error.
- Look up the mnemonic. If it is a machine instruction, find its length and format from the opcode table. If it is a declarative statement such as
DSorDC, determine how many bytes it reserves or defines.
If it is a directive, execute its pass-1 action. - For
START, set the initial LC. ForORIGIN, change LC to the specified address. ForEQU, assign the expression value to the symbol. ForEND, stop pass 1. ForLTORG, assign addresses to pending literals. - After processing an item, advance the LC by the item’s size. Thus a label gets the address of the item that follows it.
- When an operand contains a symbol not yet in the symbol table, record the reference in the FRT/fix-up list.
When an operand contains a literal, enter it in the literal table if it is new; its address is assigned at the nextLTORGor at the end. - Emit intermediate code. This contains opcode information and operand placeholders; it is not yet final machine code.
In pass 2:
- Read each intermediate-code line.
- Replace symbolic operands by their values from the symbol table.
- Patch every forward reference using its fix-up list or FRT entry.
- Compute displacements for relative addressing, base-register addressing, and similar modes.
- Convert opcode and operands into binary machine code.
- Assign addresses to literals at pools and complete object records.
- Report any symbol still undefined at the end.
Drawing the diagram…
Formulas
-
Let be the current location counter, the number of bytes occupied by the current item, the address of the current instruction, its length, the value of the target symbol, and the displacement. Use the convention that the program counter points to the next instruction for PC-relative addressing.
-
Next location counter:
-
Value assigned to a label:
-
Address assigned to a literal at
LTORG:
-
PC-relative displacement:
-
Base-register displacement, when is the base-register value:
Worked examples
Example 1: location counter and symbol table.
Consider:
START 200
L1: MOV R1, R2 ; 2 bytes
L2: DS 4
L3: ADD R1, R3 ; 2 bytes
END
START 200 sets .
L1 gets value 200; the 2-byte instruction advances to 202.
L2 gets value 202; DS 4 advances to 206.
L3 gets value 206; the 2-byte instruction advances to 208.
Final symbol table: , , ; final LC is 208.
Example 2: ORIGIN creates a gap.
START 100
A1: MOV R1, R2 ; 3 bytes
B1: DS 4
ORIGIN 500
C1: ADD R1, R2 ; 2 bytes
END
A1=100; after 3 bytes .
B1=103; after DS 4, .
ORIGIN 500 sets ; bytes 107 through 499 are not filled by this code.
C1=500; after 2 bytes final .
Example 3: literal table and LTORG.
Assume each instruction below is 4 bytes and the literal ='5' occupies 1 byte.
START 0
I1: LOAD ='5'
I2: ADD R1, R2
LTORG
I3: SUB R1, R3
I4: MUL R1, R4
I5: DIV R1, R5
END
I1=0; literal ='5' is entered as pending; .
I2=4; .
LTORG assigns the literal address 8; literal size 1 advances to 9.
I3=9; .
I4=13; .
I5=17; final .
Literal table: ='5' at address 8. Symbol table: , , , , .
Example 4: relative displacement, two-step calculation.
An instruction at address 100 is 2 bytes long and refers to label L defined at address 130. With the program counter pointing to the next instruction,
The displacement is 28. If a question explicitly says the PC points to the current instruction, use ; always follow the stated convention.
Example 5: match-the-list style.
Match List-I with List-II.
| List-I | List-II |
|---|---|
| A. Symbol table | i. mnemonic to opcode and format |
| B. Literal table | ii. unresolved forward references |
| C. Mnemonic table | iii. label and its value |
| D. Forward Reference Table | iv. literal and its assigned address |
Correct mapping: A-iii, B-iv, C-i, D-ii. Reason: symbol table stores labels and values; literal table stores literals and addresses; mnemonic table maps mnemonics to machine opcodes; FRT records references not yet resolved.
Comparison
| Point | Pass 1 | Pass 2 |
|---|---|---|
| Main purpose | Define symbols and addresses; build tables | Generate machine code and object program |
| Location counter | Used and updated | Usually not used to define symbols |
| Symbol table | Built | Used |
| Literal table | Built; literals entered | Literal addresses used and pools completed |
| Forward references | Recorded in FRT/fix-up list | Resolved and patched |
| Machine code | Not generated | Generated |
| Output | Intermediate code | Object module |
| Feature | One-pass assembler | Two-pass assembler | Load-and-go assembler |
|---|---|---|---|
| Source scans | One | Two | Depends on design; can be one-pass or two-pass |
| Forward references | Patched later using fix-up list | Resolved in pass 2 | Must be resolved before execution |
| Object file | May produce object file | Produces object file | Avoids writing/reading object file |
| Main advantage | Faster, less storage | Cleaner handling of forward references | Saves object-program I/O overhead |
| Table | What it stores | Typical structure |
|---|---|---|
| Symbol table | Label/symbol, value/address, attributes | Array or hash table |
| Literal table | Literal value, assigned address | Array/list |
| Mnemonic/opcode table | Mnemonic, opcode, format, length | Permanent array/table |
| Pseudo-op table | Directive and handling routine | Permanent table |
| Forward Reference Table | Unresolved symbol references | Linked list |
| Pool table | Start of each literal pool | Array/list |
| Segment register table | Segment-register base information | Usually array indexed by register number |
Common assembler directives:
| Directive | Meaning |
|---|---|
START | Set the starting address of the assembly |
END | End of source program; may specify entry point |
ORIGIN | Change the location counter to a given address |
EQU | Assign a symbol a value equal to an expression |
DS | Define storage; reserve bytes without initial values |
DC | Define constant; reserve and initialize data |
LTORG | Assign addresses to pending literals |
EXTRN / ENTRY | Declare external symbols / define entry points for linking |
Revision summary
- Pass 1 builds the symbol table; pass 2 uses it.
- Pass 1 also separates opcode and operand fields and builds intermediate code, but it does not build final machine code.
- A label’s value is the LC before the labelled item is processed.
ORIGINchanges LC;DSreserves bytes;EQUgives a value without allocating storage;LTORGallocates literal addresses.- Literals go to the literal table, not the symbol table.
- Forward references are handled through the FRT/fix-up list.
- One assembly instruction normally equals one machine instruction.
- An assembler is hardware/instruction-set dependent.
- The mnemonic table is permanent; the symbol table is per assembly.
- A load-and-go assembler avoids object-file write/read, whether it is one-pass or two-pass.
- In the general language-processor model, the terminal table has one entry per terminal symbol, while the identifier table stores identifier attributes.
- If a forward-referenced symbol is used in an
EQU, its final value may need pass 2.
Previous-year exam insights
- Direct questions on “which activity is in the first pass” usually expect build symbol table, construct intermediate code, and separate mnemonic/operand fields; an option saying “construct machine code” is the wrong activity.
- Questions on the symbol table often test that it stores label and value/address, and that it is generated in pass 1.
- Assertion–reason items may contrast a load-and-go assembler with one-pass/two-pass designs; remember that avoiding object-program I/O is true, but the pass design is a separate fact.
- Match-the-list items mix symbol table, literal table, mnemonic table, FRT, pool table, and segment register table; learn the data structure and stored content of each.
- Numeric questions use LC arithmetic with instructions,
DS,ORIGIN, literals, andLTORG; draw a small timeline and advance LC item by item. - Relative-addressing questions require you to check whether the PC points to the current instruction or the next instruction; the usual textbook convention is next instruction.
- Trap options confuse “literal table” with “symbol table” and “mnemonic table” with “pseudo-op table”; read what is being stored, not just the name.
- For assembly-statement classification, remember imperative, declarative, and assembler-directive statements; one imperative statement represents one machine instruction.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- Define an assembler and state why it is machine-dependent.
- List exactly what pass 1 and pass 2 of a two-pass assembler do.
- Build a symbol table from a short assembly program using LC values.
- Handle
START,ORIGIN,DS,EQU,LTORG, andENDin location-counter calculations. - Explain how forward references are recorded and resolved.
- Distinguish symbol table, literal table, mnemonic table, pseudo-op table, FRT, and pool table.
- Calculate the address assigned to a literal at
LTORG. - Compute PC-relative displacement with the correct PC convention.
- Identify duplicate-label and undefined-symbol errors and the pass in which they are detected.
- Answer match-the-list and assertion–reason questions on assembler tables and pass activities.
Attempt the sub-topic test first, then the subject test.