-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc3-disassembler.py
executable file
·175 lines (154 loc) · 5.51 KB
/
lc3-disassembler.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
#!/bin/python3
import sys, argparse, pathlib
def parse_hex(x):
if x[0] == 'x': x = x[1:]
return int(x, base=16)
def parse_2scompl(bits):
if bits[0] == '1':
return -(pow(2,len(bits)) - int(bits, 2))
else:
return int(bits, 2)
def decode_hex(x):
return hex(int(x, 2))[1:]
def decode_int(bits):
return '#{}'.format(parse_2scompl(bits))
def decode_reg(bits):
return 'R{}'.format(int(bits, 2))
def decode_conditions(bits):
r = ''
if bits[0] == '1':
r += 'n'
if bits[1] == '1':
r += 'z'
if bits[2] == '1':
r += 'p'
return r
def decode_pcoffset(n, bits):
return n + parse_2scompl(bits) + 1
class Asm:
def __init__(self, location, opcode, operands = None, label = None):
self.location = location
self.opcode = opcode
self.operands = operands
self.label = label
def decode_instruction(n, i):
if len(i) != 16:
print("Invalid instruction length {} of '{}' on location {}!".format(len(i), i, n))
exit()
match i[0:4]:
case '0001':
if i[10] == '1':
return Asm(n, 'ADD', [decode_reg(i[4:7]), decode_reg(i[7:10]), decode_int(i[11:16])])
else:
return Asm(n, 'ADD', [decode_reg(i[4:7]), decode_reg(i[7:10]), decode_reg(i[13:16])])
case '0101':
if i[10] == '1':
return Asm(n, 'AND', [decode_reg(i[4:7]), decode_reg(i[7:10]), decode_int(i[11:16])])
else:
return Asm(n, 'AND', [decode_reg(i[4:7]), decode_reg(i[7:10]), decode_reg(i[13:16])])
case '1001':
return Asm(n, 'NOT', [decode_reg(i[4:7]), decode_reg(i[7:10])])
case '0000':
return Asm(n, 'BR' + decode_conditions(i[4:7]), label=decode_pcoffset(n, i[7:16]))
case '1100':
reg = decode_reg(i[7:10])
if reg == 'R7':
return Asm(n, 'RET')
else:
return Asm(n, 'JMP', [reg])
case '0100':
if i[4] == '1':
return Asm(n, 'JSR', label=decode_pcoffset(n, i[5:16]))
else:
return Asm(n, 'JSRR', [decode_reg(i[7:10])])
case '1000':
return Asm('RTI')
case '1110':
return Asm(n, 'LEA', [decode_reg(i[4:7])], decode_pcoffset(n, i[7:16]))
case '0010':
return Asm(n, 'LD', [decode_reg(i[4:7])], decode_pcoffset(n, i[7:16]))
case '1010':
return Asm(n, 'LDI', [decode_reg(i[4:7])], decode_pcoffset(n, i[7:16]))
case '0110':
return Asm(n, 'LDR', [decode_reg(i[4:7]), decode_reg(i[7:10]), decode_int(i[10:16])])
case '0011':
return Asm(n, 'ST', [decode_reg(i[4:7])], decode_pcoffset(n, i[7:16]))
case '1011':
return Asm(n, 'STI', [decode_reg(i[4:7])], decode_pcoffset(n, i[7:16]))
case '0111':
return Asm(n, 'STR', [decode_reg(i[4:7]), decode_reg(i[7:10]), decode_int(i[10:16])])
case '1111':
trapvec = decode_hex(i[8:16])
shorthand = None
match trapvec:
case 'x20':
shorthand = 'GETC'
case 'x21':
shorthand = 'OUT'
case 'x22':
shorthand = 'PUTS'
case 'x23':
shorthand = 'IN'
case 'x24':
shorthand = 'PUTSP'
case 'x25':
shorthand = 'HALT'
if shorthand:
return Asm(n, shorthand)
else:
return Asm(n, 'TRAP', [trapvec])
case _:
print("Invalid opcode or instruction '{}' on location {}!".format(i, n))
exit()
def fill_instruction(n, i):
return Asm(n, '.FILL', [decode_hex(i)])
def disassemble(instructions, auto_fill):
asm_lines = []
labels = set()
asm_lines.append(Asm(None, '.ORIG', ['x3000']))
n = 0
after_halt = False
for instr in instructions:
if not after_halt:
asm = decode_instruction(n, instr)
if asm.label:
labels.add(asm.label)
asm_lines.append(asm)
if asm.opcode == 'HALT' and auto_fill:
after_halt = True
else:
asm_lines.append(fill_instruction(n, instr))
n += 1
asm_lines.append(Asm(None, '.END'))
n = 0
label_names = {}
for l in sorted(list(labels)):
label_names[l] = "LABEL{}".format(n)
n += 1
for asm in asm_lines:
print(label_names[asm.location].ljust(8) if asm.location in label_names else 8 * ' ', end='')
print(asm.opcode.ljust(6), end='')
params = []
if asm.operands:
params.extend(asm.operands)
if asm.label:
params.append(label_names[asm.label])
if len(params) > 0:
print(' ', ', '.join(params), end='')
print('')
parser = argparse.ArgumentParser(
prog = 'LC-3 Disassembler',
description = 'Disassemble LC-3 machine code',
epilog = 'By Rijk van Putten <rijk@rijkvp.nl>')
parser.add_argument('-f', '--fill', action='store_true', help='instructions after halt are interpreted as .FILL')
args = parser.parse_args()
instructions = []
for line in sys.stdin:
if line.strip() == '':
continue
elif line[0] == 'x':
instr_val = parse_hex(line)
instructions.append("{0:b}".format(instr_val).zfill(16))
else:
instructions.append(line.strip())
disassemble(instructions, args.fill)