You are on page 1of 5

Don Bosco Institute of Technology, Kurla(W)

Department of Computer Engineering


CSL602: System Software Lab
2020-21

Experiment Experiment Name


No.
3 Design and Implement Pass 2 of 2 Pass Assembler

Student Name: Reena Kale


Roll No : 23

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.

Theory & Algorithm : Assembler is a program for converting instructions written in


low-level assembly code into relocatable machine code and
generating along information for the loader. It generates
instructions by evaluating the mnemonics (symbols) in the
operation field and finding the value of symbols and literals to
produce machine code. Now, if an assembler does all this work
in one scan then it is called a single pass assembler, otherwise
if it does in multiple scans then called multiple pass assembler.

Here assembler divide these tasks in two passes:


Pass-2:
1. Generate object code by converting symbolic op-code into
respective numeric op-code
2. Generate data for literals and look for values of symbols

Algorithm for Pass 2 assembler:


begin
read a line;
if op code = START then ;;
write header record;
while op code != END do ;; or EOF
begin
search OPTAB for the op code;
if found
if the operand is a symbol then
replace it with an address using SYMTAB;
assemble the object code;
else if is a defined directive convert it to object code;
add object code to the text;
read next line;
end write End record to the text;
output text;
End

FLOWCHART:

Program Code: def literaladdress(number):


for literal in literals:
literal = literal.split(" ")
# print(literal)
if(literal[0]==number):
return literal[2]

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/

Course In-charge: Ditty Varghese

You might also like