-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task-1.py
108 lines (78 loc) · 2.54 KB
/
Task-1.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
R_type_funct_codes = {
'add' : '100000',
'sub' : '100010',
'and' : '100100',
'or' : '100101',
'slt' : '101010'
}
I_type_op_codes = {
'lw' : '100011',
'beq' : '000100',
'addi' : '001000'
}
J_type_op_codes = {
'j': '000010'
}
Register_codes = {
'$zero': '00000', '$t0': '01000', '$t1': '01001', '$t2': '01010',
'$t3': '01011', '$t4': '01100', '$t5': '01101', '$t6': '01110',
'$t7': '01111', '$s0': '10000', '$s1': '10001'
}
def compile(assembly):
binary_code = []
data_section = False
text_section = False
for line in assembly:
line = line.strip()
if line == '.data':
data_section = True
text_section = False
elif line == '.text':
data_section = False
text_section = True
elif text_section == True:
instruction = convert(line)
binary_code.append(instruction)
return binary_code
def convert(line):
# parts = line.split()
parts = [part.strip(',') for part in line.split()]
if parts[0] in R_type_funct_codes:
instruction = parse_R_type(parts)
elif parts[0] in I_type_op_codes:
instruction = parse_I_type(parts)
elif parts[0] in J_type_op_codes:
instruction = parse_J_type(parts)
return instruction
def parse_R_type(parts):
op_code = '000000'
shamt = '00000'
funct = R_type_funct_codes[parts[0]]
rd = Register_codes[parts[1]]
rs = Register_codes[parts[2]]
rt = Register_codes[parts[3]]
return op_code + " " + rs + " " + rt + " " + rd + " " + shamt + " " + funct
def parse_I_type(parts):
instruction = parts[0]
op_code = I_type_op_codes[instruction]
if instruction == 'lw':
rt = Register_codes[parts[1]]
offset, base = parts[2].split('(')
offset = format(int(offset), '016b')
rs = Register_codes[base[:-1]]
return op_code + " " + rs + " " + rt + " " + offset
elif instruction == 'beq':
rs = Register_codes[parts[1]]
rt = Register_codes[parts[2]]
offset = format(int(parts[3]), '016b')
return op_code + " " + rs + " " + rt + " " + offset
elif instruction == 'addi':
rs = Register_codes[parts[2]]
rt = Register_codes[parts[1]]
immediate = format(int(parts[3]), '016b')
return op_code + " " + rs + " " + rt + " " + immediate
def parse_J_type(parts):
instruction = parts[0]
op_code = J_type_op_codes[instruction]
address = format(int(parts[1]), '026b')
return op_code + " " + address