0% found this document useful (0 votes)
6 views3 pages

Gauri Pss2

Uploaded by

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

Gauri Pss2

Uploaded by

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

Name- GAURI HARALKAR

Roll_NO.-13162
Batch-A3
Group A Assignments-1 PASS-II
--------------------------------------------------Pass-
II_Program-----------------------------------------------------
import os

# Mnemonics and Registers as provided in the question


mnemonics = {
'STOP': ('00', 'IS', 0), 'ADD': ('01', 'IS', 2), 'SUB': ('02', 'IS', 2), 'MUL':
('03', 'IS', 2),
'MOVER': ('04', 'IS', 2), 'MOVEM': ('05', 'IS', 2), 'COMP': ('06', 'IS', 2),
'BC': ('07', 'IS', 2),
'DIV': ('08', 'IS', 2), 'READ': ('09', 'IS', 1), 'PRINT': ('10', 'IS', 1),
'LTORG': ('05', 'AD', 0),
'ORIGIN': ('03', 'AD', 1), 'START': ('01', 'AD', 1), 'EQU': ('04', 'AD', 2),
'DS': ('01', 'DL', 1),
'DC': ('02', 'DL', 1), 'END': ('AD', 0)
}

REGISTERS = {'AREG': 1, 'BREG': 2, 'CREG': 3, 'DREG': 4}

def read_intermediate_code():
inter_code = []
with open("inter_code.txt", "r") as f:
for line in f:
inter_code.append(line.strip())
return inter_code

def read_literals():
literals = {}
with open("literals.txt", "r") as f:
for line in f:
parts = line.strip().split("\t")
literals[parts[0]] = parts[1]
return literals

def read_symtab():
symtab = {}
with open("SymTab.txt", "r") as f:
for line in f:
parts = line.strip().split("\t")
symtab[parts[0]] = parts[1]
return symtab

def read_pooltab():
pooltab = []
with open("PoolTab.txt", "r") as f:
for line in f:
pooltab.append(int(line.strip()))
return pooltab

def generate_machine_code(inter_code, literals, symtab, pooltab):


machine_code = []

for line in inter_code:


parts = line.split()
if parts[0].startswith('('):
machine_code.append(line) # Directly copy non-instruction lines (like
literals)
else:
mnemonic = parts[1]
if mnemonic == 'IS': # Instruction Statement
opcode = parts[2]
operand_code = ""
for operand in parts[3:]:
if operand.startswith('('):
operand_code += f" {operand}"
elif operand.startswith('S,'):
symbol = operand.split(',')[1]
if symbol in symtab:
operand_code += f" ({symtab[symbol]})"
else:
operand_code += f" {operand}"
elif operand.startswith('L,'):
literal_index = int(operand.split(',')[1])
if literal_index in pooltab:
operand_code += f" ({literals[str(literal_index)]})"
else:
operand_code += f" {operand}"
elif operand.startswith('RG'):
operand_code += f" {operand}"
elif operand.startswith('C,'):
operand_code += f" {operand}"
machine_code.append(f"{opcode}{operand_code}")
elif mnemonic == 'AD': # Assembler Directive (Handle as per
requirement)
machine_code.append(line)
elif mnemonic == 'DL': # Declaration Statement (Handle as per
requirement)
machine_code.append(line)

return machine_code

def write_machine_code(machine_code):
with open("machine_code.txt", "w") as f:
for line in machine_code:
f.write(f"{line}\n")

def pass2_assemble():
inter_code = read_intermediate_code()
literals = read_literals()
symtab = read_symtab()
pooltab = read_pooltab()

machine_code = generate_machine_code(inter_code, literals, symtab, pooltab)

write_machine_code(machine_code)

if __name__ == "__main__":
pass2_assemble()
---------------------------------------------------------
inter_code.txt------------------------------------------------
(AD,01) (C,200)
(DL,02) (C,10)
(IS,04) (RG,1)(S,1)
(IS,05) (RG,2)(L,1)
(IS,01) (RG,1)(L,2)
(IS,02) (RG,2)(L,3)
(DL,02) (C,20)
(AD,03) (C,300)
(AD,05) (DL,02)(C,1)
(AD,05) (DL,02)(C,2)
(AD,05) (DL,02)(C,1)
(IS,04) (RG,1)(S,2)
(IS,04) (RG,3)(S,3)
(IS,01) (RG,2)(L,4)
(DL,01) (C,5)
(DL,02) (C,10)
(AD,02)
------------------------------------------------------
literas.txt-----------------------------------------------------
='1' 301
='2' 302
='1' 303
='1' 313
-----------------------------------------------------
machine_code.txt-------------------------------------------------
03 01 202 ; MOVER AREG, B
04 02 001 ; MOVEM BREG, =1
05 01 002 ; ADD AREG, =2
06 02 001 ; SUB BREG, =1
03 01 300 ; MOVER AREG, NUM
03 03 305 ; MOVER CREG, LOOP
05 02 001 ; ADD BREG, =1
---------------------------------------------------
PoolTab.txt--------------------------------------------------------
0
3
---------------------------------------------------
SymTab.txt-----------------------------------------------------------
A 200
B 205
NUM 306
LOOP 311
---------------------------------------------------
input.txt-----------------------------------------------------------
START 200
A DC 10
MOVER AREG, B
MOVEM BREG, ='1'
ADD AREG, ='2'
SUB BREG, ='1'
B DC 20
ORIGIN 300
LTORG
MOVER AREG, NUM
MOVER CREG, LOOP
ADD BREG, ='1'
NUM DS 5
LOOP DC 10
LTORG
END
-----------------------------------------------------------------------------------
------------------------------------

You might also like