-
Notifications
You must be signed in to change notification settings - Fork 1
/
assembler.py
138 lines (129 loc) · 5.54 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
#IITB RISC ASSEMBLER
'''
This assembler is capable of converting IITB-RISC-22 ISA assembly instructions to binaries
Which can be fed to the memory of our processor using a bootloader
'''
import sys
binaries = ''
if __name__ == '__main__':
params = sys.argv
if(len(params) > 1):
filename = params[1]
with open(filename + '.asm', 'r') as t:
code = t.readlines()
# comment handler
code = [i for i in code if i[0] != ';' or len(i) != 0]
for i, j in enumerate(code):
if ';' in j:
j = j.split(';')[0]
code[i] = j
code = [i for i in code if i != '']
for line in code:
inst, args = line.split()[:2]
args = args.split(',')
if(inst=="add" or inst=="adc" or inst=="adz" or inst=="adl"):
binaries+="0001"
regs = ''
for i in args:
if i[0] == "r":
regs = bin(int(i[1]))[2:].zfill(3) + regs
binaries += regs[3:6] + regs[:3] + regs[6:] # due to encoding being ra rb rc, asm being rc ra rb
binaries += "0"
if (inst == "add"):
binaries += "00"
elif (inst == "adc"):
binaries += "10"
elif (inst == "adz"):
binaries += "01"
else:
binaries += "11"
if(inst == "adi"):
binaries += "0000"
regs = ''
for i in args:
if i[0] == "r":
regs = bin(int(i[1]))[2:].zfill(3) + regs
else:
binaries += regs
binaries += bin(int(i))[2:].zfill(6)
if(inst == "ndu" or inst == "ndc" or inst == "ndz"):
binaries += "0010"
regs = ''
for i in args:
if i[0] == "r":
regs = bin(int(i[1]))[2:].zfill(3) + regs
binaries += regs[3:6] + regs[:3] + regs[6:] # due to encoding being ra rb rc, asm being rc ra rb
binaries += "0"
if(inst == "ndu"):
binaries += "00"
elif (inst == "ndc"):
binaries += "10"
elif (inst == "ndz"):
binaries += "01"
if(inst == "lhi"):
binaries += "0011"
for i in args:
if i[0]=="r":
binaries += bin(int(i[1]))[2:].zfill(3)
else:
binaries += bin(int(i))[2:].zfill(9)
if(inst == "lw"):
binaries += "0111"
for i in args:
if i[0]=="r":
binaries += bin(int(i[1]))[2:].zfill(3)
else:
binaries += bin(int(i))[2:].zfill(6)
if(inst == "sw"):
binaries += "0101"
for i in args:
if i[0] == "r":
binaries += bin(int(i[1]))[2:].zfill(3)
else:
binaries += bin(int(i))[2:].zfill(6)
if(inst == "lm"):
binaries += "1100"
for i in args:
if i[0] == "r":
binaries += bin(int(i[1]))[2:].zfill(3)
else:
binaries += bin(int(i))[2:].zfill(9)
if(inst == "sm"):
binaries += "1101"
for i in args:
if i[0] == "r":
binaries += bin(int(i[1]))[2:].zfill(3)
else:
binaries += bin(int(i))[2:].zfill(9)
if(inst == "beq"):
binaries += "1000"
for i in args:
if i[0] == "r":
binaries += bin(int(i[1]))[2:].zfill(3)
else:
binaries += bin(int(i))[2:].zfill(6)
if(inst == "jal"):
binaries += "1001"
for i in args:
if i[0] == "r":
binaries += bin(int(i[1]))[2:].zfill(3)
else:
binaries += bin(int(i))[2:].zfill(9)
if(inst == "jlr"):
binaries += "1010"
for i in args:
if i[0] == "r":
binaries += bin(int(i[1]))[2:].zfill(3)
binaries += "000000"
if(inst == "jri"):
binaries += "1011"
for i in args:
if i[0] == "r":
binaries += bin(int(i[1]))[2:].zfill(3)
else:
binaries += bin(int(i))[2:].zfill(9)
with open('source.bin', 'w') as file:
file.write(binaries)
print("Assembled code successfully to /source.bin")
else:
print("No filename was passed")