Assignment 4
Name- Pranav Mandare
Roll No- 321045
PRN-22110054
Div- TY A
Aim: WAP to generate a Literal table of a two-pass Assembler
for the given Assembly language source code.
Objective:
1) To parse and analyze a given assembly language source code to
extract and list all the literals in a structured table format.
2) Literal Table is crucial for the second pass of the assembler
where the actual translation of assembly language instructions to
machine code takes place.
3) The literals are constant values defined within the code which the
assembler must recognize and help in appropriately allocating
memory.
Theory:
A two-pass assembler processes the source code in two phases. In the
first pass, it reads through the code to identify and build tables for
symbols and literals (constants), setting the groundwork for the actual
translation process. The Literal Table is a key component generated
during this phase, containing all the literals used in the source code
along with their assigned addresses.
Literals are specific values or constants used in the assembly code, like
numbers or character constants, which need to be stored at some
memory location. The Literal Table keeps track of these constants,
allowing the assembler to efficiently allocate memory and replace the
literal references in the code with actual memory addresses in the
second pass.
The Literal Table typically includes columns for:
Literal: The actual value or constant.
Address: The allocated memory address for the literal.
Code:
#literal table\
def getClass(operator):
clas = 0
for i in EMOT:
if(i[0] == operator):
clas = i[1]
return clas
EMOT = [['STOP', 1, 0], ['ADD', 1, 1], ['SUB', 1, 2], ['MULT', 1, 3],
['MOVER', 1, 4], ['MOVEM', 1, 5], ['COMP', 1, 6], ['BC', 1, 7], ['DIV', 1, 8],
['READ', 1, 9], ['PRINT', 1, 10], ['START', 3, 1], ['END', 3, 2], ['ORIGIN', 3,
3], ['EQU', 3, 4], ['LTORG', 3, 5], ['DS', 2, 1], ['DC', 2, 2], ['AREG', 4, 1],
['BREG', 4, 2], ['CREG', 4, 3], ['DREG', 4, 4]]
file = open("ASSEMBLY CODE.txt", 'r')
lines = file.readlines()
tokens = []
for line in lines:
tokens.append(line.split())
n = len(tokens)
lc = int(tokens[0][-1])
#print(lc)
lcList = []
for i in tokens:
lcList.append(lc)
length = len(tokens)
#print("length= ", length)
if(length == 4):
operator = i[1]
clas = getClas(operator)
if(clas == 1):
lc+=1
elif(clas == 2):
if(operator == 'DS'):
incr = int(i[-1])
#print("incr= ", incr)
lc+=incr
else:
lc+=1
#lcList.append(lc)
else:
if('DS' in i or 'DC' in i):
operator = i[1]
else:
operator = i[0]
clas = getClass(operator)
#print(operator, clas)
if(clas == 1):
lc+=1
elif(clas == 2):
if(operator == 'DS'):
incr = int(i[-1])
#print("incr= ", incr)
lc+=incr
else:
lc+=1
elif(clas == 3):
pass
#lcList.append(lc)
#print(lcList)
#print(len(lcList))
lt=[]
for i in tokens:
if(i[-1][0] == '='):
lt.append([i[-1], -1])
#print(lt)
end = lcList[-1]
for i in lt:
i[-1] = end
end+=1
for i in lt:
print(i)
Output:
Conclusion:
The program successfully generates a Literal Table from the provided
assembly language source code, identifying all literals and assigning
unique memory addresses to each. Understanding and implementing
such foundational elements of assemblers are crucial for developing
more efficient and effective assembly language programming and
compilation tools.