0% found this document useful (0 votes)
3 views5 pages

Department of Computer Engineering: Rushikesh Nikam

The document outlines an experiment for designing and implementing the first pass of a two-pass assembler for a general machine. It includes a Python program that processes source code, builds a symbol table, and generates intermediate code based on various assembly language instructions. The experiment concludes with the results of the symbol table and intermediate code generated from a sample source code.

Uploaded by

rushikesh.nhitm
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)
3 views5 pages

Department of Computer Engineering: Rushikesh Nikam

The document outlines an experiment for designing and implementing the first pass of a two-pass assembler for a general machine. It includes a Python program that processes source code, builds a symbol table, and generates intermediate code based on various assembly language instructions. The experiment concludes with the results of the symbol table and intermediate code generated from a sample source code.

Uploaded by

rushikesh.nhitm
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/ 5

Department of Computer Engineering

Academic Year: 2024-25 Name of Student: Aashish Radheshyam Gupta

Semester: VI Student ID: 22102146

Class / Branch: T.E Comp Date of Performance: 10/01/2025

Subject: SPCC Date of Submission: 10/01/2025

Name of Instructor: Prof. Rushikesh Nikam

Experiment No.1

Aim: Design and implementation of pass1 of two-pass assembler for general machine.

Problem Statement: Design and implementation of pass1 of two-pass assembler for general machine.

Program:

# Two-Pass Assembler: Pass 1

def pass1(source_code):

# Initialization

symtab = {} # Symbol Table

locctr = 0 # Location Counter

intermediate_code = [] # For storing intermediate results

start_address = 0

# OpCode Table (OPTAB)

optab = {

"LDA": 3, "STA": 3, "ADD": 3, "SUB": 3,

"MUL": 3, "DIV": 3, "COMP": 3, "LDX": 3,

"STX": 3, "JMP": 3, "JSUB": 3, "RSUB": 3,

# Pass 1 logic

for line in source_code:

line = line.strip()
if not line or line.startswith(';'): # Skip comments or empty lines

continue

parts = line.split()

if len(parts) == 3: # Label, Mnemonic, Operand

label, mnemonic, operand = parts

elif len(parts) == 2: # Mnemonic, Operand

label = ""

mnemonic, operand = parts

else: # Only Mnemonic

label = ""

mnemonic = parts[0]

operand = ""

# Add label to SYMTAB

if label:

if label in symtab:

print(f"Error: Duplicate label {label}")

return None

symtab[label] = locctr

# Handle START directive

if mnemonic == "START":

start_address = int(operand)

locctr = start_address

intermediate_code.append((locctr, line))
continue

# Add current instruction to intermediate code

intermediate_code.append((locctr, line))

# Handle END directive

if mnemonic == "END":

break

# Update LOCCTR for assembler directives

if mnemonic == "WORD":

locctr += 3

elif mnemonic == "RESW":

locctr += 3 * int(operand)

elif mnemonic == "RESB":

locctr += int(operand)

elif mnemonic == "BYTE":

# Handle character ('C'X') or hex ('X'...)

if operand.startswith("C'") or operand.startswith("c'"):

locctr += len(operand[2:-1])

elif operand.startswith("X'") or operand.startswith("x'"):

locctr += len(operand[2:-1]) // 2

elif mnemonic in optab: # Check if mnemonic is in OPTAB

locctr += optab[mnemonic]

else:

print(f"Error: Invalid operation {mnemonic}")


return None

# Return the symbol table and intermediate code

return symtab, intermediate_code, start_address

# Example source code

source_code = [

"COPY START 1000",

"FIRST LDA FIVE",

" STA ALPHA",

"FIVE WORD 5",

"ALPHA RESW 1",

" END FIRST"

# Run Pass 1

symtab, intermediate_code, start_address = pass1(source_code)

# Print the results

print("Symbol Table:")

for symbol, address in symtab.items():

print(f"{symbol} -> {address}")

print("\nIntermediate Code:")

for address, code in intermediate_code:

print(f"{address} : {code}")
print(f"\nStart Address: {start_address}")

Output:

Conclusion : Hence, we studied the two pass assembler.

You might also like