-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.py
More file actions
345 lines (250 loc) · 7.47 KB
/
assembler.py
File metadata and controls
345 lines (250 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#returns no of bytes (in decimal)
def cal_bytes(mnemonic,operand):
found=0
noOfBytes=-1
if(mnemonic=="RESW" or mnemonic=="RESB" or mnemonic=="WORD" or mnemonic=="BYTE"):
found=1
if(mnemonic=="RESW"):
operand = int(operand)
noOfBytes = operand*3
elif(mnemonic=="RESB"):
operand = int(operand)
noOfBytes = operand
elif(mnemonic=="WORD"):
noOfBytes = 3
elif(mnemonic=="BYTE"):
length=len(operand)-3
if(operand[0]=='X'):
if(length%2==0): noOfBytes=length/2
else: noOfBytes = (length/2)+1
elif(operand[0]=='C'):
noOfBytes=length
if(found==0):
opcode = open("OPCODE.txt","r") #opcode file
for opLine in opcode:
op= opLine.split('\t')
if(mnemonic==op[0]):
found=1
noOfBytes=op[1]
break
opcode.close()
if(found==0):
noOfBytes=-1
noOfBytes = int(noOfBytes)
return noOfBytes
#function to check if the symbol already exists or not
def notExists(symbol):
symFileR = open("SYMTAB.txt","r")
found=0
for aline in symFileR:
line = aline
line = line[:-1]
spLine = line.split('\t')
if(spLine[0]==symbol):
found=1
break
symFileR.close()
if(found==1):
return 0
else:
return 1
#function returns the opcode of the operand
def retOpcode(mnemonic):
opcodeF = open("OPCODE.txt","r") #opcode file
found=0
for opLine in opcodeF:
line = opLine
line = line[:-1]
op= line.split('\t')
if(mnemonic==op[0]):
found=1
opcode=op[2]
break
opcodeF.close()
if(found==1):
return opcode
else:
return -1
#function returns address of label
def retAddress(label):
symFileR = open("SYMTAB.txt","r") #to read addresses corresponding to labels
found=0
for aline in symFileR:
line = aline
line = line[:-1]
spLine = line.split('\t')
if(spLine[0]==label):
found=1
tAdd = spLine[1]
break
symFileR.close()
if(found==1):
return tAdd
else:
return -1
#function that returns ascii code of a character
def retAscii(char):
ascii = open("ASCII.txt","r")
found=0
for aline in ascii:
line = aline
line = line[:-1]
lineSp = line.split('\t')
if(lineSp[1]==str(char)):
found=1
asciiCode = lineSp[0]
break
ascii.close()
if(found==1):
return asciiCode
else:
return -1
##################################################################################################
#The program execution begins from here.....
###################################################################################################
fname=input("Enter the name of the file: ")
#print (a)
asmFile = open(fname,"r") #assembly code file
aCode = open("aCODE.txt","w") #to store the assembly file with addresses
symFile = open("SYMTAB.txt","w") #to store addresses corresponding to labels
start = asmFile.readline(); #assumption: first line of the code is the start statement
while(start[0]=='.'):
start = asmFile.readline();
start = start[:-1]
firstLine = start.split('\t')
#print (firstLine)
Addf = firstLine[2] #value of address in hex
Add = int(Addf,16) #converted the value in decimal
Add1= Addf # string containing hex value
#convert the first address into decimal
#add correct no of bytes everytime (in decimal)
#then convert back to hexadecimal
print("\n")
for iline in asmFile:
line = iline
line = line[:-1]
lineSp = line.split('\t')
if(line[0]!='.'):
if(lineSp[1]=="END"):
break
noOfBytes=0
if(len(lineSp)==3):
noOfBytes = cal_bytes(lineSp[1],lineSp[2]) #returns no of bytes in decimal
else:
noOfBytes = cal_bytes(lineSp[1],0)
if(noOfBytes==-1):
error= "Error: Invalid mnemonic " + lineSp[1]
print(error)
input()
exit(0)
# ENTRY INTO SYMTAB
if(lineSp[0]!=''): #if label is not empty
if(notExists(lineSp[0])): #checks if the label is already present or not
symbol = lineSp[0] + "\t" + Add1
#print(symbol)
symFile.write(symbol)
symFile.write("\n")
symFile.flush()
else:
error= "Error: " + lineSp[0] + " - Multiple declaration "
print(error)
input()
exit(0)
#WRITING INSTRUCTIONS ALONG WITH ASSIGNED ADDRESSES
writeLine= Add1 + "\t" + line #check if \n is a part of line #Add1 is hex
#print(writeLine) #PRINTS ON-SCREEN
aCode.write(writeLine) #writes into file
aCode.write("\n")
aCode.flush()
#CALCULATION OF NEXT ADDRESS
Add = Add + noOfBytes #performs decimal addition
Add1=str(format(Add,'04X')) #converts to hex before storing
symFile.close()
asmFile.close()
aCode.close()
#get=input()
#intermediate file with appropriate addresses and SYMTAB have been created
################################################################################
aCodeI = open("aCODE.txt","r") #assembly code file with addresses
objCode = open("objCODE.txt","w") #to store the assembly file with object code
obj = open("assembler.o","w") #to store only the object code
aCodeI.seek(0)
for aline in aCodeI:
line = aline
line = line[:-1]
lineSp = line.split('\t')
address=lineSp[0]
label = lineSp[1]
mnemonic = lineSp[2]
if(len(lineSp)==4):
operand = lineSp[3]
if(mnemonic!="RESW" and mnemonic!="RESB"):
if(mnemonic=="BYTE"): #OBJECT CODING for mnemonic: BYTE
arr = operand.split('\'')
if(arr[0]=="X"):
objLine = arr[1]
elif(arr[0]=="C"):
chars = list(arr[1])
objLine = ""
for char in chars: #for each x in C'xxx'
asciiCode = retAscii(char)
if(asciiCode==-1):
print("Error: Invalid character in BYTE")
input()
exit(0)
objLine = objLine + asciiCode
elif(mnemonic=="WORD"): #OBJECT CODING for mnemonic: WORD
operand = int(operand)
objLine = str(format(operand,'06X'))
elif(mnemonic=="RSUB"): #OBJECT CODING for mnemonic: RSUB
opcode = retOpcode(mnemonic)
if(opcode==-1):
print("Error: Opcode for RSUB could not be found")
input()
exit(0)
objLine = opcode + "0000"
else: #OBJECT CODING for all other mnemonics
opcode = retOpcode(mnemonic)
if(opcode==-1):
error="Error: Opcode for " + mnemonic + " could not be found"
print(error)
input()
exit(0)
operandSp = operand.split(',')
length = len(operandSp)
targetAdd = retAddress(operandSp[0])
if(targetAdd==-1):
error="Error: Target address of " + operandSp[0] + " could not be found"
print(error)
input()
exit(0)
if(length==2 and operandSp[1]=="X"):
string=targetAdd
part1 = string[:1]
part2 = string[1:]
part1 = int(part1)
part1 = part1 + 8
part1 = str(format(part1,'01X'))
targetAdd = part1+part2
objLine = opcode + targetAdd
if(mnemonic=="RSUB"):
writeLine = line + "\t\t" + objLine
else:
writeLine = line + "\t" + objLine
objCode.write(writeLine) #object code along with instructions
objCode.write("\n")
obj.write(objLine) #only object code file
obj.write("\n")
else:
objCode.write(line)
objCode.write("\n")
aCodeI.close()
objCode.close()
obj.close()
#object code written in appropriate files
objCode = open("objCODE.txt","r")
for aline in objCode:
line = aline[:-1]
print(line)
#print the file with addresses and object code
get = input()