forked from dougallj/applegpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassemble.py
45 lines (36 loc) · 1.21 KB
/
assemble.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
import sys
import applegpu
# TODO: labels, file i/o
def begin_encoding(mnem, operand_strings):
for o in applegpu.instruction_descriptors:
fields = o.fields_for_mnem(mnem, operand_strings)
if fields is not None:
return o, fields
raise Exception('unknown mnem %r' % mnem)
def assemble_line(line):
line = line.strip()
operands = []
parts = line.split(None, 1)
mnem = parts[0]
if len(parts) == 1:
operand_strings = []
else:
assert len(parts) == 2
operand_strings = [i.strip() for i in parts[1].split(',')]
idesc, fields = begin_encoding(mnem, operand_strings)
operand_strings = idesc.rewrite_operands_strings(mnem, operand_strings)
for opdesc, opstr in zip(idesc.ordered_operands, operand_strings):
opdesc.encode_string(fields, opstr)
for opdesc in idesc.ordered_operands[len(operand_strings):]:
opdesc.encode_string(fields, '')
n = idesc.encode_fields(fields)
return idesc.to_bytes(n)
if __name__ == '__main__':
if len(sys.argv) > 1:
inp = ' '.join(sys.argv[1:]).split(';')
for line in inp:
asm_bytes = assemble_line(line)
print(asm_bytes.hex().ljust(32), applegpu.disassemble_bytes(asm_bytes))
else:
print('usage: python3 asssemble.py [instruction text separated by semicolons]')
exit(1)