0% found this document useful (0 votes)
30 views10 pages

SS Module 2

The document explains the Two-Pass Assembler, which converts assembly language into machine code by first creating a symbol table in Pass 1 and then generating object code in Pass 2. It details the processes involved, including handling forward references and the use of assembler directives. The final output consists of an object program format with header, text, and end records.

Uploaded by

himarr168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

SS Module 2

The document explains the Two-Pass Assembler, which converts assembly language into machine code by first creating a symbol table in Pass 1 and then generating object code in Pass 2. It details the processes involved, including handling forward references and the use of assembler directives. The final output consists of an object program format with header, text, and end records.

Uploaded by

himarr168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

---

📘 System Software – Module 2: Two-Pass Assembler (Full Explanation)


---

🔶 What is an Assembler?
An assembler is a program that converts assembly language into machine code (binary
instructions).
Example:

LDA VALUE

gets converted to:

00 1020

Assembler does this using two types:

One-Pass Assembler

Two-Pass Assembler ← This is what we focus on in Module 2.

---

🌀 Why Do We Need a Two-Pass Assembler?


Because of forward references.

Imagine you use a label like LOOP in your program before defining it:

JUMP LOOP
...
LOOP ADD A

In Pass 1, assembler doesn’t yet know where LOOP is.


So it first scans to build a symbol table, then uses that in Pass 2 to generate proper machine
code.
---

✅ Pass 1 – Symbol Table Creation


In Pass 1, the assembler:

1. Reads each line of code.

2. Assigns addresses to labels.

3. Keeps track of current address using Location Counter (LOCCTR).

4. Builds the SYMTAB (Symbol Table):

Label → Address.

It does not generate machine code in this step.

🎯 Example:
COPY START 1000
FIRST STL RETADR
CLOOP JSUB RDREC

COPY → start address = 1000

FIRST → assigned 1003

CLOOP → assigned 1006

SYMTAB will store: | Label | Address | |--------|---------| | FIRST | 1003 | | CLOOP | 1006 |

---

✅ Pass 2 – Object Code Generation


Now, assembler knows all label addresses.
In Pass 2, it:

1. Reads the code again.

2. Uses:

SYMTAB for label addresses

OPTAB for instruction opcodes (e.g., LDA = 00)

3. Generates machine code (object code).

4. Creates:

Object File (for machine to run)

Listing File (for programmers to check)

---

🔧 Assembler Directives (Pseudo-Operations)


These are not instructions for the CPU but commands for the assembler.

Directive​ Meaning

START​Start of the program


END​ End of the program
BYTE​ Define 1-byte constant (e.g., C'Z')
WORD​Define 3-byte integer
RESB​ Reserve bytes
RESW​Reserve words (3 bytes per word)

🧠 Example:
VAL1 WORD 5 ; Reserves 3 bytes and stores 5
TEMP RESW 1 ; Reserves 3 bytes (empty)

---

📑 SYMTAB and OPTAB


These are lookup tables used by assembler:

🔹 SYMTAB (Symbol Table)


Created in Pass 1.

Stores label → address pairs.

🔹 OPTAB (Operation Table)


Built-in table with opcodes.

Stores mnemonic → opcode pairs.

Example: | Mnemonic | Opcode | |----------|--------| | LDA | 00 | | STA | 0C | | ADD |


18 |

---

📄 Intermediate File & Listing File


Intermediate File: Output of Pass 1

Contains labels, opcodes, operands with addresses.

Listing File: Final human-readable version

Shows original code + generated object code

Used for debugging.


---

💡 Handling Forward References


When a label like LOOP is used before it’s declared, assembler:

Keeps a placeholder in Pass 1.

Fills the correct address in Pass 2 using SYMTAB.

This is why a single pass is not enough.

---

📦 Object Program Format


After Pass 2, assembler generates the object program, which has 3 main parts:

🟩 Header Record (H)


Program name, start address, program length

H COPY 001000 00107A

🟨 Text Record (T)


Actual object code instructions

T 001000 1E141033281030…

🟥 End Record (E)


Shows starting execution address

E 001000
---

🧠 Final Summary:
Two-pass assembler is needed to handle forward references.

Pass 1 creates SYMTAB, tracks addresses.

Pass 2 uses SYMTAB + OPTAB to make object code.

Output: Object file with H, T, E records.

---

🪩STORY MODE
---

📘 System Software – Module 2: Assemblers Continued


🏰 Chapter 1: The Journey of the Two-Pass Assembler
In the land of Assembly, there was a wise program called the Two-Pass Assembler.
Why two passes? Because it was careful and clever.

Imagine you’re writing a treasure map (program), but some places (labels) don’t exist yet. The
assembler walks twice:

🔄 Pass 1 – The Map Maker


Walks line by line.

Finds all the labels and gives them addresses.

Builds the SYMTAB — a map of symbols.

> Example: If it sees LOOP:, it notes that LOOP is at address 1020.


🔄 Pass 2 – The Code Crafter
Comes back with the full map.

Converts each instruction into machine code using:

SYMTAB

OPTAB (like a spellbook of operation codes)

---

🔧 Chapter 2: What Happens Inside Each Pass


🧭 Pass 1:
Initializes the Location Counter (LOCCTR).

Checks each line for:

Labels → Add to SYMTAB

Instructions → Increase LOCCTR

Directives like START, RESW, BYTE → handled specially

🔑 Pass 2:
Uses the SYMTAB and OPTAB.

Generates Object Code.

Also creates:

Listing File (for humans to read)

Object File (for machine to run)


---

📚 Chapter 3: The Directives – Royal Commands


In Assembly, not every word is an instruction. Some are directives — they give orders to the
assembler, not to the machine.

Directive​ Meaning

START​Start of program
END​ End of program
BYTE​ Store 1 byte of constant (e.g., char or hex)
WORD​Store 3 bytes
RESB​ Reserve bytes
RESW​Reserve words (3 bytes each)

> Example:
NUM WORD 5 → Reserves 3 bytes and stores value 5
TEMP RESW 1 → Reserves 3 bytes of space, leaves it empty

---

🧠 Chapter 4: What’s Inside SYMTAB and OPTAB


📒 SYMTAB (Symbol Table):
Stores labels with addresses.

Label​ Address

LOOP​ 1010
VALUE​1020

📘 OPTAB (Operation Table):


Stores instructions and their codes.
Mnemonic​ Opcode

LDA​ 00
STA​ 0C
ADD​ 18

---

🖨️ Chapter 5: Intermediate File and Listing File


After Pass 1, the assembler creates an Intermediate File:

It includes line number, label, opcode, operand.

Helps Pass 2 generate final object code.

The Listing File is like a nice printed report — for humans to check.

---

⚔️ Chapter 6: Handling Forward References


What if the program uses a label before it’s defined?

J LOOP
...
LOOP ADD B

In Pass 1, assembler notes: “Hmm, I’ve seen LOOP but don’t know the address yet.”
It fills it in later, once LOOP is defined.

---

📦 Chapter 7: Final Output – Object Program Format


The final machine code comes out in object program format:
Header Record (H): Program name and start address.

Text Record (T): Actual instructions.

End Record (E): Tells where to begin execution.

> Sample:

H COPY 001000 00107A


T 001000 1E 141033 281030 …
E 001000

---

🎓 Moral of the Story


The Two-Pass Assembler is not just a translator — it's a detective, mapmaker, and magician all
in one.

It handles labels, errors, addresses, and code — making sure the final object file is perfect for
the machine.

---

You might also like