You are on page 1of 5

LPCC Assignment –2

Name : Yuvraj Sharma


Gr No : 21910694
Roll No: 324054
Batch : D3

Problem Statement
Design suitable data structures and implement first pass of a two-pass Macro
processor.

Theory:
• 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 in operation field and
find the value of symbol and literals to produce machine code
• A Macro instruction is the notational convenience for the programmer
• For every occurrence of macro, the whole macro body or macro block of
statements gets expanded in the main source code
• Thus, Macro instructions make writing code more convenient
• Macro represents a group of commonly used statements in the source
programming language
• Macro Processor replaces each macro instruction with the corresponding
group of source language statements. This is known as the expansion of
macros
• Using Macro instructions programmer can leave the mechanical details to be
handled by the macro processor
• Macro Processor designs are not directly related to the computer
architecture on which it runs
• Macro Processor involves definition, invocation, and expansion

Algorithm:
• The very first step in the 1st pass of macro processor is import the .asm file
containing the macro code
• This code will be first checked for comments if any. Comments encountered
will be eliminated for further processing
• The next step to be executed in the macro processor is generating
intermediate code for the given assembly code
• The next part is to expand the macros to create Macro definition table (MDT)
which is an important element in the 2-pass macro processor
• On basis of MDT, the Macro name table is to be generated which has the
index on macros in the code and the macro names. Also, the number of
arguments is recorded in the table
• The macro name table and macro definition table contribute to formal v/s
positional parameters and actual v/s positional parameter for every macro

Code:

#comment remover OF .ASM file


f = open('Sample2.asm','r',encoding='utf-8')
code = []
for line in f:
code.append(line.split())
print(line)

#creating data structure for pass1


MDT = []
uid = 0
class MNTable:
def __init__(self,name,para,start):
global uid
self.uId = uid
uid+=1
self.name = name
self.noPara = para
self.startInd = start
self.endInd = 0

def assignEnd(unid,ind):
for i in MNT:
if i.uId == unid:
i.endInd =ind
break

MNT = []
ForPos = []
def addForPosParameters(paraList,mac):
i=0
table=[]
while(i<paraList.__len__()):
posi = "#"+str(i+1)
if(i!=paraList.__len__()-1):
paraList[i] = paraList[i][:-1]
table.append((paraList[i],posi))
i+=1
mac.ForPos = table
ForPos.append([mac.name,table])

def addToDef(line,m):
k=0
while(k<line.__len__()):
for var in m.ForPos:
if line[k].find(var[0]) != -1:
line[k] = line[k].replace(var[0],var[1])
k+=1
MDT.append(line)

definationMode = False
pointer =1
MNT =[]
MDT=[]
ForPos = []
uid=0

current = ''#currently whichmacro are we working with


for line in code:
if line[0]=="MACRO":
paraLn = line.__len__() - 2
m = MNTable(line[1],paraLn,pointer)
if(paraLn>0):
addForPosParameters(line[2:],m)
MNT.append(m)
definationMode = True
current = line[1]

elif definationMode:
if paraLn>0:
addToDef(line,m)
else:
MDT.append(line)
pointer +=1

if line[0]=="MEND" and definationMode:


definationMode= False
assignEnd(m.uId,pointer-1)
current = ''

print("Displaying the MNT contents:")


print("Name \tNo. of Parameters \tStart Index \tEnd Index")
for i in MNT:
print(i.name,'\t\t',i.noPara,'\t\t',i.startInd,'\t\t',i.endInd)

print("\n\nDisplaying MDT contents")


for i in MDT:
for j in i:
print(j,end=' ')
print()
print("\n\n")
for i in ForPos:
print("Displaying Formal vs Positional for: ",i[0])
for j in i[1]:
print(j[0],'\t',j[1])
print("\n")

Input File:

Output:
Conclusion:
1. The first pass of 2 pass macro processor generates some important data
structures like MDT, MNT and intermediate code. Also, the actual v/s
positional parameter list and the formal v/s positional parameter list is
obtained.
2. The 1st pass also is significant for expansion of macros to execute the
instructions in the code one by one serially.
3. Also, the comments are eliminated in the pass.

You might also like