-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleAssembler.py
352 lines (278 loc) · 11.9 KB
/
SimpleAssembler.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
'''if regval=1 -> $Imm mov instruction
if regval=2 -> reg mov instruction
if ins not found -> return 0 '''
def opcode(ins, regval=0):
#returns the opcode of the instruction; returns 0 if instruction not present
opcode={"add": "10000",
"sub":"10001",
"mov":["10010","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"}
if regval==1:
return opcode["mov"][0]
elif regval==2:
return opcode["mov"][1]
for i in opcode.keys():
if ins==i:
return opcode[ins]
return 0
def regaddress(reg):
#returns the address of a specified register: returns 0 if register not present
regaddress={"R0":"000",
"R1":"001",
"R2":"010",
"R3":"011",
"R4":"100",
"R5":"101",
"R6":"110",
"FLAGS":"111"}
if reg in regaddress.keys():
return( regaddress[reg])
else:
return 0
def instype(ins,regval=0):
#returns the type of instruction as a string
typetable={"a":["add","sub","mul","xor","or","and","addf","subf"],
"b":["mov","ls","rs","movf"],
"c":["mov","div","not","cmp"],
"d":["ld","st"],
"e":["jmp","jlt","jgt","je"],
"f":["hlt"]}
if regval==1:
return "b"
elif regval==2:
return "c"
for i in typetable.keys():
if ins in typetable[i]:
return i
return 0
def eightbit(num):
#converts a number into an 8 bit binary number and returns it
binaryno=bin(num)[2::]
number=(8-len(binaryno))*"0"+binaryno
return number
from sys import stdin
#f=open("stdin")
linesofcode=stdin.readlines()
#stores all the lines of the input in a list
binarycode=[] #final converted binary lines are stored here
varinitstart=True #flag variable which is true at the beginning where the variables are initiated
halt=False #flag variable stating that the code has not been stopped yet
errors=[] #all the errors in the code are stored here
var={} # all the initiated variables are stored here as keys with values = their address
labels={} # all the initiated labels are stored here as keys with values = their address
variables=0 #stores the total number of variables
mptlines=0 #stores the number of blank lines in the code
for i in linesofcode:
if not i:
mptlines+=1
elif i.split()[0]=="var":
variables+=1
else:
break
store=variables
count=1
#for variable declaration
varadd=len(linesofcode)-variables-mptlines #temp variable for the address of a variable in the code
#the variables are stored after all the instructions
while(variables):
if (not linesofcode[0]):
count+=1
continue
line= linesofcode[0].split()
if (line[0]=="var" and len(line)==2):
if line[1] not in var.keys():
var[line[1]]=eightbit(varadd)
varadd+=1
variables-=1
linesofcode.pop( 0 )
count+=1
continue
else:
errors.append("ERROR at line "+ str(count)+" :Multiple declaration for the same variable found")
count+=1
continue
elif(line[0]=="var"):
errors.append("ERROR at line "+str(count+1)+" :Improper variable declaration")
variables-=1
linesofcode.pop(0)
count+=1
continue
else:
varinitstart=False
break
varinitstart=False
variables=store
#for labels
for line in range (len(linesofcode)):
if len(linesofcode[line].split())==0:
continue
if (linesofcode[line].split()[0][-1]==":"):
if linesofcode[line].split()[0][:len(linesofcode[line].split()[0])-1:] not in labels:
labels[linesofcode[line].split()[0][:len(linesofcode[line].split()[0])-1:]]=eightbit(line)
x=""
for i in range(1,len(linesofcode[line].split())):
x+=linesofcode[line].split()[i]+" "
linesofcode[line]=x
else:
errors.append("ERROR at line "+str(line+variables+1)+" :Multiple declaration for the same label found")
continue
for line in range (len(linesofcode)):
store=linesofcode[line].split()
if (not store):
continue
if (store[0]=="var" and not varinitstart):
errors.append("ERROR at line "+str(line+variables+1)+" :Variable not declared at the beginning")
continue
if (store[0][-1]==":"):
errors.append("ERROR at line "+str(line+variables+1)+" :Use of multiple labels is not supported")
continue
if instype(store[0])==0 and store[0]!="mov":
errors.append("ERROR at line "+str(line+variables+1)+" :Instruction not valid")
continue
if halt:
errors.append("ERROR at line "+str(line+variables+1)+" :Halt(hlt) is not the last instruction.")
continue
if (store[0]=="mov" and len(store)==3 and (store[2][0]=="R" or store[2]=="FLAGS")):
if (regaddress(store[2])==0):
errors.append("ERROR at line "+str (line +variables+1)+" :Register not valid.")
continue
elif(regaddress(store[2]!=0)):
if (regaddress(store[1])==0):
errors.append("ERROR at line "+str(line+variables+1)+" :Register not valid.")
continue
if (regaddress(store[1])=="111"):
errors.append("ERROR at line "+str(line+variables+1)+" :Illegal use of flags.")
continue
binarycode.append(opcode(store[0],2)+"00000"+regaddress(store[1])+regaddress(store[2]))
continue
elif (len(store)==3 and store[0]=="mov" and store[2][0]!="R"):
if regaddress(store[1]==0):
errors.append("ERROR at line "+str(line +variables+1)+" :Register not valid")
continue
if regaddress(store[1])=="111":
errors.append("ERROR at line "+str(line+variables+1)+" :Illegal use of flags")
continue
if store[2][0]!="$":
errors.append("ERROR at line "+str(line+variables+1)+" :Syntax Error")
continue
if (int(store[2][1::]) not in range(0,256)):
errors.append("ERROR at line "+str(line+variables+1)+" :Illegal Immediate Value ")
continue
binarycode.append(opcode(store[0],1)+regaddress(store[1])+eightbit(int(store[2][1::])))
continue
elif store[0]=="mov":
errors.append("ERROR at line "+str(line+variables+1)+" :Wrong syntax used for instruction")
continue
if(instype(store[0])=="a" and len(store)==4):
if regaddress(store[1])==0 or regaddress(store[2])==0 or regaddress(store[3])==0:
errors.append("ERROR at line"+str(line+variables+1)+" :Invalid Register")
continue
if regaddress(store[1])=="111" or regaddress(store[2])=="111" or regaddress(store[3])=="111":
errors.append("ERROR at line "+str(line +variables+1)+" :Illegal use of flags")
continue
binarycode.append(opcode(store[0])+"00"+regaddress(store[1])+regaddress(store[2])+regaddress(store[3]))
continue
elif (instype(store[0])=="a"):
errors.append("ERROR at line "+str(line+variables+1)+" :Wrong syntax used for instruction")
continue
if (instype(store[0])=="b" and len(store)==3 and store[0]!="mov"):
if store[2][0]!="$":
errors.append("ERROR at line "+str(line +variables+1)+" :Syntax error")
continue
try:
if (int(store[2][1::]) not in range (0,256)):
errors.append("ERROR at line "+str(line+variables+1)+" Illegal Immediate value")
continue
except ValueError:
errors.append("ERROR at line "+str(line+variables+1)+" Illegal Immediate value")
continue
if regaddress(store[1]=="111"):
errors.append("ERROR at line "+str(line +variables+1)+" Illegal use of flags")
continue
if (regaddress(store[1])==0):
errors.append("ERROR at line "+str(line+variables+1)+" Invalid register")
continue
binarycode.append(opcode(store[0])+regaddress(store[1])+eightbit(int(store[2][1::])))
continue
elif (instype(store[0])=="b" and store[0]!="mov"):
errors.append("ERROR at line "+str(line+variables+1)+" Wrong syntax used for instruction")
continue
if (instype(store[0])=="c" and len(store)==3 and store[0]!="mov"):
if regaddress(store[1]=="111" or regaddress(store[2])=="111"):
errors.append("ERROR at line "+str(line +variables+1)+" Illegal use of flags")
continue
if (regaddress(store[1])==0 or regaddress(store[2])==0):
errors.append("ERROR at line "+str(line+variables+1)+" Invalid register")
continue
binarycode.append(opcode(store[0])+"00000"+regaddress(store[1])+regaddress(store[2]))
continue
elif (instype(store[0])=="c"):
errors.append("ERROR at line "+str(line+variables+1)+" Wrong syntax used for instruction")
continue
if (instype(store[0])=="d" and len(store)==3):
if regaddress(store[1]=="111"):
errors.append("ERROR at line "+str(line +variables+1)+" Illegal use of flags")
continue
if (regaddress(store[1])==0):
errors.append("ERROR at line "+str(line+variables+1)+" Invalid register")
continue
if store[2] not in var.keys():
if store[2] in labels.keys():
errors.append("ERROR at line "+str (line+variables+1)+" Misuse of label as variable")
continue
else:
errors.append("ERROR at line "+str(line +variables+1)+" Use of undefined variable")
continue
else:
binarycode.append(opcode(store[0])+regaddress(store[1])+var[store[2]])
continue
elif (instype(store[0])=="d"):
errors.append("ERROR at line "+str(line+variables+1)+" Wrong syntax used for instruction")
continue
if (instype(store[0])=="e" and len(store)==2):
if store[1] not in labels.keys():
if store[1] in var.keys():
errors.append("ERROR at line "+str (line+variables+1)+" Misuse of variable as label")
continue
else:
errors.append("ERROR at line "+str(line +variables+1)+" Use of undefined label")
continue
else:
binarycode.append(opcode(store[0])+"000"+labels[store[1]])
continue
elif (instype(store[0])=="e"):
errors.append("ERROR at line "+str(line+variables+1)+" Wrong syntax used for instruction")
continue
if instype(store[0])=="f" and len(store)==1:
binarycode.append(opcode(store[0])+"00000000000")
halt=True
elif instype(store[0])=="f":
errors.append("ERROR at line "+str(line +variables+1)+" Wrong syntax used for instruction")
continue
if (not halt):
errors.append("ERROR: No halt(hlt) instruction found")
if len(binarycode)>256:
errors.append("ERROR: The code length exceeds the maximum capacity (256 lines)")
elif(len(errors)>0):
print(errors[0])
else:
for i in binarycode:
print(i)