-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_assembler.py
422 lines (317 loc) · 12.2 KB
/
simple_assembler.py
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# HARJEET SINGH YADAV
# IIIT - DELHI
# ROLL NO - 2020561
# BRANCH - CSAI
# uncomment these 2 lines for taking a file as an input through console
# import sys
# code = sys.stdin.read().splitlines()
# uncomment these 2 lines if you want to read your own ( custom )input file
with open('test_case1.txt') as f: # here test_case1.txt is an input file with assembly code
code = f.read().splitlines()
# ACTUAL CODE STARTS FORM HERE
#dictionary to map registers with their code
RegAddress = {
"R0":"000",
"R1":"001",
"R2":"010",
"R3":"011",
"R4":"100",
"R5":"101",
"R6":"110",
"FLAGS":"111"
}
# dictionary to map instructions with their opcode and type
operations = {
"add":["00000","A"],
"sub":["00001","A"],
"mov1":["00010","B"],
"mov2":["00011","C"],
"ld":["00100","D"],
"st":["00101","D"],
"mul":["00110","A"],
"div":["00111","C"],
"rs":["01000","B"],
"ls":["01001","B"],
"xor":["01010","A"],
"or":["01011","A"],
"and":["01100","A"],
"not":["01101","C"],
"cmp":["01110","C"],
"jmp":["01111","E"],
"jlt":["10000","E"],
"jgt":["10001","E"],
"je":["10010","E"],
"hlt":["10011","F"]
}
operations_symbol = ["add","sub","mov","ld","st","mul","div","rs","ls",
"xor","or","and","not","cmp","jmp","jlt","jgt","je","hlt"]
registers = [ "R0", "R1" , "R2" , "R3" , "R4" , "R5" , "R6"]
registers_flag= [ "R0", "R1" , "R2" , "R3" , "R4" , "R5" , "R6" , "FLAGS"]
labels=["hlt"]
variables=[]
error=False
#**********************************************************************************************************************************************
#***THIS IS ERROR HANDLING PART***
# this piece of code will detect any syntax error in the input assembly code and display the error
#*************************** THIS FUCTION CHECKS ERROR IN IMMEDIATE VALUES ********************************************#
def check_immediate(a):
global error
try:
n = int(a[1:])
if(n<0 or n>255):
print("line no" , line_no , n ,"is not in range [0, 255] ", sep=' ')
error=True
except:
print("line no" , line_no , "invalid immediate value", sep=' ')
error=True
#**************************** THIS FUNCTION HANDLES ALL ERROR CASES OF TYPE A *******************************************#
def type_A(value):
global error
if(len(value)!=4):
print("line no" , line_no , " wrong syntax used for", value[0],"instruction",sep=' ' )
error=True
return
for i in range(1,len(value)):
if(value[i]=="FLAGS"):
print("line no" , line_no, " invalid use of flags ",sep=' ')
error=True
elif(value[i] not in registers):
print("line no" , line_no ,'(',value[i],')', "is invalid register name ",sep=' ')
error=True
#*************************** THIS FUNCTION HANDLES ALL ERROR CASES OF TYPE B ****************************************#
def type_B(value):
global error
if(len(value)!=3):
print("line no" , line_no , " wrong syntax used for", value[0],"instruction",sep=' ' )
error=True
return
if(value[1]=="FLAGS"):
print("line no" , line_no, " invalid use of flags ",sep=' ')
error=True
elif(value[1] not in registers):
print("line no" , line_no ,'(',value[1],')', "is invalid register name ",sep=' ')
error=True
a = value[2]
if(a[0]!="$"):
print("line no", line_no , "use of " , a[0] , "is invalid" , sep=' ')
error=True
else:
check_immediate(a)
#*************************** THIS FUNCTION HANDLES ALL ERROR CASES OF TYPE C ****************************************#
def type_C(value):
global error
if(len(value)!=3):
print("line no" , line_no , " wrong syntax used for type C instruction",sep=' ' )
error=True
return
if(value[1]=="FLAGS"):
print("line no" , line_no, " invalid use of flags ",sep=' ')
error=True
elif(value[1] not in registers):
print("line no" , line_no ,'(',value[1],')', "is invalid register name ",sep=' ')
error=True
if(value[0]=="mov2" and value[2] not in registers_flag):
print("line no" , line_no , " invalid register or flag name ",sep=' ')
error=True
elif value[0]!="mov2" and value[2] not in registers:
print("line no",line_no,"invalid register name",sep=' ')
error=True
#*************************** THIS FUNCTION HANDLES ALL ERROR CASES OF TYPE D *****************************************#
def type_D(value):
global error
if(len(value)!=3):
print("line no" , line_no , " wrong syntax used for", value[0],"instruction",sep=' ' )
error=True
return
if(value[2] in labels):
print("line no", line_no , "labels cannot be used inplace of variables", sep=' ')
error=True
elif(value[2] not in variables):
print("line no" , line_no , '(',value[2] ,')'," is undefined variable",sep=' ')
error=True
#*************************** THIS FUNCTION HANDLES ALL ERROR CASES OF TYPE E *******************************************#
def type_E(value):
global error
if(len(value)!=2):
print("line no" , line_no , " wrong syntax used for", value[0],"instruction",sep=' ' )
error=True
return
if(value[1] in variables):
print("line no", line_no , "variables cannot be used inplace of labels", sep=' ')
error=True
elif(value[1] not in labels):
print("line no" , line_no , '(',value[1],')' ," is undefined label ",sep=' ')
error=True
#*************************** THIS FUNCTION HANDLES ALL ERROR CASES OF TYPE F *********************************************#
def type_F(value):
if(line_no!=len(code)):
print("line no", line_no , "hlt must be at the end",sep=' ')
error=True
elif(len(value)!=1):
print("line no" , line_no , " wrong syntax used for", value[0],"instruction",sep=' ' )
error=True
#***************************** THIS IS HELPER FUNCTION TO HANDLE CASES OF VARIABLES ***************************************#
def handle_variables(value):
global error
global flag
if(value[0]!="var"):
flag=1
if value[0]=="var" and len(value)!=2:
print("line no",line_no,"invalid syntax",sep=' ')
error=True
return
if value[0]=="var":
if(flag==1):
print("line no", line_no , "variable not decalared in the beginning of code ",sep=' ')
error=True
if(value[1] in variables):
print("line no", line_no , "mulitiple declaration of variable " , value[1] , sep=' ')
error = True
else:
variables.append(value[1])
#*********************************THIS HELPER FUNCTION TO HANDLE CASES OF LABELS ********************************#
def handle_labels(value):
global error
if(value[0][-1]==":"):
if(value[0][0:-1] in labels):
print("line no", line_no , "multiple definations of label " ,'(', value[0],')',sep=' ')
error=True
else:
labels.append(value[0][0:-1])
#**********************************THIS IS HELPER FUNCTION TO HANDLE HALT *********************************************#
def handle_hlt(value):
global error
if(len(value)==2 ):
if value[1]!="hlt":
print("line no" ,line_no +1 ," no hlt instruction at the end ", sep=' ')
error=True
elif(value[0]!="hlt"):
print("line no" ,line_no +1 ," no hlt instruction at the end ", sep=' ')
error=True
#HANDLING ALL CASES OF VARIABLES
line_no =0
flag=0
for line in code:
line_no+=1
if(len(line)==0):
continue
value = list(line.split())
handle_variables(value)
# HANDLING ALL CASES OF LABELES
line_no=0
for line in code:
line_no+=1
if(len(line)==0):
continue
value = list(line.split())
handle_labels(value)
# HANDLING ALL CASES OF NORMAL INSTRUCTIONS
line_no=0
for line in code:
line_no+=1
if(len(line)==0):
continue
value = list(line.split())
if line_no==len(code):
handle_hlt(value)
if(value[0]=="var"):
continue
if(value[0][0:-1] in labels):
value.pop(0)
if(len(value)==0):
print("line no", line_no , "invalid defnation of labels",sep=' ')
error=True
continue
if(value[0] not in operations_symbol):
print("line no",line_no , '(',value[0],')'," is invalid instruction name ", sep=' ')
error=True
continue
if(value[0]=="mov" and len(value)>=2):
c = value[2][0]
if(65<=ord(c)<=90 or 97<=ord(c)<=122):
value[0]="mov2"
else:
value[0]="mov1"
if (operations[value[0]][1] == "A"):
type_A(value)
elif (operations[value[0]][1] == "C"):
type_C(value)
elif (operations[value[0]][1] == "B"):
type_B(value)
elif (operations[value[0]][1] == "D"):
type_D(value)
elif (operations[value[0]][1] == "E"):
type_E(value)
elif (operations[value[0]][1] == "F"):
type_F(value)
else:
print("line no",line_no,"invalid syntax",sep=' ')
error=True
# this is printing the binary code part
#********************************************************************************************************************************************
# THIS IS ASSEMBLER THIS WILL RUN ONLY WHEN THERE ARE NO ERRORS IN THE ASSEMBLY CODE
labels={}
variables={}
t=1
address=-1
if(error==True):
exit()
#*********************************THIS LOOP WILL STORE THE ADDRESS OF ALL VARIABLES IN DICTIONARY*********************
for line in code:
if len(line)==0:
continue
value = list(line.split())
if(value[0] in operations_symbol):
address+=1
if value[0]=="hlt":
labels[value[0]+":"]=address
if(value[0][-1]==":"):
address+=1
labels[value[0]]=address
#********************************* THIS LOOP WILL STORE THE ADDRESS OF ALL LABELS IN DICTIONARY ***********************
for line in code:
if(len(line)==0):
continue
value = list(line.split())
if value[0]=="var" and len(value)==2:
variables[value[1]]=t+address
t+=1
#********************************* THIS IS MAIN LOOP TO COVERT ASSEMBLY INTO BINARY CODE *******************************
for line in code:
if(len(line)==0):
continue
value = list(line.split())
if( len(value)>1 and value[0] in labels and value[1] in operations_symbol):
value.pop(0)
if (value[0] in operations_symbol):
if(value[0]=="mov" ):
if(value[2][0]=="$"):
value[0]="mov1"
else:
value[0]="mov2"
if (operations[value[0]][1] == "B"):
a = value[1]
b = value[2][1:]
b1 = bin(int(b))[2:]
s = operations[value[0]][0] + RegAddress[a] + (8-len(b1))*"0" + b1
elif (operations[value[0]][1] == "A"):
a = value[1]
b = value[2]
c = value[3]
s = operations[value[0]][0] + "00" + RegAddress[a] + RegAddress[b] + RegAddress[c]
elif (operations[value[0]][1] == "C"):
a = value[1]
b = value[2]
s = operations[value[0]][0] + "00000" + RegAddress[a] + RegAddress[b]
elif (operations[value[0]][1] == "D"):
a = value[1]
b = bin(variables[value[2]])[2:]
s = operations[value[0]][0] + RegAddress[a] + (8 - len(b)) * "0" + b
elif (operations[value[0]][1] == "E"):
a=value[1]
b=bin(labels[a+":"])[2:]
s=operations[value[0]][0] + "000" + (8 - len(b)) * "0" + b
elif (operations[value[0]][1] == "F"):
s = operations[value[0]][0] + "00000000000"
print(s)
# ********************************THE END*********************************************************************