-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisassembler.py
176 lines (144 loc) · 4.47 KB
/
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
176
import sys
# Disassemble the stack based emoji machine code
class Disassembler:
def __init__(self, rom):
self.rom = rom
self.instruction_pointer = 1
def step(self):
cur_ins = self.rom[self.instruction_pointer]
self.instruction_pointer += 1
fn = Disassembler.OPERATIONS.get(cur_ins, None)
if cur_ins[0] == '🖋':
return
if fn is None:
raise RuntimeError("Unknown instruction '{}' at {}".format(
repr(cur_ins), self.instruction_pointer - 1))
else:
fn(self)
def add(self): # done
print(f'add\t(%esp),4(%esp)')
self.pop_out()
def sub(self): # done
print(f'sub\t(%esp),4(%esp)')
self.pop_out()
def if_zero(self):
if self.stack[-1] == 0:
while self.rom[self.instruction_pointer] != '😐':
if self.rom[self.instruction_pointer] in ['🏀', '⛰']:
break
self.step()
else:
self.find_first_endif()
self.instruction_pointer += 1
def if_not_zero(self):
if self.stack[-1] != 0:
while self.rom[self.instruction_pointer] != '😐':
if self.rom[self.instruction_pointer] in ['🏀', '⛰']:
break
self.step()
else:
self.find_first_endif()
self.instruction_pointer += 1
def find_first_endif(self):
while self.rom[self.instruction_pointer] != '😐':
self.instruction_pointer += 1
def jump_to(self):
marker = self.rom[self.instruction_pointer]
if marker[0] != '💰':
print('Incorrect symbol : ' + marker[0])
raise SystemExit()
marker = '🖋' + marker[1:]
self.instruction_pointer = self.rom.index(marker) + 1
def jump_top(self):
self.instruction_pointer = self.stack.pop()
def exit(self):
print('\nDone.')
raise SystemExit()
def print_top(self):
sys.stdout.write(chr(self.stack.pop()))
sys.stdout.flush()
def push(self): # donet
if self.rom[self.instruction_pointer] == '🥇':
register = '%eax'
elif self.rom[self.instruction_pointer] == '🥈':
register = '%ebx'
else:
raise RuntimeError('Unknown instruction {} at position {}'.format(
self.rom[self.instruction_pointer], str(self.instruction_pointer)))
print(f'push\t{register}')
self.instruction_pointer += 1
def pop(self):
if self.rom[self.instruction_pointer] == '🥇':
self.accumulator1 = self.stack.pop()
elif self.rom[self.instruction_pointer] == '🥈':
self.accumulator2 = self.stack.pop()
else:
raise RuntimeError('Unknown instruction {} at position {}'.format(
self.rom[self.instruction_pointer], str(self.instruction_pointer)))
self.instruction_pointer += 1
def pop_out(self): # done
print('add\t$4,%esp')
def load(self): # done
num = 0
if self.rom[self.instruction_pointer] == '🥇':
register = '%eax'
elif self.rom[self.instruction_pointer] == '🥈':
register = '%ebx'
else:
raise RuntimeError('Unknown instruction {} at position {}'.format(
self.rom[self.instruction_pointer], str(self.instruction_pointer)))
self.instruction_pointer += 1
while self.rom[self.instruction_pointer] != '✋':
num = num * 10 + (ord(self.rom[self.instruction_pointer][0]) - ord('0'))
self.instruction_pointer += 1
print(f'mov\t${num},{register}')
self.instruction_pointer += 1
def clone(self): # done
print('sub\t$4,%esp')
print('mov\t4(%esp),(%esp)')
def multiply(self):
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(b * a)
def divide(self):
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(b // a)
def modulo(self):
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(b % a)
def xor(self):
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(b ^ a)
OPERATIONS = {
'🍡': add,
'🤡': clone,
'📐': divide,
'😲': if_zero,
'😄': if_not_zero,
'🏀': jump_to,
'🚛': load,
'📬': modulo,
'⭐': multiply,
'🍿': pop,
'📤': pop_out,
'🎤': print_top,
'📥': push,
'🔪': sub,
'🌓': xor,
'⛰': jump_top,
'⌛': exit
}
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Missing program')
raise SystemExit()
with open(sys.argv[1], 'r') as f:
print('Disassembling ....')
all_ins = ['']
all_ins.extend(f.read().split())
dis = Disassembler(all_ins)
while dis.instruction_pointer < len(dis.rom):
dis.step()