-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembler_gpr69.py
559 lines (483 loc) · 15.6 KB
/
assembler_gpr69.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
""" ASUMPTIONS:
label type instruction: only 1 label declaration per line i.e, ":"occurs only once.
mov R1 FLAGS is taken to be a valid command since FLAGS doesn't represent a variable but instead represents a valid register name , where
MOV can be used with two types of instructions , type1- mov register value , mov reg1 reg2 , hence we have taken to be a valid name
"""
import sys
op_code = {
'add': '00000',
'sub': '00001',
'mov': {'00010', '00011'}, # B,C
'ld': '00100',
'st': '00101',
'mul': '00110',
'div': '00111',
'rs': '01000',
'ls': '01001',
'xor': '01010',
'or': '01011',
'and': '01100',
'not': '01101',
'cmp': '01110',
'jmp': '01111',
'jlt': '11100',
'jgt': '11101',
'je': '11111',
'hlt': '11010',
'clear': '10011',
'incr': '11000',
'decr': '10110',
'swap': '10101',
'set': '10011'
}
op_type = {
"A": ['add', 'sub', 'mul', 'xor', 'or', 'and'],
"B": ['mov', 'rs', 'ls'],
"C": ['mov', 'div', 'not', 'cmp', 'swap'],
"D": ['ld', 'st'],
"E": ['jmp', 'jlt', 'jgt', 'je'],
"F": ['hlt'],
"G": ['clear', 'incr', 'decr','set'],
"H": ['set']
}
reg = {
'R0': '000',
'r0': '000',
'R1': '001',
'r1': '001',
'R2': '010',
'r2': '010',
'R3': '011',
'r3': '011',
'R4': '100',
'r4': '100',
'R5': '101',
'r5': '101',
'R6': '110',
'r6': '110',
'FLAGS': '111'
}
def error_lno2(lno):
for i in range(len(empty_lines)):
if empty_lines[i] == all_instructions[lno]:
return i + 1
def error_lno(lno):
for i in range(len(all_with_elines)):
if all_with_elines[i] == all_instructions[lno]:
return i + 1
def error_inst(l):
for i in range(len(all_with_elines)):
if all_with_elines[i][0] == l and all_with_elines[i][1] == False:
all_with_elines[i][1] = True
return (i + 1)
def error_1(lst, lno):
lst_str = []
for i in lst:
i_str = " ".join(i)
lst_str.append(i_str)
for i in range(len(empty_lines)):
if empty_lines[i] == lst_str[lno]:
return i + 1
def hlt_checker(list, tmp_words):
flag = 1
tmp_flag = 0
for word in tmp_words:
word_index = tmp_words.index(word)
if "hlt" in word:
tmp_flag |= 1
if not (tmp_flag):
flag = 0
error_output.append("Missing hlt instruction.")
else:
if (tmp_words[-1][-1] != "hlt"):
flag = 0
s = "Error as hlt is not being used as the last instruction.Error in line " + str(
word_index + 1)
error_output.append(s)
for instruction in tmp_words[0:-1]:
if instruction[-1] == "hlt":
flag = 0
s = "ERROR_in_hlt: General syntax error.Error in line " + str(
word_index + 1)
error_output.append(s)
return flag
def check_imm(n, tmp_ins):
if 0 <= int(n) <= 127:
return True
else:
for it in empty_lines:
if tmp_ins in it:
lab_add = empty_lines.index(it) + 1
s = "Illegal immediate value. Error in line" + str(lab_add)
error_output.append(s)
return False
def imm_to_bin(n):
return "{0:07b}".format(int(n))
def check_labels(list1):
empty_lines2 = []
flag = 1
for word in list1:
if word[0] in op_type["E"]:
label = word[1]
if label not in labels_list:
word = " ".join(word)
for k in empty_lines:
k = k.strip().split()
k = " ".join(k)
empty_lines2.append(k)
indx = empty_lines2.index(word) + 1
s = "ERROR: Use of undefined label.Error in line " + str(indx)
error_output.append(s)
flag = 0
return flag
def flags():
flag = 1
for k in range(0, len(inst)):
i = inst[k]
if "FLAGS" in i:
if not (i[0] == "mov" and i[-1] == "FLAGS" and len(i) == 3):
flag = 0
s = "ERROR: Illegal use of FLAGS register. Error in line " + str(error_inst(i))
error_output.append(s)
return flag
def typos(list):
flag = 1
countf = 0
for words in list:
countf += 1
flag &= identify_error_type(words)
return flag
def var_not_dec(inst, var):
flag = 1
for i, ins in enumerate(inst):
if (ins[0] in op_type.get("D")) and (ins[-1] not in var) and len(ins) == 2:
s = f"ERROR: variable {ins[-1]} is not declared. Error in line " + str(
error_1(inst, i))
error_output.append(s)
flag = 0
"""elif ins[0] in op_type.get("E") and ins[-1] not in labels_list:
s = f"ERROR: variable {ins[-1]} is not declared. Error in line " + str(
error_1(inst, i))
error_output.append(s)
flag = 0"""
return flag
def not_defined_at_beginning(list1):
flag = 1
for i, line in enumerate(list1):
if line[0] == "var" and len(line) == 2:
if line[-1] in op_code.keys():
s = "Opcode cannot be used as variable name. Error in line " + str(
error_1(list1, i))
error_output.append(s)
flag = 0
functions = [line[0] for line in tmp_words]
j = 0
while functions[j] == 'var':
j += 1
for i in range(j, len(functions)):
if functions[i] == 'var' and len(tmp_words[i]) == 2:
s = "ERROR:Variables not declared at the beginning. Error in line " + str(
error_inst(tmp_words[i]))
error_output.append(s)
flag = 0
return flag
def immediate():
flag = 1
for line in all_instructions:
if "$" in line:
k = line.split()
imm = k[-1]
if imm[1:].isdigit():
imm = int(imm[1:])
if not check_imm(imm, line):
flag = 0
else:
s = "Immediate is not a numeric value. Error in line " + str(
error_lno2(all_instructions.index(line)))
error_output.append(s)
flag = 0
return flag
def error_type_A(lst):
flag = 1
if len(lst) == 4:
if not (lst[1] in reg.keys()) & (lst[2] in reg.keys()) & (lst[3]
in reg.keys()):
s = "ERROR:Typos in register name. Error in line " + str(error_inst(lst))
error_output.append(s)
flag = 0
else:
s = "ERROR: General Syntax error. Error in line " + str(error_inst(lst))
error_output.append(s)
flag = 0
return flag
def error_type_B(lst):
flag = 1
if len(lst) == 3:
if not (lst[1] in reg.keys()):
s = "ERROR:Typos in register name. Error in line " + str(error_inst(lst))
error_output.append(s)
flag = 0
else:
s = "ERROR: General Syntax error. Error in line " + str(error_inst(lst))
error_output.append(s)
flag = 0
return flag
def error_type_C(lst):
flag = 1
if len(lst) == 3:
if not (lst[1] in reg.keys()) & (lst[2] in reg.keys()):
indx = empty_lines.index(" ".join(lst)) + 1
s = "ERROR:Typos in register name. Error in line " + str(indx)
error_output.append(s)
flag = 0
else:
s = "ERROR: General Syntax error. Error in line " + str(
empty_lines.index(" ".join(lst)) + 1)
error_output.append(s)
flag = 0
return flag
def error_type_D(lst, labl, varl):
flag = 1
if len(lst) == 3:
if lst[1] in reg.keys():
if lst[-1] in labl:
flag = 0
print("ERROR: Use of label as variable.\nError in line ",
error_inst(lst))
s = "ERROR: Use of label as variable.\nError in line " + str(
error_inst(lst))
error_output.append(s)
elif (not (lst[2] in varl)):
flag = 0
s = "ERROR: only variables can be used as m_add. Error in line " + str(
error_inst(lst))
error_output.append(s)
else:
flag = 0
s = "ERROR: Typos in register name. Error in line " + str(
error_inst(lst))
error_output.append(s)
if lst[-1] + ":" in labl:
s = "ERROR: Use of label as variable. Error in line " + str(
error_inst(lst))
error_output.append(s)
else:
s = "ERROR: General Syntax error. Error in line " + str(error_inst(lst))
error_output.append(s)
flag = 0
return flag
def error_type_E(lst, varl):
flag = 1
if len(lst) == 2:
if lst[-1] in varl:
flag = 0
s = "ERROR: Use of variable as label. Error in line " + str(error_inst(lst))
error_output.append(s)
elif not (lst[-1] in labels_list):
flag = 0
s = "ERROR: in m_add Error in line " + str(error_inst(lst))
error_output.append(s)
else:
flag = 0
s = "ERROR: General Syntax error.Error in line " + str(error_inst(lst))
error_output.append(s)
if lst[-1] in varl:
s = "ERROR: Use of variable as label. Error in line " + str(error_inst(lst))
return flag
def identify_error_type(lst):
flag = 1
if lst[0] in ['mov']:
if lst[-1][0] == '$':
inst_type.append("b")
flag = error_type_B(lst)
else:
flag = error_type_C(lst)
inst_type.append("c")
elif lst[0] in op_type.get("A"):
inst_type.append("a")
flag = error_type_A(lst)
elif lst[0] in op_type.get("B"):
inst_type.append("b")
flag = error_type_B(lst)
elif lst[0] in op_type.get("C"):
inst_type.append("c")
flag = error_type_C(lst)
elif lst[0] in op_type.get("D"):
inst_type.append("d")
flag = error_type_D(lst, labels_list, var)
elif lst[0] in op_type.get("E"):
inst_type.append("e")
flag = error_type_E(lst, var)
elif lst[0] in op_type.get("F"):
inst_type.append("f")
elif lst[0] in op_type.get("G"):
inst_type.append("g")
elif lst[0] in op_type.get('H'):
inst_type.append("h")
else:
s = "ERROR:Typos in instruction name. Error in line " + str(
error_inst(lst))
error_output.append(s)
flag = 0
return flag
def check_empty():
flag = 1
if tmp_words == []:
flag = 0
f = open('output.txt', 'w')
f.write("ERROR: File empty.")
return flag
# checking all errors
def check_all_errors():
flag = 1
if check_empty() == 0:
flag = 0
return flag
if var_not_dec(inst, var) != 1:
flag = 0
if not_defined_at_beginning(tmp_words) != 1:
flag = 0
if hlt_checker(all_instructions, tmp_words) != 1:
flag = 0
if immediate() != 1:
flag = 0
if check_labels(tmp_words) != 1:
flag = 0
if flags() != 1:
flag = 0
if typos(inst) != 1:
flag = 0
return (flag and empty_label and flag_out)
# main
# reading input file.
# page=""
# line=""
# while True:
# line=sys.stdin.readline().strip()
# if not line:
# break
# page+=line + "\n"
# page = sys.stdin.read()
f = open('input.txt', 'r')
page = f.read()
all_instructions = [x.lstrip().rstrip() for x in page.split('\n') if x != ""]
empty_lines = [x.lstrip().rstrip() for x in page.split('\n')]
all_with_elines, tmp_words, = [], []
for lin in empty_lines:
lin = lin.split()
if lin == []:
all_with_elines.append([[], False])
elif lin[0][-1] == ":":
if len(lin) == 1:
all_with_elines.append([lin, False])
else:
all_with_elines.append([lin[1::], False])
else:
all_with_elines.append([lin, False])
for line in all_instructions:
words = line.split()
tmp_words.append(words)
# classification of labels, var dec & instructions.
inst, inst_type = [], []
labels_list = []
var, var_dic = [], {}
error_output, labels_with_inst = [], []
labels = {}
empty_label, flag_out = 1, 1
for i in range(0, len(tmp_words)):
line = tmp_words[i]
if line[0] == "var":
if len(line) == 2:
var.append(line[1])
else:
pe = "ERROR: General Syntax error. Error in line " + str(error_inst(line))
error_output.append(pe)
flag_out &= 0
elif line[0][-1] == ":":
labels_list.append(line[0][0:-1])
if len(line) == 1:
pe = "ERROR: empty label encountered. Error in line " + str(error_inst(line))
error_output.append(pe)
empty_label = 0
else:
l = " ".join(line)
labels_with_inst.append(l)
inst.append(
line[1:]) # adding the instruction only not the name in the inst list
elif (line[0] in op_code.keys() and len(line) < 2
and "hlt" not in line) or line[0] not in op_code.keys():
pe = "ERROR: General Syntax error. Error in line " + str(error_inst(line))
error_output.append(pe)
flag_out &= 0
else:
inst.append(line)
# functions to print binary of the specific type of instructions
def type_A(lst):
opcode, r1, r2, r3 = lst[0], lst[1], lst[2], lst[3]
return op_code.get(opcode) + '00' + reg.get(r1) + reg.get(r2) + reg.get(
r3) + '\n'
def type_B(lst):
opcode, r1, imm = lst[0], lst[1], lst[2]
if opcode == 'mov':
return '00010' + '0' + reg.get(r1) + imm_to_bin(imm[1:]) + '\n'
return op_code.get(opcode) + '0' + reg.get(r1) + imm_to_bin(imm[1:]) + '\n'
def type_C(lst):
opcode, r1, r2 = lst[0], lst[1], lst[2]
if opcode == 'mov':
return '00011' + '00000' + reg.get(r1) + reg.get(r2) + '\n'
return op_code.get(opcode) + '00000' + reg.get(r1) + reg.get(r2) + '\n'
def type_D(lst):
opcode, r1, m_add = lst[0], lst[1], str(imm_to_bin(var_dic.get(lst[2])))
return op_code.get(opcode) + '0' + reg.get(
r1) + m_add + '\n'
def type_E(lst):
tmp_label = lst[-1] + ":"
for i in tmp_words:
if tmp_label in i:
tmp_label = i[1::]
lab_add = inst.index(tmp_label)
opcode = lst[0]
return op_code.get(opcode) + '0000' + str(imm_to_bin(lab_add)) + '\n'
def type_F(lst):
opcode = lst[0]
return op_code.get(opcode) + '00000000000'
def type_G(lst):
opcode = lst[0]
reg_nam = lst[1]
return op_code.get(opcode) + '00000000' + reg.get(reg_nam) + '\n'
def print_binary():
out = ""
for i in range(0, len(inst_type)):
type = inst_type[i]
if type == "a":
out += type_A(inst[i])
elif type == "b":
out += type_B(inst[i])
elif type == "c":
out += type_C(inst[i])
elif type == "d":
out += type_D(inst[i])
elif type == "e":
out += type_E(inst[i])
elif type == "f":
out += type_F(inst[i])
elif type == "g":
out += type_G(inst[i])
print(out)
def print_errors():
out = ""
for error in error_output:
out += error
out += "\n"
print(out)
var_counter = len(all_instructions) - len(var)
for variable in var:
var_dic[variable] = var_counter
var_counter = var_counter + 1
if check_empty():
# f = open('output.txt', 'w')
if check_all_errors() and flag_out:
print_binary()
else:
error_output = sorted(error_output, key=lambda x: x[-2])
print_errors()