Exp 6

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Experiment No.

06

Aim: Write a Program to generate a symbol table of Pass-I of 2 pass Assembler.


Code:
mot =
[['Mnemonic_Opcode','Binary_Opcode','Instruction_Length','Instruction_F
ormat'],['L','4A','4','001'],['A','5A','4','001'],['ST','1E','4','100']
]
pot = [['Pseudo_Opcode','Address of routine to process Pseudo Op (
Predefined
)'],['START','P1START'],['USING','P1USING'],['END','P1END'],['DC','P1DC
'],['DS','P1DS']]
machineOpInstructions=['L','A','ST']
pseudoOpInstructions=['START','END','USING','DC','DS']

symbol_table = [['Symbol','Address']]

print("MOT table : \n\n")


for row in mot:
for column in row:
print(column,end='\t\t')
print("\n\n")
print("\n\n POT Table :\n\n\n")
for row in pot:
for column in row:
print(column,end='\t\t\t')
print("\n\n")

#FileOpening & storing it line by line


f = open('/content/drive/MyDrive/SampleCodeForPass1.txt','r')
lines = f.read().splitlines()

labels,registers,machineOps,pseudoOps=[],[],[],[]
k=0
for line in lines:
k+=1
words=line.split(' ')
if(words[0]!=''):
if(words[1]=='DS'):
symbol_table.append([words[0],program_counter])
if(words[1]!='START' and words[1]!='DS'):
word_sep_by_apostrophe=words[2].split('\'')
symbol_table.append([words[0],program_counter])
program_counter+=4
if('START' in words):
symbol_table.append([words[0],'0'])
program_counter = int(words.pop())
for word in words:
word_sep_by_comma=word.split(',')
if(word in machineOpInstructions):
print("Machine Op Instruction " + word + " Found at line :
" + str(k))
for row in mot:
if(word in row):
program_counter+=int(row[2])
break
machineOps.append(word)
if(word in pseudoOpInstructions):
print("Pseudo Op Instruction " + word + " Found at line :
" + str(k))
pseudoOps.append(word)
if(word=='START'):
pass
elif(word=='END'):
print("\n\n\nPass 1 Completed.............")
print("Program Counter resetted to zero")
program_counter=0
else:
# program_counter+=1
pass
if(word.isalpha() and word not in machineOpInstructions and
word not in pseudoOpInstructions):
print("Label " + word + " found at line : " + str(k))
labels.append(word)
if(len(word_sep_by_comma) > 1):
for w in word_sep_by_comma:
if(w.isnumeric()):
print("Register R" + w + " found at line : " + str(k))
if("R"+w not in registers):
registers.append("R" + w)
if(w.isalpha()):
print("Label " + w + " found at line : " + str(k))
print("Program Counter : \t" + str(program_counter) + "\n\n")

print("\n\nLabels : ",labels)
print("\n\nRegisters : ",registers)
print("\n\nMachine Ops : ",machineOps)
print("\n\nPseudo Ops : ",pseudoOps)
print("\n\n\nSymbol table : ")
for row in symbol_table:
for c in row:
print(c,end='\t\t')
print("\n\n")
Output:
MOT table :
Mnemonic_Opcode Binary_Opcode Instruction_Length
Instruction_Format

L 4A 4 001

A 5A 4 001

ST 1E 4 100

POT Table :
Pseudo_Opcode Address of routine to process Pseudo Op ( Predefined )

START P1START

USING P1USING

END P1END

DC P1DC

DS P1DS

Label JOHN found at line : 1


Pseudo Op Instruction START Found at line : 1
Program Counter : 0

Pseudo Op Instruction USING Found at line : 2


Register R15 found at line : 2
Program Counter : 0

Machine Op Instruction L Found at line : 3


Register R1 found at line : 3
Label FOUR found at line : 3
Program Counter : 4

Machine Op Instruction A Found at line : 4


Register R1 found at line : 4
Label FIVE found at line : 4
Program Counter : 8

Machine Op Instruction ST Found at line : 5


Register R1 found at line : 5
Label TEMP found at line : 5
Program Counter : 12

Label FOUR found at line : 6


Pseudo Op Instruction DC Found at line : 6
Program Counter : 16

Label FIVE found at line : 7


Pseudo Op Instruction DC Found at line : 7
Program Counter : 20

Label TEMP found at line : 8


Pseudo Op Instruction DS Found at line : 8
Program Counter : 20

Pseudo Op Instruction END Found at line : 9


Pass 1 Completed.............
Program Counter resetted to zero
Program Counter : 0

Labels : ['JOHN', 'FOUR', 'FIVE', 'TEMP']

Registers : ['R15', 'R1']

Machine Ops : ['L', 'A', 'ST']

Pseudo Ops : ['START', 'USING', 'DC', 'DC', 'DS', 'END']

Symbol table :
Symbol Address

JOHN 0

FOUR 12

FIVE 16

TEMP 20

You might also like