-
Notifications
You must be signed in to change notification settings - Fork 1
/
Assembler.py
620 lines (559 loc) · 17.6 KB
/
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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# registers binary code
d={"R0":"000",
"R1":"001",
"R2":"010",
"R3":"011",
"R4":"100",
"R5":"101",
"R6":"110",
"FLAGS":"111"
}
# type D instructions and opcode
ld="00100"
st="00101"
add="00000"
sub="00001"
mul="00110"
xor="01010"
Or="01011"
And="01100"
movi="00010"
rsi="01000"
lsi="01001"
mov="00011"
div="00111"
Not="01101"
com="01110"
# # type f
hlt="11010"
#
var = {}
register=["R0","R1","R2","R3","R4","R5","R6"]
# a string of special chars for type b instn.
special_chars="~`!@#$%^&()_-=+{|}|:;'\"<>,.?/[]/"
#
possible_instructuion={ #dict for type of instruction
"add": "A",
"sub": "A",
"mov": "B&C",
"ld": "D",
"st": "D",
"mul": "A",
"div": "C",
"rs": "B",
"ls": "B",
"xor": "A",
"and": "A",
"or": "A",
"not": "C",
"cmp": "C",
"jmp": "E",
"jlt": "E",
"jgt": "E",
"je":"E",
"hlt": "F",
"clr":"G",
"adi":"H",
"exp":"I",
"inc":"J",
"dec":"K"
}
list_output=[]
label_names={}
label_collection={}
#
list_of_errors=[]
instructions=[]
def is_halt_at_last(data):
data=data[::-1]
for i in data:
instrn=i.strip().split()
if(len(instrn)<=1 and instrn==[]):
pass # a empty line
else:
if(instrn[-1]=="hlt"):
# hlt is at last
break
else:
list_of_errors.append("hlt instruction is not at last!!!".title())
print(list_of_errors[0],end="")
exit(1)
'''with open("output.txt","w") as f:
f.write(list_of_errors[0])
exit(1)'''
def error_has_occurred():
global is_unhandled_error
is_unhandled_error=True
print(f"Error in Line:{line_counter+1}\n",end="")
print(list_of_errors[0],end="")
'''file_handle=open("output.txt","w")
file_handle.write(f"Error Has Occurred In Line:{line_counter+1}\n")
file_handle.write(list_of_errors[0])
file_handle.close()'''
exit(1)
def check_for_valid_registers(string):
if(string in register):
return 1
else:
return -1
def type_A(instruct,list_output):
s=""
if(len(instruct)!=4):
list_of_errors.append("parameters not satisfied!!!".title())
error_has_occurred()
if instruct[0]=="add":
s += add
elif instruct[0]=="sub":
s += sub
elif instruct[0]=="mul":
s += mul
elif instruct[0]=="xor":
s += xor
elif instruct[0]=="or":
s += Or
elif instruct[0]=="and":
s += And
s += "00"
value1=check_for_valid_registers(instruct[1])
value2=check_for_valid_registers(instruct[2])
value3=check_for_valid_registers(instruct[3])
if(value1==-1 or value2==-1 or value3==-1):#register is wrong
error_string="register is invalid".title()
list_of_errors.append(error_string)
error_has_occurred()
else:
s += d[instruct[1]]
s += d[instruct[2]]
s += d[instruct[3]]
list_output.append(s)
def type_G(instruct,list_output):
s=""
s += "11001"
s += "0"*11
list_output.append(s)
def type_H(instruct,list_output):
s=""
s += "11110"
s += "0"
val1=check_for_valid_registers(instruct[1])
if(val1==-1):
error_string="register is invalid".title()
list_of_errors.append(error_string)
error_has_occurred()
else:
s += d[instruct[1]]
string=instruct[2]
number = int(''.join(filter(str.isdigit, string)))
fg = format(number, '07b')
s += fg
list_output.append(s)
def type_I(instruct,list_output):
s=""
s += "10011"
s += "0"*2
value1=check_for_valid_registers(instruct[1])
value2=check_for_valid_registers(instruct[2])
value3=check_for_valid_registers(instruct[3])
if(value1==-1 or value2==-1 or value3==-1):#register is wrong
error_string="register is invalid".title()
list_of_errors.append(error_string)
error_has_occurred()
else:
s += d[instruct[1]]
s += d[instruct[2]]
s += d[instruct[3]]
list_output.append(s)
def type_J(instruct,list_output):
s=""
s += "10100"
s += "0"*8
val=check_for_valid_registers(instruct[1])
if(val==-1):
error_string="register is invalid".title()
list_of_errors.append(error_string)
error_has_occurred()
else:
s += d[instruct[1]]
list_output.append(s)
def type_K(instruct,list_output):
s=""
s += "10101"
s += "0"*8
val=check_for_valid_registers(instruct[1])
if(val==-1):
error_string="register is invalid".title()
list_of_errors.append(error_string)
error_has_occurred()
else:
s += d[instruct[1]]
list_output.append(s)
def check_for_valid_immediate_value(string):
symbol=string[0]
int_value=string[1:]
if(symbol=="$"):
if(0<=int(int_value)<=127):
return 1
else:
return -2
else:
return -1
def type_B(instruct,list_output):
s=""
if(len(instruct)!=3):
list_of_errors.append("parameters not satisfied!!!".title())
error_has_occurred()
if instruct[0]=="mov":
s += movi
elif instruct[0]=="ls":
s += lsi
elif instruct[0]=="rs":
s += rsi
s += "0"
value1=check_for_valid_registers(instruct[1])
if(value1==-1 and instruct[0]!="mov"): #register is worng
error_string="register is invalid".title()
list_of_errors.append(error_string)
error_has_occurred()
else:
if(instruct[0]=="mov"):
if( instruct[1]=="FLAGS"): #this is added to insure the use of flags register is only for mov
s+="111"
else:
value1=check_for_valid_registers(instruct[1])
if(value1==-1):
error_string="register is invalid".title()
list_of_errors.append(error_string)
error_has_occurred()
else:
s+=d[instruct[1]]
else:
s += d[instruct[1]]
result=check_for_valid_immediate_value(instruct[2])
if(result==-1):
list_of_errors.append("syntax error: invalid symbol!!!".title())
error_has_occurred()
elif (result==-2):
list_of_errors.append("invalid immediate value!!!".title())
error_has_occurred()
else:
string=instruct[2]
number = int(''.join(filter(str.isdigit, string)))
fg = format(number, '07b')
s += fg
list_output.append(s)
def type_C(instruct,list_output):
s=""
if(len(instruct)!=3):
list_of_errors.append("parameters not satisfied!!!".title())
error_has_occurred()
if instruct[0]=="mov":
s += mov
elif instruct[0]=="div":
s += div
elif instruct[0]=="not":
s += Not
elif instruct[0]=="cmp":
s += com
s += "00000"
value1=check_for_valid_registers(instruct[1])
value2=check_for_valid_registers(instruct[2])
if((value1==-1 or value2==-1) and (instruct[0]!="mov")):
error_string="register is invalid".title()
list_of_errors.append(error_string)
error_has_occurred()
else:
if(instruct[0]=="mov"):
if(instruct[1]=="FLAGS" or instruct[2]=="FLAGS"):
if(instruct[1]=="FLAGS" and instruct[2] in register):
s+="111"
s+=d[instruct[2]]
elif (instruct[2]=="FLAGS" and instruct[1] in register):
s+=d[instruct[1]]
s+="111"
elif (instruct[1]=="FLAGS" and instruct[2]=="FLAGS"):
s+="111"
s+="111"
else:
list_of_errors.append("register is invalid!!!".title())
error_has_occurred()
else:
value1=check_for_valid_registers(instruct[1])
value2=check_for_valid_registers(instruct[2])
if(value1==-1 or value2==-1):
list_of_errors.append("register is invalid!!!".title())
error_has_occurred()
else:
s+=d[instruct[1]]
s+=d[instruct[2]]
else:
s += d[instruct[1]]
s += d[instruct[2]]
list_output.append(s)
def type_D(instruct,list_output):
s=""
if(len(instruct)!=3):
list_of_errors.append("parameters not satisfied!!!".title())
error_has_occurred()
value1=check_for_valid_registers(instruct[1])
if(value1==-1):
error_string="register is invalid".title()
list_of_errors.append(error_string)
error_has_occurred()
else:
try:
if instruct[0]=="ld":
s += ld
s += "0" #unused bit
s += d[instruct[1]] #register
s += var[instruct[2]]
list_output.append(s)
else:
s += st
s += "0" #unused bit
s += d[instruct[1]] #register
s += (var[instruct[2]])
list_output.append(s)
except:
list_of_errors.append("variable is not declared!!!".title())
error_has_occurred()
def type_E(instruct,list_output):
s=""
if(len(instruct)!=2):
list_of_errors.append("parameters not satisfied!!!".title())
error_has_occurred()
if(instruct[1] in var):
list_of_errors.append("illegal use of variables!!!".title())
error_has_occurred()
if((instruct[1]+":") in label_collection):
binary_line=label_collection[instruct[1]+":"]
else:
list_of_errors.append("label is not declared and tried to use!!!".title())
error_has_occurred()
if instruct[0]=="jmp":
s += "01111"
s += "0000"
s += binary_line
elif instruct[0]=="jlt":
s += "11100"
s += "0000"
s += binary_line
elif instruct[0]=="jgt":
s += "11101"
s += "0000"
s += binary_line
else:
s += "11111"
s += "0000"
s += binary_line
list_output.append(s)
def find_type_of_instruction (instruct): # func to find the type of instruction
if(instruct in possible_instructuion):
return possible_instructuion[instruct]
else:
return -1
def file_output(list_output):
end_len=len(list_output)
for i in range(0,end_len):
print(list_output[i],end="")
if(i!=end_len-1):
print("\n",end="")
'''
file_handle=open("output.txt","w")
for i in list_output:
file_handle.write(i)
file_handle.write('\n')
file_handle.flush()
file_handle.close()'''
def label_handling(label_instruction):
try:
if(label_instruction[0] =="var"):
list_of_errors.append("invalid variable use in labels!!!".title())
error_has_occurred()
val=find_type_of_instruction(label_instruction[0])
if(val!=-1):
if(val=="A"):
type_A(label_instruction,list_output)
elif (val=="B"):
type_B(label_instruction,list_output)
elif (val=="C"):
type_C(label_instruction,list_output)
elif (val=="B&C"):
just_a_temp_str=str(label_instruction[-1])
if(just_a_temp_str[0] in special_chars):
type_B(label_instruction,list_output)
else:
type_C(label_instruction,list_output)
elif (val=="D"):
type_D(label_instruction,list_output)
elif (val=="E"):
type_E(label_instruction,list_output)
#add code here
#if(len(label_instruction)!=2):
else:
list_of_errors.append("invalid instruction in memory address!!!".title())
error_has_occurred()
else:
list_of_errors.append("invalid instruction in memory address!!!".title())
error_has_occurred()
except:
list_of_errors.append("invalid instruction in memory address!!!".title())
error_has_occurred()
# main fucntion here
# checking for halt is at last or not
while True:
try:
line=input()
line=line.strip()
if(line!=""):
instructions.append(line)
except EOFError:
break
'''with open("input.txt", "r") as file:
instructions = file.readlines()'''
data=instructions.copy()
count=0
line=1
try:
for j in instructions:
ins = j.strip().split()
if(ins==[]
): #this block is used to generate the binary of labels
pass
elif ins[0]=="hlt":
count+=1
line+=1
elif ins[0]=="clr":
count+=1
line+=1
elif( ins[0]!="var"):
if(ins[1] in possible_instructuion and ins[0][-1]==":"):
if ins[0] not in label_collection:
label_collection[ins[0]]=format(count, '07b')
count +=1
line+=1
else:
line+=1
except:
print(f"error has occurred in line:{line}\n".title(),end="")
print("instruction undefined!!!".title(),end="")
exit(1)
line_counter=0
is_unhandled_error=False
is_halt=False
is_error=False
is_variable=True
'''
this block is used to check for multiple halts
'''
halt_counter=0
try:
for instruction in instructions:
if(len(instruction)<=1 or len(instruction.strip())<=1):
pass
else:
instruct=instruction.strip().split()
if(instruct[-1]=="hlt"):
halt_counter+=1
else:
pass
except:
print("instruction undefined!!!".title(),end="")
exit(1)
# now adding condition to check for multiple halts
if(halt_counter>1):
print("halt encountered more than one time !!!".title())
exit(1)
try:
for instruction in instructions:
if(len(instruction)<=1 or len(instruction.strip())<=1):
line_counter-=1
else:
instruct = instruction.strip().split()
result=find_type_of_instruction(instruct[0])
temp_s=""
if instruct[0]!="var":
is_variable=False
if ((instruct[-1])=="hlt"):
is_halt=True
temp_s += hlt
temp_s += ("0"*11)
list_output.append(temp_s)
break
elif instruct==[]:
pass
elif instruct[0]=="var":
if instruct[1] not in var:
binary = format(count, '07b')
var[instruct[1]]=binary
count +=1
if (is_variable==False):
# var is defined later
list_of_errors.append("variable not declared in the beginning!!!".title())
error_has_occurred()
else:
temp=str(instruct[-1])
if result !=-1:
if(result=="A"):
type_A(instruct,list_output)
elif result=='B' and temp[0] in special_chars:
type_B(instruct,list_output)
elif result=='C' and temp[0] not in special_chars:
type_C(instruct,list_output)
elif result=='B&C':
if temp[0] in special_chars:
type_B(instruct,list_output)
else:
type_C(instruct,list_output)
elif result=='D':
type_D(instruct,list_output)
elif result=='E':
type_E(instruct,list_output)
elif result=="G":
type_G(instruct,list_output)
elif result=="H":
type_H(instruct,list_output)
elif result=="I":
type_I(instruct,list_output)
elif result=="J":
type_J(instruct,list_output)
elif result=="K":
type_K(instruct,list_output)
else:
# instrution not found
if(instruct[1] in possible_instructuion): #valid
if(instruct[0] in label_collection):
label_handling(instruct[1:])
else:
if (instruct[0][-1]!=":"):
list_of_errors.append("label is not followed by colon!!!".title())
error_has_occurred()
else:
list_of_errors.append("general syntax error!!!".title())
error_has_occurred()
else:
list_of_errors.append("wrong instruction!!!".title())
error_has_occurred()
break
line_counter+=1
except:
if(is_unhandled_error==False):
print("general syntax error!!!".title(),end="")
'''with open("output.txt","w") as f:
f.write("general syntax error!!!".title())'''
exit(1)
else:
exit(1)
'''
output function here
'''
#print(label_names)
if(is_halt==True):
is_halt_at_last(instructions)
file_output(list_output) #output function here
else:
list_of_errors.append("halt not found!!!".title())
print("syntax error \n".title(),end="")
print(list_of_errors[0],end="")
'''with open("output.txt","w") as f:
f.write("syntax error\n".title())
f.write(list_of_errors[0])'''