-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyasm.py
619 lines (452 loc) · 16.2 KB
/
pyasm.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
import opcodes
import functions
import sys
import os.path
# check for arguments
if len(sys.argv) < 2:
functions.error("You need to specify an output file\nUsage: pyasm.py [output file]")
elif len(sys.argv) > 2:
functions.error("You need to specify only an output file\nUsage: pyasm.py [output file]")
else:
pass
output_file = open(sys.argv[1], "wb")
print("Welcome to Pyasm!")
print("Press enter to exit, and remember, use 4 digits when moving to immediant register!")
## segment and offset
segment = 0000
offset = 0000
## dictionary to save labels
labels = {}
## flags
is_label = False
try:
while True:
asm_input = input(str(segment) + ":" + str(offset) + " ")
# check if key is enter
if asm_input == "":
print("Program terminated successfully.")
exit(0)
asm_input_tokenized = asm_input.split(" ")
# set flag to None to avoid bugs
is_label = False
# checks if it is a label the input
for i in asm_input:
if i == ":":
label_without_colon = asm_input.replace(":", "")
labels[label_without_colon] = offset
is_label = True # set is_label flag
break
else:
pass
# if its a label, the for loop above sets is_label variable to 1
if is_label == True:
continue
# cli -> clear interrupt flag
elif asm_input_tokenized[0].lower() == "cli" and len(asm_input_tokenized) == 1:
result = functions.cli(opcodes.cli["cli"])
offset+=1
output_file.write(result)
# db -> declare byte
elif asm_input_tokenized[0].lower() == "db":
# the for loop is to make raw all the bytes after DB
try:
for i in range(0, len(asm_input_tokenized) - 1):
j = i + 1
result = bytearray(bytes([int(asm_input_tokenized[j], 16)]))
output_file.write(result)
offset+=1 # increase offset
except ValueError:
print("Error")
# incbin -> include binary file
elif asm_input_tokenized[0].lower() == "#incbin" and len(asm_input_tokenized) == 2:
if not os.path.exists(str(asm_input_tokenized[1])):
print("Error")
elif os.path.isdir(str(asm_input_tokenized[1])):
print("Error")
else:
input_file = open(str(asm_input_tokenized[1]), "rb")
input_file_content = input_file.read()
output_file.write(input_file_content)
input_file_len = bytearray(bytes(input_file_content))
input_file_len = len(input_file_len)
for i in range(0, input_file_len):
offset += 1
input_file.close()
# fill -> fill with x y times
elif asm_input_tokenized[0].lower() == "fill" and len(asm_input_tokenized) == 3:
try:
value = int(asm_input_tokenized[1])
times = int(asm_input_tokenized[2])
for i in range(0, times):
offset+=1 # increase offset
result = bytearray(bytes([value]))
offset+=1
output_file.write(result)
except ValueError:
print("Error")
# fillu -> fill with x until the file is y bytes long
elif asm_input_tokenized[0].lower() == "fillu" and len(asm_input_tokenized) == 3:
try:
value = int(asm_input_tokenized[1])
until = int(asm_input_tokenized[2])
for i in range(0, until):
if offset == until:
break
result = bytearray(bytes([value]))
offset+=1
output_file.write(result)
except ValueError:
print("Error")
# START CHECKING DECREASE
#
# START CHECKING FOR 8 BIT REGISTERS
elif asm_input_tokenized[0].lower() == "dec" and len(asm_input_tokenized) == 2:
## START CHECKING FOR 8 BIT REGISTERS
##
if asm_input_tokenized[1].lower() == "al":
result = functions.decrease(opcodes.dec_imm8["al"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bl":
result = functions.decrease(opcodes.dec_imm8["bl"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "cl":
result = functions.decrease(opcodes.dec_imm8["cl"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "dl":
result = functions.decrease(opcodes.dec_imm8["dl"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "ah":
result = functions.decrease(opcodes.dec_imm8["ah"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bh":
result = functions.decrease(opcodes.dec_imm8["bh"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "ch":
result = functions.decrease(opcodes.dec_imm8["ch"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "dh":
result = functions.decrease(opcodes.dec_imm8["dh"])
offset+=2
output_file.write(result)
##
## END CHECKING FOR 8 BIT REGISTERS
## START CHECKING FOR 16 BIT REGISTERS
##
elif asm_input_tokenized[1].lower() == "ax":
result = functions.decrease(opcodes.dec_imm16["ax"])
# Opcode for decreasing 16 bit register is one byte
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bx":
result = functions.decrease(opcodes.dec_imm16["bx"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "cx":
result = functions.decrease(opcodes.dec_imm16["cx"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "dx":
result = functions.decrease(opcodes.dec_imm16["dx"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "sp":
result = functions.decrease(opcodes.dec_imm16["sp"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bp":
result = functions.decrease(opcodes.dec_imm16["bp"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "si":
result = functions.decrease(opcodes.dec_imm16["si"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "di":
result = functions.decrease(opcodes.dec_imm16["di"])
offset+=1
output_file.write(result)
else:
print("Error")
#
# END CHECKING DECREASE
# START CHECKING HLT
elif asm_input_tokenized[0].lower() == "hlt" and len(asm_input_tokenized) == 1:
result = functions.hlt(opcodes.hlt["hlt"])
offset+=1
output_file.write(result)
# START CHECKING INCREASE
#
elif asm_input_tokenized[0].lower() == "inc" and len(asm_input_tokenized) == 2:
## START CHECKING FOR 8 BIT REGISTERS
##
if asm_input_tokenized[1].lower() == "al":
result = functions.increase(opcodes.inc_imm8["al"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bl":
result = functions.increase(opcodes.inc_imm8["bl"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "cl":
result = functions.increase(opcodes.inc_imm8["cl"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "dl":
result = functions.increase(opcodes.inc_imm8["dl"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "ah":
result = functions.increase(opcodes.inc_imm8["ah"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bh":
result = functions.increase(opcodes.inc_imm8["bh"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "ch":
result = functions.increase(opcodes.inc_imm8["ch"])
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "dh":
result = functions.increase(opcodes.inc_imm8["dh"])
offset+=2
output_file.write(result)
##
## END CHECKING FOR 8 BIT REGISTERS
## START CHECKING FOR 16 BIT REGISTERS
##
# note that the opcode for inc r16 is one byte
elif asm_input_tokenized[1].lower() == "ax":
result = functions.increase(opcodes.inc_imm16["ax"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bx":
result = functions.increase(opcodes.inc_imm16["bx"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "cx":
result = functions.increase(opcodes.inc_imm16["cx"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "dx":
result = functions.increase(opcodes.inc_imm16["dx"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "sp":
result = functions.increase(opcodes.inc_imm16["sp"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bp":
result = functions.increase(opcodes.inc_imm16["bp"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "si":
result = functions.increase(opcodes.inc_imm16["si"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[1].lower() == "di":
result = functions.increase(opcodes.inc_imm16["di"])
offset+=1
output_file.write(result)
else:
print("Error")
#
# END CHECKING INCREASE
# START CHECKING INTERRUPTS
#
elif asm_input_tokenized[0].lower() == "int" and len(asm_input_tokenized) == 2:
# try statement if the value given is incorrect
try:
result = functions.interrupt(opcodes.interrupt["int_imm8"], asm_input_tokenized[1])
if result == None:
continue
offset+=2
output_file.write(result)
except ValueError:
print("Error")
elif asm_input_tokenized[0].lower() == "int3" and len(asm_input_tokenized) == 3:
result = bytearray(opcodes.interrupt["int3"])
offset+=1
output_file.write(result)
elif asm_input_tokenized[0].lower() == "into":
result = bytearray(opcodes.interrupt["into"])
offset+=1
output_file.write(result)
#
# END CHECKING INTERRUPTS
# START CHECKING JMP
#
elif asm_input_tokenized[0].lower() == "jmp":
## START CHECKING LABELS
##
# check if label exists
if asm_input_tokenized[1] in labels:
# check offset
# EB's range is from -128 to 127
if labels[asm_input_tokenized[1]] < offset:
distance = offset - labels[asm_input_tokenized[1]]
if distance < 127 or distance > -128:
distance = 254 - distance # unsigned range of EB is 255, but EB <bytes> are 2 bytes, so thats why we substact 254 (discounting <bytes>).
result = functions.jump(opcodes.jmp["jmp"], str(distance))
offset+=2
output_file.write(result)
else:
print("Error")
else:
print("Error")
##
## END CHECKING LABELS
else:
print("Error")
#
# END CHECKING JMP
# START CHECKING FOR MOV
#
elif asm_input_tokenized[0].lower() == "mov":
## START CHECKING FOR IMMEDIATE ADDRESSES
##
### START CHECKING 8BIT IMMEDIATE ADDRESSES
###
if asm_input_tokenized[1].lower() == "al," and len(asm_input_tokenized) == 3:
result = functions.mov(opcodes.mov_r8_imm8["al"], asm_input_tokenized[2])
# if given as address an invalid number the function returns None
if result == None:
continue
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bl," and len(asm_input_tokenized) == 3:
result = functions.mov(opcodes.mov_r8_imm8["bl"], asm_input_tokenized[2])
if result == None:
continue
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "cl," and len(asm_input_tokenized) == 3:
result = functions.mov(opcodes.mov_r8_imm8["cl"], asm_input_tokenized[2])
if result == None:
continue
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "dl," and len(asm_input_tokenized) == 3:
result = functions.mov(opcodes.mov_r8_imm8["dl"], asm_input_tokenized[2])
if result == None:
continue
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "ah," and len(asm_input_tokenized) == 3:
result = functions.mov(opcodes.mov_r8_imm8["ah"], asm_input_tokenized[2])
if result == None:
continue
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bh," and len(asm_input_tokenized) == 3:
result = functions.mov(opcodes.mov_r8_imm8["bh"], asm_input_tokenized[2])
if result == None:
continue
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "ch," and len(asm_input_tokenized) == 3:
result = functions.mov(opcodes.mov_r8_imm8["ch"], asm_input_tokenized[2])
if result == None:
continue
offset+=2
output_file.write(result)
elif asm_input_tokenized[1].lower() == "dh," and len(asm_input_tokenized) == 3:
result = functions.mov(opcodes.mov_r8_imm8["dh"], asm_input_tokenized[2])
if result == None:
continue
offset+=2
output_file.write(result)
###
### END CHECKING 8BIT IMMEDIATE ADDRESSES
### STARTING CHECKING 16BIT IMMEDIATE ADDRESSES
elif asm_input_tokenized[1].lower() == "ax," and len(asm_input_tokenized) == 4:
# ax and all 16 bit registers = lower part and higher part, so we do functions.mov() twice
result = functions.mov(opcodes.mov_r16_imm16["ax"], asm_input_tokenized[3])
if result == None:
continue
result = functions.mov(result, asm_input_tokenized[2])
if result == None:
continue
offset+=3
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bx," and len(asm_input_tokenized) == 4:
result = functions.mov(opcodes.mov_r16_imm16["bx"], asm_input_tokenized[2])
if result == None:
continue
result = functions.mov(result, asm_input_tokenized[2])
if result == None:
continue
offset+=3
output_file.write(result)
elif asm_input_tokenized[1].lower() == "cx," and len(asm_input_tokenized) == 4:
result = functions.mov(opcodes.mov_r16_imm16["cx"], asm_input_tokenized[2])
if result == None:
continue
result = functions.mov(result, asm_input_tokenized[2])
if result == None:
continue
offset+=3
output_file.write(result)
elif asm_input_tokenized[1].lower() == "dx," and len(asm_input_tokenized) == 4:
result = functions.mov(opcodes.mov_r16_imm16["dx"], asm_input_tokenized[2])
if result == None:
continue
result = functions.mov(result, asm_input_tokenized[2])
if result == None:
continue
offset+=3
output_file.write(result)
elif asm_input_tokenized[1].lower() == "sp," and len(asm_input_tokenized) == 4:
result = functions.mov(opcodes.mov_r16_imm16["sp"], asm_input_tokenized[2])
if result == None:
continue
result = functions.mov(result, asm_input_tokenized[2])
if result == None:
continue
offset+=3
output_file.write(result)
elif asm_input_tokenized[1].lower() == "bp," and len(asm_input_tokenized) == 4:
result = functions.mov(opcodes.mov_r16_imm16["bp"], asm_input_tokenized[2])
if result == None:
continue
result = functions.mov(result, asm_input_tokenized[2])
if result == None:
continue
offset+=3
output_file.write(result)
elif asm_input_tokenized[1].lower() == "si," and len(asm_input_tokenized) == 4:
result = functions.mov(opcodes.mov_r16_imm16["si"], asm_input_tokenized[2])
if result == None:
continue
result = functions.mov(result, asm_input_tokenized[2])
if result == None:
continue
offset+=3
output_file.write(result)
elif asm_input_tokenized[1].lower() == "di," and len(asm_input_tokenized) == 4:
result = functions.mov(opcodes.mov_r16_imm16["di"], asm_input_tokenized[2])
if result == None:
continue
result = functions.mov(result, asm_input_tokenized[2])
if result == None:
continue
offset+=3
output_file.write(result)
else:
print("Error")
###
### END CHECKING 16BIT IMMEDIATE ADDRESSES
##
## END CHECKING FOR IMMEDIATE ADDRESSES
#
# END CHECKING MOV
except EOFError:
output_file.close()
exit(0)