-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleAssembler.py
511 lines (395 loc) · 13 KB
/
SimpleAssembler.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import fileinput
# opcode={'add':'10000','sub':'10001','mov':'10010','ld':'10100','st':'10101','mul':'10110',
# 'div':'10111','rs':'11000','ls':'11001','xor':'11010','or':'11011','and':'11100','not':'11101',
# 'cmp':'11110','jmp':'11111','jlt':'01100','jgt':'01101','je':'01111'}
hlt={"hlt":"01010"}
reg={"R0":"000","R1":"001","R2":"010","R3":"011","R4":"100","R5":"101","R6":"110"}
opcode_A={"add":"10000","sub":"10001","mul":"10110","xor":"11010","or":"11011","and":"11100",}
opcode_B={"mov":"10010","ls":"11001",'rs':'11000'}
opcode_C={"mov":"10011","div":"10111","not":"11101","cmp":"11110",}
opcode_D={"ld":"10100","st":"10101"}
opcode_E={"jmp":"11111","jlt":"01100","jgt":"01101","je":"01111"}
Flags={"FLAGS":"111"}
counter="00000000"
# mov r1 256
# check b or d
# check_a={mov,mov,ls,}
# check_d={ld,st}
convert1=""
var={}
label={}
errorflag=0
finalflag=0
immediateflag=0
# empty line
# whitespace in start
# varible only at start
# error typo of name in reg op
# error with line number (print any one)
# error for use of flag in other operations
# interchanged misuse of var & lab
# do we need to print all erors or single error or save text file for error
def var_form(name):
global var
global counter
sum = str(bin(1 + int(counter, 2)).replace("0b",""))
while(len(sum)<8):
sum="0"+sum
var[str(name)]=str(sum)
counter=str(sum)
def label_form(name):
global label
global counter
sum = str(bin(1 + int(counter, 2)).replace("0b",""))
while(len(sum)<8):
sum="0"+sum
label[str(name)]=str(sum)
counter=str(sum)
type_A=[opcode_A,reg,reg,reg]
type_B=[opcode_B,reg]
type_C=[opcode_C,reg,reg]
type_D=[opcode_D,reg]
type_E=[opcode_E]
type_F=[hlt]
def checkA(data,line_num):
global finalflag
global errorflag
global convert1
# print("IN A")
arglen=len(data)
if (arglen!=4):
# print("Here")
return False
checkstr=""
truestr="ORRR"
for i in range(0,len(data)):
if data[i] in type_A[0]:
checkstr+="O"
elif data[i] in type_A[2]:
checkstr+="R"
elif data[i]=="FLAGS": # flag error udated
print(f"Line {line_num}:Misuse of FLAGS Error")
finalflag=1
errorflag=1
return False
if checkstr==truestr:
converted=""
for i in data:
if i in opcode_A:
converted+=opcode_A[i]+"00"
elif i in reg:
converted+=reg[i]
convert1=converted
return True
else:
return False
def checkB(data,line_num):
global finalflag
global errorflag
global immediateflag
global convert1
if(len(data)!=3):
return False
truestr="ORI"
checkstr=""
for i in range(0,len(data)):
if data[i] in type_B[0]:
checkstr+="O"
elif data[i] in type_B[1]:
checkstr+="R"
elif (data[i][0] =="$" and data[i][1:len(data[i])].isdigit()):
checkstr+="I"
elif data[i]=="FLAGS": # flag error udated
print(f"Line {line_num}:Misuse of FLAGS Error")
finalflag=1
errorflag=1
return False
if(checkstr==truestr):
converted=""
for i in data:
if i in opcode_B:
converted+=opcode_B[i]
elif i in reg:
converted+=reg[i]
elif (i[0]=="$"):
# check for immediate >=0 and <=255
imm=str(bin(int(i[1:len(i)])).replace("0b",""))
if (len(imm)>8):
print(f"Line {line_num}:Immediate out of range")
finalflag=1
immediateflag=1
errorflag=1
return False
while(len(imm)<8):
imm="0"+imm
converted+=imm
convert1=converted
return True
else:
return False
def checkC(data,line_num):
global finalflag
global errorflag
global convert1
truestr="1" #truestr=1 so that checkstr!=truestr if truestr not updated in if elif statement
if(len(data)!=3):
return False
if(data[-1][0]=="R"):
truestr="ORR"
elif(data[-1]=="FLAGS"):
truestr="ORF"
checkstr=""
for i in range(0,len(data)):
if data[i] in type_C[0]:
checkstr+="O"
elif data[i] in type_C[1]:
checkstr+="R"
elif data[i] in Flags:
checkstr+="F" # what if flag is in between we have to report that as misuse of FLAG not as syntax error
elif data[i]=="FLAGS" and i<(len(data)-1): # flag error udated
print(f"Line {line_num}:Misuse of FLAGS Error")
finalflag=1
errorflag=1
return False
if(checkstr==truestr):
converted=""
for i in data:
if i in opcode_C:
converted+=str(int(opcode_C[i])+1)+"00000"
elif i in reg:
converted+=reg[i]
elif i in Flags:
converted+=Flags[i]
convert1=converted
return True
else:
return False
def checkD(data,line_num):
global finalflag
global errorflag
global immediateflag
global var
global convert1
arglen=len(data)
if (arglen!=3 or immediateflag==1):
return False
truestr="ORM"
checkstr=""
for i in range(0,len(data)):
if data[i] in type_D[0]:
checkstr+="O"
elif data[i] in type_D[1]:
checkstr+="R"
elif data[i]=="FLAGS": # flag error udated
print(f"Line{line_num}:Misuse of FLAGS Error")
finalflag=1
errorflag=1
return False
else:
checkstr+="M"
if(checkstr==truestr):
converted=""
valid_var=None
for i in data:
if i in opcode_D:
converted+=opcode_D[i]
elif i in reg:
converted+=reg[i]
else:
try:
valid_var=var[i]
except KeyError:
pass
if(valid_var==None and i in label):
print(f"Line {line_num}:Label used in place of variable")
finalflag=1
errorflag=1
return False
elif(valid_var==None):
print(f"Line {line_num}:Undeclared variable used")
finalflag=1
errorflag=1
return False
converted+=var[i]
convert1=converted
return True
else:
return False
def checkE(data,line_num):
global finalflag
global errorflag
global convert1
global label
arglen=len(data)
if arglen!=2:
return False
truestr="OM"
checkstr=""
for i in range(0,len(data)):
if data[i] in type_E[0]:
checkstr+="O"
else:
checkstr+="M"
if(checkstr==truestr):
converted=""
valid_addr=None
for i in data:
if i in opcode_E:
converted+=opcode_E[i]+"000"
else:
try:
valid_addr=label[i]
except KeyError:
pass
if(valid_addr==None and i in var):
print(f"Line {line_num}:Variable used in place of label")
finalflag=1
errorflag=1
return False
elif(valid_addr==None):
print(f"Line {line_num}:Undeclared label used")
finalflag=1
errorflag=1
return False
converted+=label[i]
convert1=converted
return True
else:
return False
def checkF(data,line_num):
global errorflag
global convert1
arglen=len(data)
if (arglen!=1):
return False
truestr="O"
checkstr=""
converted=""
for i in range(0,len(data)):
if data[i] in type_F[0]:
checkstr+="O"
if(checkstr==truestr):
for i in data:
if i in hlt:
converted+=hlt[i]+"00000000000"
convert1=converted
return True
else:
return False
flagvar=0
finallist=[]
list1=[]
for line in fileinput.input():
list1.append(line)
list1=[line.rstrip() for line in list1]
# print(list1)
if(len(list1)>256):
print("line limit exceeded")
finalflag=1
for i in list1:
doublelabelflag=0
data=i.split()
try:
if(data[0][-1]==":"):
data2=[]
for k in range(1,len(data)):
data2.append(data[k])
# print(data2)
for j in data2:
if (":" in j):
# print(": in data")
doublelabelflag=1
break
# print(doublelabelflag)
if(doublelabelflag!=1 and len(data)>1):
# print("Form label")
label_form(data[0][0:len(data[0])-1])
except:
# print("pass")
pass
data=list1[-1].split()
if data[0]=="hlt" and len(data)>1:
print("NO hlt present at end or hlt syntax is not appropriate")
finalflag=1
elif data[0]!="hlt":
print("NO hlt present at end or hlt syntax is not appropriate")
finalflag=1
# print(list1)
# print((list1[4].split())[2])
data=[]
for i in range(len(list1)):
data=list1[i].split()
# print(i)
if(list1[i]==''):
pass
elif(data[0][-1]==":"):
doublelabelflag=0
try:
if(data[0][-1]==":"):
data2=[]
for j in range(1,len(data)):
data2.append(data[j])
# print(data2)
for k in data2:
if (":" in k):
doublelabelflag=1
break
if(doublelabelflag==1):
print(f"Line {i+1}:General Syntax Error")
finalflag=1
# print(379)
if(len(data)>1):
data=data[1:len(data)]
except:
# print("pass")
pass
if(list1[i]==''):
pass
elif(errorflag!=1 and checkA(data,i+1)==True):
finallist.append(convert1)
flagvar=1
elif(errorflag!=1 and checkC(data,i+1)==True):
finallist.append(convert1)
flagvar=1
elif(errorflag!=1 and checkB(data,i+1)==True):
finallist.append(convert1)
flagvar=1
elif(errorflag!=1 and checkD(data,i+1)==True):
finallist.append(convert1)
flagvar=1
elif(errorflag!=1 and checkE(data,i+1)==True):
finallist.append(convert1)
flagvar=1
elif(checkF(data,i+1)==True and not(i<len(list1)-1)):
finallist.append(convert1)
flagvar=1
elif(data[0]=="var" and flagvar==1):
print(f"Line {i+1}:Cannot declare variable")
finalflag=1
elif(data[0]=="var" and len(data)==2):
# can it handle redeclaration of variable & non ususal variable names
var_form(data[1])
# elif(data[0]=="Var" and len(data)!=2): #condiition added to check valid var statement
# print(f"Line {i+1}:General Syntax Error")
elif(data[0][-1]==":" and len(data)>1):
flagvar=1
print(f"Line {i+1}:General Syntax Error")
finalflag=1
elif(data[0]=="hlt" and i<(len(list1)-1)):
if len(data)>1:
print(f"Line {i+1}:General Syntax Error")
finalflag=1
else:
print(f"Line {i+1}:Hlt detected in between ")
finalflag=1
elif(errorflag!=1):
print(f"Line {i+1}:General Syntax Error")
finalflag=1
errorflag=0
immediateflag=0
import sys
def print_to_stdout(*a):
print(*a, file = sys.stdout)
if(finalflag!=1):
for i in finallist:
print_to_stdout(i)