-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDroneAssembley.py
432 lines (334 loc) · 10.4 KB
/
DroneAssembley.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
class COMMENT:
def __init__(self, param):
self.param = param
class LABEL:
def __init__(self, label):
assert isinstance(label, str)
self.label = label
class MACRO:
def __init__(self):
pass
class SYM:
def __init__(self, name, value):
self.name = name
self.value = value
class START(MACRO):
def __init__(self, start):
self.startLabel = start
def expand(self):
return [
COMMENT("Reads the entry point and jumps to it"),
SYM("__entry", "[2900]"),
SYM("__start", self.startLabel),
LDA("__entry"),
ADDA("__start"),
STA("[2901]"),
JGE("[2901]"),
]
def __str__(self):
return "START"
class JMP(MACRO):
def __init__(self, label):
self.label = label
def expand(self):
return [
COMMENT(str(self)),
LDA("0"),
JGE(self.label),
]
def __str__(self):
return "JUMP TO: " + self.label
class CHANGE_START(MACRO):
def __init__(self, start):
self.startLabel = start
def expand(self):
return [
COMMENT("Change the entry point next run to: " + self.startLabel),
LDA(self.startLabel),
SUBA("__start"),
STA("__entry"),
]
def __str__(self):
return "CHANGE_START " + self.startLabel
class IFLESS(MACRO):
def __init__(self, a, b, jmp):
self.a = a
self.b = b
self.jmp = jmp
def expand(self):
return [
COMMENT("if " + self.a + " < " + self.b + " (else jump to: " + self.jmp + ")"),
LDA(self.a),
SUBA(self.b),
JGE(self.jmp),
COMMENT("then:")
]
def __str__(self):
return "IFLESS: " + str(self.a) + " < " + str(self.b) + " else jump to " + str(self.jmp)
class IFEQ(MACRO):
code = 0
def __init__(self, a, b, jmp):
self.a = a
self.b = b
self.jmp = jmp
def expand(self):
IFEQ.code += 1
LabelAGEB = "AGEB-" + str(IFEQ.code)
LabelEqual = "EQUAL-" + str(IFEQ.code)
return [
COMMENT(str(self)),
LDA(self.a),
SUBA(self.b),
JGE(LabelAGEB),
LDA("0"),
JGE(self.jmp),
LABEL(LabelAGEB),
LDA(self.b),
SUBA(self.a),
JGE(LabelEqual),
LDA("0"),
JGE(self.jmp),
LABEL(LabelEqual),
COMMENT("then:")
]
def __str__(self):
return "if " + self.a + " = " + self.b + " (else jump to: " + self.jmp + ")"
class IFNEQ(MACRO):
code = 0
def __init__(self, a, b, jmp):
self.a = a
self.b = b
self.jmp = jmp
def expand(self):
IFNEQ.code += 1
LabelAGEB = "AGEBDIF-" + str(IFEQ.code)
LabelDiff = "DIFF-" + str(IFEQ.code)
return [
COMMENT(str(self)),
LDA(self.a),
SUBA(self.b),
JGE(LabelAGEB),
LDA("0"),
JGE(LabelDiff),
LABEL(LabelAGEB),
LDA(self.b),
SUBA(self.a),
JGE(self.jmp),
LABEL(LabelDiff),
COMMENT("then:")
]
def __str__(self):
return "if " + self.a + " != " + self.b + " (else jump to: " + self.jmp + ")"
class MOVE_HLT(MACRO):
def __init__(self, direction):
self.direction = direction
def expand(self):
return [
COMMENT("MOVE " + self.direction + " and HALT"),
LDA(self.direction),
STA("[0]"),
HLT()]
def __str__(self):
return "MOVE_HLT: " + str(self.direction)
class LET(MACRO):
def __init__(self, left, right):
self.left = str(left)
self.right = str(right)
def expand(self):
return [
COMMENT("LET " + self.left + " = " + self.right),
LDA(self.right),
STA(self.left)
]
def __str__(self):
return "LET: " + str(self.left) + " = " + str(self.right)
class LETSUB(MACRO):
def __init__(self, left, a, b):
self.left = str(left)
self.a = str(a)
self.b = str(b)
def expand(self):
return [
COMMENT("LET " + self.left + " = " + self.a + " - " + self.b),
LDA(self.a),
SUBA(self.b),
STA(self.left)
]
def __str__(self):
return "LETSUB: " + str(self.left) + " = " + str(self.a) + " - " + str(self.b)
class LETADD(MACRO):
def __init__(self, left, a, b):
self.left = str(left)
self.a = str(a)
self.b = str(b)
def expand(self):
return [
COMMENT("LET " + self.left + " = " + self.a + " + " + self.b),
LDA(self.a),
ADDA(self.b),
STA(self.left)
]
def __str__(self):
return "LETADD: " + str(self.left) + " = " + str(self.a) + " + " + str(self.b)
class ABS(MACRO):
code = 0
def __init__(self, left, right):
self.left = left
self.right = right
def expand(self):
ABS.code += 1
label = "ABSEND-" + str(ABS.code)
return [
COMMENT("ABS " + self.right + " = ABS(" + str(self.left) + ")"),
LDA(self.right),
JGE(label),
LDA(0),
SUBA(self.right),
LABEL(label),
STA(self.left)
]
def __str__(self):
return "ABS " + self.right + " = ABS(" + str(self.left) + ")"
class INC(MACRO):
def __init__(self, left, a):
self.left = str(left)
self.a = str(a)
def expand(self):
return [
COMMENT("INC: " + str(self.left) + " += " + str(self.a)),
LDA(self.left),
ADDA(self.a),
STA(self.left)
]
def __str__(self):
return "INC: " + str(self.left) + " += " + str(self.a)
class LETPTR(MACRO):
def __init__(self, left, right):
self.left = str(left)
self.right = str(right)
def expand(self):
return [
COMMENT("LET PTR [" + self.left + "] = " + self.right),
LDN(self.left),
LDA(self.right),
STA("[N]")
]
def __str__(self):
return "LET PTR [" + self.left + "] = " + self.right
class GETPTR(MACRO):
def __init__(self, left, right):
self.left = str(left)
self.right = str(right)
def expand(self):
return [
COMMENT("GET PTR " + self.left + " = [" + self.right + "]"),
LDN(self.right),
LDA("[N]"),
STA(self.left)
]
def __str__(self):
return "GET PTR " + self.left + " = [" + self.right + "]"
class Instruction:
def __init__(self, name, param):
self.param = str(param)
self.humanparam = self.param
self.name = name
self.macro = None
def __str__(self):
return self.name + ' ' + str(self.param)
class LDN(Instruction):
def __init__(self, param):
Instruction.__init__(self, 'LDN', param)
class STA(Instruction):
def __init__(self, param):
Instruction.__init__(self, 'STA', param)
class LDA(Instruction):
def __init__(self, param):
Instruction.__init__(self, 'LDA', param)
class ADDA(Instruction):
def __init__(self, param):
Instruction.__init__(self, 'ADDA', param)
class SUBA(Instruction):
def __init__(self, param):
Instruction.__init__(self, 'SUBA', param)
class JGE(Instruction):
def __init__(self, param):
Instruction.__init__(self, 'JGE', param)
class HLT(Instruction):
def __init__(self, ):
Instruction.__init__(self, 'HLT', '')
class DroneAssembler:
program = []
def __init__(self, program):
self.program = program
def compile(self):
self.__expandMacros()
self.__number()
self.__resolveSymbols()
self.__resolveJumps()
return self
def spit(self):
for line in self.program:
if isinstance(line, Instruction):
print str(line) + "\t\t// " + str(line.lineno)
elif isinstance(line, LABEL):
print "// LABEL: " + line.label
elif isinstance(line, COMMENT):
print "// " + line.param
return self
def save(self, fileName):
f = open(fileName, 'w+')
for line in self.program:
if isinstance(line, Instruction):
f.write(str(line) + "\t\t// " + str(line.lineno) + '\n')
elif isinstance(line, LABEL):
f.write("// LABEL: " + line.label + '\n')
elif isinstance(line, COMMENT):
f.write("// " + line.param + '\n')
return self
@property
def assembly(self):
return [line for line in self.program if isinstance(line, Instruction)]
def __resolveSymbols(self):
symbols = {}
for line in self.program:
if isinstance(line, SYM):
symbols[line.name] = line.value
for line in self.program:
if isinstance(line, Instruction):
if line.param in symbols:
line.param = symbols[line.param]
def __expandMacros(self):
self.program = self.__expandMacrosDeep(self.program)
def __expandMacrosDeep(self, sequence):
expanded = []
for line in sequence:
if isinstance(line, MACRO):
macroprog = self.__expandMacrosDeep(line.expand())
for instr in macroprog:
instr.macro = line
expanded.extend(macroprog)
else:
expanded.append(line)
return expanded
def __resolveJumps(self):
acclabels = []
labels = {}
for line in self.program:
if isinstance(line, LABEL):
acclabels.append(line)
elif isinstance(line, Instruction):
if len(acclabels) > 0:
for label in acclabels:
label.lineno = line.lineno
labels[label.label] = label.lineno
acclabels = []
for line in self.program:
if isinstance(line, Instruction) and line.param in labels:
line.param = labels[line.param]
def __number(self):
count = 0
for line in self.program:
if isinstance(line, Instruction):
line.lineno = count
count += 1