0% found this document useful (0 votes)
15 views9 pages

LPCC Assignment 6

Uploaded by

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

LPCC Assignment 6

Uploaded by

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

Assignment 6

Name- Pranav Mandare


Roll No- 321045
PRN-22110054
Div- TY A

Aim: WAP to Generate Symbol table, Literal table, Pool table &
Intermediate code of a two-pass Assembler for the given
Assembly language source code.

Objective:
1) The objective of this project is to design and implement a two-
pass assembler that processes given assembly language source
code to generate the Symbol table, Literal table, Pool table, and
Intermediate code.
2) The assembler will first analyze the source code to identify all
symbols (variables and labels), literals, and maintain pools of
literals used. In the second pass, it will convert the assembly
instructions into machine code or intermediate representation,
resolving symbolic addresses and literal values.

Theory:
An assembler is a program that translates assembly language into
machine code. It provides the necessary facilities for programming in a
machine or assembly language.

Two-pass Assembler:
First Pass: Scans the source code to find the label definitions and builds
the symbol table. It also identifies literals and builds the literal and pool
tables. The aim is to collect information needed for the second pass.
Second Pass: Uses the tables generated in the first pass to translate
assembly instructions into machine code or an intermediate form,
resolving addresses using the symbol and literal tables.
Symbol Table: A data structure used by the assembler to keep track of
identifiers (symbols) and their attributes, such as their memory
addresses or values.

Literal Table: Maintains information about literals, which are constant


values used in the program. Each entry typically includes a literal's
value and its assigned memory address.

Pool Table: A pool table keeps track of the starting index of literals in
the literal table that belong to the same pool. A new pool is created
whenever a LTORG (Literal Organize) instruction is encountered,
signaling the end of a block of literals.

Code:
import os
LC=0 #location counter initialization

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)}
file=open("ASSEMBLY CODE.txt")
ifp=open("inter_code.txt","a")
ifp.truncate(0)
REG={'AREG':1,'BREG':2,'CREG':3,'DREG':4}
lit=open("literals.txt","a+")
lit.truncate(0)
tmp=open("tmp.txt","a+")
tmp.truncate(0)
symtab={}
pooltab=[]
words=[]

def littab():
print("literal table:")
lit.seek(0,0)
for x in lit:
print(x)

def pooltab2():
global pooltab
print("Pool Table:")
print(pooltab)

def symbol():
global symtab
print("Symbol Table:")
print(symtab)

def END():
global LC
pool=0
z=0
ifp.write("\t(AD,02)\n")
lit.seek(0,0)
for x in lit:
if "" in x:
pool+=1
if pool==1:
pooltab.append(z)
y=x.split()
tmp.write(y[0]+"\t"+str(LC)+"\n")
LC+=1
else:
tmp.write(x)
z+=1
lit.truncate(0)
tmp.seek(0,0)
for x in tmp:
lit.write(x)
tmp.truncate(0)
def LTORG():
global LC
pool=0
z=0
lit.seek(0,0)
x=lit.readlines()
i=0
while(i<len(x)):
f=[]
if("" in x[i]):
j=0
pool+=1
if pool==1:
pooltab.append(z)
while(x[i][j]!="'"):
j+=1
j+=1
while(x[i][j]!="'"):
f.append(x[i][j])
j+=1
if(i!=len(x)-1):
ifp.write("\t(AD,05)\t(DL,02)(C,"+str(f[0])+")\n")
y=x[i].split()
tmp.write(y[0]+"\t"+str(LC)+"\n")
LC+=1
ifp.write(str(LC))
else:
ifp.write("\t(AD,05)\t(DL,02)(C,"+str(f[0])+")\n")
y=x[i].split()
tmp.write(y[0]+"\t"+str(LC)+"\n")
LC+=1
else:
tmp.write(x[i])
z+=1
i+=1
lit.truncate(0)
tmp.seek(0,0)
for x in tmp:
lit.write(x)
tmp.truncate(0)

def ORIGIN(addr):
global LC
ifp.write("\t(AD,03)\t(C,"+str(addr)+")\n")
LC =int(addr)

def DS(size):
global LC
ifp.write("\t(DL,01)\t(C,"+size+")\n")
LC=LC+int(size)

def DC(value):
global LC
ifp.write("\t(DL,02)\t(C,"+value+")\n")
LC+=1

def OTHERS(mnemonic,k):
global words
global mnemonics
global symtab
global LC,symindex
z=mnemonics[mnemonic]
ifp.write("\t("+z[1]+","+z[0]+")\t")
i=0
y=z[-1]
#print("y="+str(y))
for i in range(1,y+1):
words[k+i]=words[k+i].replace(",","")
if(words[k+i] in REG.keys()):
ifp.write("(RG,"+str(REG[words[k+i]])+")")
elif("=" in words[k+i]):
#print(words[k+i])
lit.seek(0,2)
lit.write(words[k+i]+"\t**\n")
lit.seek(0,0)
x=lit.readlines()
#print(len(x))
ifp.write("(L,"+str(len(x))+")")
else:
#print(words,symtab)
if(words[k+i] not in symtab.keys()):
symtab[words[k+i]]=("",symindex)
ifp.write("(S,"+str(symindex)+")")
symindex+=1
else:
w=symtab[words[k+i]]
ifp.write("(S,"+str(w[-1])+")")
#print(symtab)
ifp.write("\n")
LC+=1

def detect_mn(k):
global words,LC
if(words[k]=="START"):
LC=int(words[1])
ifp.write("\t(AD,01)\t(C,"+str(LC)+')\n')
elif(words[k]=='END'):
END()
elif(words[k]=="LTORG"):
LTORG()
elif(words[k]=="ORIGIN"):
ORIGIN(words[k+1])
elif(words[k]=="DS"):
DS(words[k+1])
elif(words[k]=="DC"):
DC(words[k+1])
else:
OTHERS(words[k],k)
littab()
pooltab2()
symbol()
symindex=0
for line in file:
#print(line)
words=line.split()
#print(words)
if(LC>0):
ifp.write(str(LC))
print("LC=",LC)
print(line)
print(words)
k=0

if words[0] in mnemonics.keys():
print("Mnemonic:",words[0])
val=mnemonics[words[0]]
k=0
detect_mn(k)
else:
print("Label:",words[0],"Mnemonic:",words[1])
if words[k] not in symtab.keys():
symtab[words[k]]=(LC,symindex)
#ifp.write("\t(S,"+str(symindex)+")\t")
symindex+=1
symbol()
else:
#print(words)
x=symtab[words[k]]
if x[0]=="":
print("yes")
symtab[words[k]]=(LC,x[1])
#ifp.write("\t(S,"+str(symindex)+")\t")
symbol()
k=1
detect_mn(k)
#print(symtab)
#print(pooltab)
ifp.close()
lit.close()
tmp.close()
sym=open("SymTab.txt","a+")
sym.truncate(0)
for x in symtab:
sym.write(x+"\t"+str(symtab[x][0])+"\n")
sym.close()
pool=open("PoolTab.txt","a+")
pool.truncate(0)
for x in pooltab:
pool.write(str(x)+"\n")
pool.close()
if os.path.exists("tmp.txt")==True:
os.remove("tmp.txt")

Output:
Intermediate code:

Symbol table:

Literal Table:
Conclusion:
This program successfully demonstrates the functionality of a two-pass
assembler by generating the Symbol table, Literal table, Pool table,
and Intermediate code for a given assembly language source code.
This project highlights the importance of these tables in the assembly
language translation process and provides a foundational
understanding of how assemblers function.

You might also like