1. Gyanastra
  2. Operating Systems
  3. UGC NET: System Software
  4. Assemblers
Free lesson, no account needed 13 min read 28 practice questions after it
Asked in past papers7 times
  • UGC NET2022201520142008200720052004

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, JMP that become machine instructions; declarative statements such as DS and DC that reserve or define data; and assembler directives or pseudo-operations such as START, END, ORIGIN, EQU, LTORG, EXTRN, ENTRY that 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 EQU value) 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; LTORG assigns 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 EQU receives 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.

  1. 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.
  2. 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.
  3. 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 DS or DC, determine how many bytes it reserves or defines.
    If it is a directive, execute its pass-1 action.
  4. For START, set the initial LC. For ORIGIN, change LC to the specified address. For EQU, assign the expression value to the symbol. For END, stop pass 1. For LTORG, assign addresses to pending literals.
  5. After processing an item, advance the LC by the item’s size. Thus a label gets the address of the item that follows it.
  6. 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 next LTORG or at the end.
  7. Emit intermediate code. This contains opcode information and operand placeholders; it is not yet final machine code.

In pass 2:

  1. Read each intermediate-code line.
  2. Replace symbolic operands by their values from the symbol table.
  3. Patch every forward reference using its fix-up list or FRT entry.
  4. Compute displacements for relative addressing, base-register addressing, and similar modes.
  5. Convert opcode and operands into binary machine code.
  6. Assign addresses to literals at pools and complete object records.
  7. Report any symbol still undefined at the end.
Figure. Pass 1 separates fields, updates the location counter, builds symbol and literal tables, and creates intermediate code with unresolved symbols. Pass 2 resolves those symbols and generates machine code and the object program.

Formulas

  • Let LCLC be the current location counter, s(item)s(item) the number of bytes occupied by the current item, AA the address of the current instruction, LL its length, SS the value of the target symbol, and DD the displacement. Use the convention that the program counter points to the next instruction for PC-relative addressing.

  • Next location counter:
    LCnext=LC+s(item)LC_{\text{next}} = LC + s(item)

  • Value assigned to a label:
    label value=LC before processing the labelled item\text{label value} = LC \text{ before processing the labelled item}

  • Address assigned to a literal at LTORG:
    literal address=LC at the LTORG\text{literal address} = LC \text{ at the LTORG}

  • PC-relative displacement:
    D=S−(A+L)D = S - (A + L)

  • Base-register displacement, when RbR_b is the base-register value:
    D=S−RbD = S - R_b

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 LC=200LC=200.
L1 gets value 200; the 2-byte instruction advances LCLC to 202.
L2 gets value 202; DS 4 advances LCLC to 206.
L3 gets value 206; the 2-byte instruction advances LCLC to 208.
Final symbol table: L1=200L1=200, L2=202L2=202, L3=206L3=206; 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 LC=103LC=103.
B1=103; after DS 4, LC=107LC=107.
ORIGIN 500 sets LC=500LC=500; bytes 107 through 499 are not filled by this code.
C1=500; after 2 bytes final LC=502LC=502.

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; LC=4LC=4.
I2=4; LC=8LC=8.
LTORG assigns the literal address 8; literal size 1 advances LCLC to 9.
I3=9; LC=13LC=13.
I4=13; LC=17LC=17.
I5=17; final LC=21LC=21.
Literal table: ='5' at address 8. Symbol table: I1=0I1=0, I2=4I2=4, I3=9I3=9, I4=13I4=13, I5=17I5=17.

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,
D=130−(100+2)=130−102=28.D = 130 - (100+2) = 130-102 = 28.
The displacement is 28. If a question explicitly says the PC points to the current instruction, use 130−100=30130-100=30; always follow the stated convention.

Example 5: match-the-list style.
Match List-I with List-II.

List-IList-II
A. Symbol tablei. mnemonic to opcode and format
B. Literal tableii. unresolved forward references
C. Mnemonic tableiii. label and its value
D. Forward Reference Tableiv. 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

PointPass 1Pass 2
Main purposeDefine symbols and addresses; build tablesGenerate machine code and object program
Location counterUsed and updatedUsually not used to define symbols
Symbol tableBuiltUsed
Literal tableBuilt; literals enteredLiteral addresses used and pools completed
Forward referencesRecorded in FRT/fix-up listResolved and patched
Machine codeNot generatedGenerated
OutputIntermediate codeObject module
FeatureOne-pass assemblerTwo-pass assemblerLoad-and-go assembler
Source scansOneTwoDepends on design; can be one-pass or two-pass
Forward referencesPatched later using fix-up listResolved in pass 2Must be resolved before execution
Object fileMay produce object fileProduces object fileAvoids writing/reading object file
Main advantageFaster, less storageCleaner handling of forward referencesSaves object-program I/O overhead
TableWhat it storesTypical structure
Symbol tableLabel/symbol, value/address, attributesArray or hash table
Literal tableLiteral value, assigned addressArray/list
Mnemonic/opcode tableMnemonic, opcode, format, lengthPermanent array/table
Pseudo-op tableDirective and handling routinePermanent table
Forward Reference TableUnresolved symbol referencesLinked list
Pool tableStart of each literal poolArray/list
Segment register tableSegment-register base informationUsually array indexed by register number

Common assembler directives:

DirectiveMeaning
STARTSet the starting address of the assembly
ENDEnd of source program; may specify entry point
ORIGINChange the location counter to a given address
EQUAssign a symbol a value equal to an expression
DSDefine storage; reserve bytes without initial values
DCDefine constant; reserve and initialize data
LTORGAssign addresses to pending literals
EXTRN / ENTRYDeclare 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.
  • ORIGIN changes LC; DS reserves bytes; EQU gives a value without allocating storage; LTORG allocates 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, and LTORG; 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:

  1. Define an assembler and state why it is machine-dependent.
  2. List exactly what pass 1 and pass 2 of a two-pass assembler do.
  3. Build a symbol table from a short assembly program using LC values.
  4. Handle START, ORIGIN, DS, EQU, LTORG, and END in location-counter calculations.
  5. Explain how forward references are recorded and resolved.
  6. Distinguish symbol table, literal table, mnemonic table, pseudo-op table, FRT, and pool table.
  7. Calculate the address assigned to a literal at LTORG.
  8. Compute PC-relative displacement with the correct PC convention.
  9. Identify duplicate-label and undefined-symbol errors and the pass in which they are detected.
  10. Answer match-the-list and assertion–reason questions on assembler tables and pass activities.

Attempt the sub-topic test first, then the subject test.

Pages 1–2 of 2