Experiment No. Experiment Name
Experiment No. Experiment Name
Experiment No. Experiment Name
Objective:
Students will be able to learn various file handling operations
Students will be able to produce the target code using the intermediate
representation generated in Pass 1.
Students will be able to implement the working of Pass 2 of 2 pass Assembler
Task to be performed:
A. For any Assembly input for a hypothetical machine, implement pass 2 by using
the intermediate code generated as input to pass 2 program which was generated
as output in pass 1.
FLOWCHART:
def symboladdress(number):
for symbol in symbols:
symbol = symbol.split(" ")
if(symbol[0]==number):
return symbol[2]
def LTORG(list,ltorgcount):
ltorgcount += 1
literallist = []
for literal in literals:
literal = literal.split(" ")
literallist.append(literal)
for literal in literals:
literal = literal.split(" ")
if(str(ltorgcount)==literal[0]):
list.append(["(00)","(00)","(00"+str(literal[1][2])+")"
])
if(int(literal[2])+1==int(literallist[ltorgcount][2])):
ltorgcount += 1
return list
input = open('pass1.txt',"r")
data = input.read()
instructions = data.replace('(',"")
instructions = instructions.replace(')',"")
instructions = instructions.replace(',',"")
instructions = instructions.split('\n')
littab = open('littab.txt',"r")
data = littab.read()
literals = data.split('\n')
ltorgcount=0
symtab = open('symtab.txt',"r")
data = symtab.read()
symbols = data.split('\n')
pass2=[]
for instruction in instructions:
list =[]
instruction = instruction.split(' ')
# print(instruction)
iterable = iter(instruction)
for opcode in iterable:
if(opcode != "" ):
# print(opcode)
if(opcode == 'AD01' ):
next(iterable)
continue
elif('IS' in opcode):
list.append("("+str(opcode[2:])+")")
continue
elif(opcode.isnumeric() and len(opcode)==1):
list.append("(0"+str(opcode)+")")
continue
elif('DS' in opcode or 'DL' in opcode):
next(iterable)
next(iterable)
continue
elif('C' in opcode):
list.append("("+str(opcode[1:])+")")
continue
elif('L' in opcode):
list.append("("+literaladdress(opcode[2])+")")
elif('S' in opcode):
list.append("("+symboladdress(opcode[2])+")")
elif('AD03'== opcode):
list.append("(00)")
list.append("(00)")
elif('AD05' == opcode):
list = LTORG(list,ltorgcount)
try:
if(list[0][0]=="("):
# print(list)
pass2.append(list)
elif(len(list)>1):
for element in list:
pass2.append(element)
except:
pass2.append(list)
print("Pass 2 Code\n")
for instruction in pass2:
for opcode in instruction:
print(opcode+" ",end=" ")
print("\n",end=" ")
Input to the Program: (AD,01) (C,100)
(DL,01) (C,10)
(IS,04) (01) (S,2)
(IS,05) (02) (L,1)
(IS,01) (01) (L,2)
(IS,02) (02) (L,3)
(DL,01) (C,20)
(AD,03) (C,300)
(AD,05)
(IS,04) (01) (S,3)
(IS,04) (03) (S,4)
(IS,01) (02) (L,4)
(DL,02) (C,5)
(DL,01) (C,10)
(AD,02)
Literal Table:
1 ='1' 300
2 ='2' 301
3 ='3' 302
4 ='4' 312
Symbol Table:
1 A 100 10
2 B 105 20
3 NUM 306 5
4 LOOP 311 10
Pool Table:
Sr No | Literal Pointer
1 #1
2 #4
Output Screenshot:
Outcome of the In this experiment, we have successfully implemented pass 2
Experiment: of a 2 pass assembler in python using the intermediate
representation generated in Pass 1. We have also learned
various file handling operations.
References used: https://www.geeksforgeeks.org/introduction-of-assembler/