C Programming • Introduction to Computer, Programming & algorithms
C Programming / Assembler

Assembler

Notes 1 Introduction to Computer, Programming & algorithms

An assembler is a language translator that converts a program written in assembly language into machine language or object code that the computer can use for execution.

Notes

What is an Assembler?

An assembler is a language translator that converts a program written in assembly language into machine language or object code that the computer can use for execution.

Assembly language uses symbolic instructions called mnemonics, such as MOV, ADD, SUB, instead of writing binary instructions directly.

💡 In Simple Words

An assembler acts like a translator: it changes easy-to-read assembly instructions into machine-level instructions understood by the processor.

Why is an Assembler Needed?

A computer processor directly executes machine-level instructions. Writing programs directly in binary is difficult for humans. Assembly language makes those instructions easier to write using symbolic names, and the assembler converts them into machine code.

  • Makes low-level programming easier than writing binary code.
  • Converts mnemonics into machine instructions.
  • Handles symbols, labels and addresses.
  • Produces object code for later linking and execution.

Input and Output of an Assembler

Item Description
Input Assembly language source file, usually with an .asm extension.
Processing Translates mnemonics, resolves symbols and addresses.
Output Object code, commonly stored in an .obj or .o file.

How an Assembler Works

The assembler reads the assembly language program, identifies instructions and symbols, determines addresses, and produces machine or object code.

ASSEMBLY LANGUAGE ↓ ASSEMBLER ↓ OBJECT CODE ↓ LINKER ↓ EXECUTABLE

One-to-One Translation

Assembly language instructions generally have a one-to-one relationship with machine instructions. This means one assembly instruction corresponds closely to one machine instruction.

Assembly Instruction Machine Representation
MOV AX, 5 B8 05 00
📌 Remember

The assembler does not convert a whole high-level program like a compiler. Its main job is to translate assembly language into machine/object code.

Two-Pass Assembler

A two-pass assembler processes the source program in two passes. This allows it to identify symbols and addresses before generating the final object code.

PASS 1 ↓ Build Symbol Table ↓ PASS 2 ↓ Generate Object Code

Pass 1 — Symbol Table Construction

In the first pass, the assembler scans the source program and identifies labels and symbols. It records their corresponding addresses in a symbol table.

  • Reads the source program.
  • Maintains addresses for instructions and data.
  • Identifies labels and symbols.
  • Builds the symbol table.

Pass 2 — Object Code Generation

In the second pass, the assembler uses the information collected during Pass 1 to generate the actual object or machine code.

  • Reads the source program again.
  • Replaces mnemonics with their machine opcodes.
  • Resolves symbolic addresses.
  • Generates object code.

Pass 1 vs Pass 2

Pass 1 Pass 2
Scans the source program. Scans the source program again.
Builds the symbol table. Generates object code.
Assigns/records addresses. Resolves symbols and addresses.
Prepares information required for code generation. Produces machine/object instructions.

Symbols and Labels

Assembly programs often use symbolic names, such as labels, instead of directly writing numeric memory addresses. The assembler keeps track of these names and their associated addresses.

💡 In Simple Words

A label is a name given to an instruction or memory location. The assembler later finds its actual address.

Example of Assembly Code

MOV AX, 5 ADD AX, 3

The assembler translates these assembly instructions into their corresponding machine/object representation.

Examples of Assemblers

Assembler Use
NASM Assembler commonly used for x86 and x86-64 programming.
MASM Microsoft Macro Assembler for x86-family assembly.
TASM Borland Turbo Assembler.

Advantages of an Assembler

  • Provides a readable symbolic form of machine instructions.
  • Allows close control over processor operations.
  • Produces efficient low-level code.
  • Useful for system programming and hardware-related programming.
  • Allows use of labels and symbolic addresses.

Limitations of Assembly Language and Assembler-Based Programming

  • Assembly programs are harder to write than high-level programs.
  • Assembly language is generally hardware dependent.
  • Programs are less portable between processor architectures.
  • Debugging and maintenance can be more difficult.

Quick Revision

Concept Remember This
Assembler Converts assembly language into machine/object code.
Input Assembly source file, usually .asm.
Output Object code, usually .obj or .o.
Pass 1 Builds the symbol table and records addresses.
Pass 2 Generates object code and resolves symbols.
Translation Assembly instruction → Machine/Object instruction.
Examples NASM, MASM, TASM.
🧠 Easy Revision Trick

Pass 1 = Symbols & Addresses
Pass 2 = Machine/Object Code

Important Exam Questions

Short Answer Questions

  1. What is an assembler?
  2. What is the main function of an assembler?
  3. What is the input of an assembler?
  4. What is the output of an assembler?
  5. What is a mnemonic in assembly language?
  6. What is a symbol or label?
  7. What is a two-pass assembler?
  8. What is performed during Pass 1?
  9. What is performed during Pass 2?
  10. What is a symbol table?
  11. Name any three assemblers.

Long Answer Questions

  1. Define an assembler and explain its working.
  2. Explain the need for an assembler in computer programming.
  3. Explain the two-pass assembler with the functions of Pass 1 and Pass 2.
  4. Explain how symbols, labels and addresses are handled by an assembler.
  5. Differentiate between Pass 1 and Pass 2 of an assembler.
  6. Explain the input and output of an assembler with a suitable example.
🎥 Recommended Learning

After reading the topic, watch this Hindi explanation of the two-pass assembler and its working.

▶ Watch: 2-Pass Assembler — Hindi Lecture

📝 Handwritten Notes

A short handwritten-style revision sheet covering assembler, symbol tables, Pass 1 and Pass 2 will be provided here.

🧠 Mind Map

Use the mind map for quick revision of assembler working, two-pass processing, symbols and object-code generation.