Repository navigation
Expand file tree
/
Copy pathassembler_float.py
More file actions
executable file
·310 lines (249 loc) · 10.3 KB
/
Copy pathassembler_float.py
File metadata and controls
executable file
·310 lines (249 loc) · 10.3 KB
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
#
# Q3_Assembler.py
# By - Shivoy Arora
# Suhani Mathur
# Shobhit Pandey
# Date - 20/06/2022
#
import sys
######## Declaring dictionaries ########
# opcode of each command
opcodes = {"add": "10000", "sub": "10001", "mov1": "10010", "mov2": "10011", "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": "01010", "addf": "00000", "subf": "00001", "movf": "00010"}
# registers binary representation
regs = {"R0": "000", "R1": "001", "R2": "010", "R3": "011",
"R4": "100", "R5": "101", "R6": "110", "FLAGS": "111"}
# statement types
stmtTypes = {"add": "A", "sub": "A", "mov1": "B", "mov2": "C", "ld": "D", "st": "D", "mul": "A", "div": "C", "rs": "B", "ls": "B", "xor": "A",
"or": "A", "and": "A", "not": "C", "cmp": "C", "jmp": "E", "jlt": "E", "jgt": "E", "je": "E", "hlt": "F", "addf": "A", "subf": "A", "movf": "B"}
# unused space in regard to ops type
unusedSpace = {"A": "00", "B": "", "C": "00000",
"D": "", "E": "000", "F": "00000000000"}
# address of the variables declared
vars = {}
# address of the labels
labels = {}
class color:
""" Class color to color the output of the terminal """
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
def floatBin(number):
""" Convert a floating number to binary representation with 3-bit exp and 5-bit mantissa
Args:
number: floating point number to be converted
Returns:
num: string of the floating point representation
"""
whole, dec = str(number).split(".")
whole = int(whole)
dec = float("0."+dec)
binRepr = bin(whole)[2:] + "."
for _ in range(7):
whole, dec = str(dec * 2).split(".")
binRepr += whole
dec = float("0."+dec)
binRepr.strip("0")
pt = binRepr.find(".")
exp = pt - 1
binRepr = "".join(binRepr.split("."))
mantissa = binRepr[1:6]
num = "0" * (3 - len(bin(exp)[2:])) + bin(exp)[2:]
num += mantissa + (5 - len(mantissa)) * "0"
return num
def checkLine(line, lineNum):
""" Check if a line of command is correct """
global vars, labels, stmtTypes
global flag, errorGenerated
regs = {"R0", "R1", "R2", "R3", "R4", "R5", "R6"}
# Operations allowed in the assembler
opSet = {"add", "sub", "mov", "ld", "st", "mul", "div", "rs", "ls", "xor", "or",
"and", "not", "cmp", "jmp", "jlt", "jgt", "je", "hlt", "addf", "subf", "movf"}
try:
if line == ["hlt"]:
flag = 1
elif line[0] not in opSet:
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" +
color.END, "Line", lineNum, ", invalid operation")
else:
ops = line[0]
# converting ops to incorporate two types of mov command
if ops == "mov":
if line[2][0] == "$":
ops = "mov1"
else:
ops = "mov2"
stmtType = stmtTypes[ops]
if stmtType == "A" or stmtType == "C":
if ops == "mov2" and line[1] == "FLAGS" and line[2] in regs:
return
for i in line[1:]:
if i not in regs:
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" +
color.END, "Line", lineNum, ", invalid register")
elif stmtType == "B":
try:
if line[1] not in regs:
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" +
color.END, "Line", i+1, ", invalid register")
elif ops == "movf" and (float(line[2][1:]) > 252 or float(line[2][1:]) < 1):
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" + color.END, "Line",
lineNum, ", invalid value of floating point immediate")
elif ops == "movf" and line[2][1:].find(".") == -1:
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" + color.END, "Line",
lineNum, ", invalid floating point immediate (No floating point found)")
elif ops != "movf" and (int(line[2][1:]) > 255 or int(line[2][1:]) < 0):
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" + color.END, "Line",
lineNum, ", invalid value of immediate")
except ValueError as e:
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" + color.END, "Line",
lineNum, ", invalid immediate value")
elif stmtType == "D":
if line[1] not in regs:
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" +
color.END, "Line", lineNum, ", invalid register")
elif line[2] not in vars:
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" +
color.END, "Line", lineNum, ", variable not found")
elif stmtType == "E":
if line[1] not in labels:
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" +
color.END, "Line", lineNum, ", label not found")
except:
errorGenerated = 1
print(color.RED + color.BOLD + "Error:" +
color.END, "Line", lineNum, ", unexpected error found")
################# Main Function #################
if __name__ == "__main__":
######## Getting the input from STDIN ########
"""
Format of lines: List[List[string]]
For eg:-
for command: "add R1 R2 R3\nmov R3 R4"
lines is: [["add", "R1", "R2", "R3"], ["mov", "R3", "R4"]]
"""
lines = [[j.strip() for j in i.strip().split()]
for i in sys.stdin.readlines()]
######## Managing Addresses of labels and variables ########
# Getting the labels addresses
addr = 0
commands = []
for i in lines:
if i == []:
continue
elif i[0] == "var":
continue
elif i[0][-1] == ":":
binAddr = bin(addr)[2:]
labels[i[0][:-1]] = "0" * (8 - len(binAddr)) + binAddr
# removing the label from the command
i.pop(0)
# if the label doesn't have a command on the same line continuing
if i == []:
continue
commands.append(i)
addr += 1
# Getting the variables addresses
for i in lines:
if i == []:
continue
if i[0] == "var":
binAddr = bin(addr)[2:]
vars[i[1]] = "0" * (8 - len(binAddr)) + binAddr
addr += 1
else:
break
######## Checking Errors ########
flag = 0 # For hlt instruction not found
errorGenerated = 0 # To check if no error is generated, if not then assemble the code
for i in range(len(lines)):
# skipping empty lines
if len(commands) > 256:
print(color.RED + color.BOLD + "Error:" + color.END,
"Number of lines in code exceed the memory of the ISA")
if lines[i] == []:
continue
# if hlt is not the last operation
if flag == 1:
print(color.RED + color.BOLD + "Error:" + color.END,
"Line", i+1, ", halt is not the last instruction")
errorGenerated = 1
break
if lines[i][0] == "var":
continue
elif lines[i][-1] == ":":
checkLine(lines[i][1:], i+1)
else:
checkLine(lines[i], i+1)
# if hlt statement is not present
if flag == 0:
print(color.RED + color.BOLD + "Error:" +
color.END, "hlt instructions not found")
errorGenerated = 1
######## Converting to machine code ########
if errorGenerated == 0:
# creating machine code lst
machineCode = []
# traversing each command
for sNo in range(len(commands)):
binLine = ""
ops = commands[sNo][0]
# converting ops to incorporate two types of mov command
if ops == "mov":
if commands[sNo][2][0] == "$":
ops = "mov1"
else:
ops = "mov2"
# adding opcode to binLine
binLine = opcodes[ops]
# adding unused space
stmtType = stmtTypes[ops]
binLine += unusedSpace[stmtType]
# For statements with only registers and immediate values
if stmtType == "A" or stmtType == "B" or stmtType == "C":
for i in commands[sNo][1:]:
# if registers are present
if i[0] != "$":
binLine += regs[i]
# immediate value is present
#
# For floating point
elif ops == "movf" and i[0] == "$":
binLine += floatBin(i[1:])
else:
binRepr = bin(int(i[1:]))[2:]
binLine += "0" * (8 - len(binRepr)) + binRepr
# For ld and st statements
elif stmtType == "D":
# adding register to binLine
binLine += regs[commands[sNo][1]]
binLine += vars[commands[sNo][2]]
# For jumping statements
elif stmtType == "E":
# adding label addr to binLine
binLine += labels[commands[sNo][1]]
# For hlt statement
elif stmtType == "F":
pass
# Adding the line to the machine code
machineCode.append(binLine)
######## Writing to stdout ########
for i in machineCode:
print(i)