-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsimple-vm.py
executable file
·159 lines (126 loc) · 3.8 KB
/
simple-vm.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
#!/usr/bin/env python
def match(opcodes, opcode):
assert opcodes[0] == opcode
opcodes.pop(0)
def parse_uint32_l(opcodes):
return hex(opcodes.pop(0) + \
(opcodes.pop(0) << 8) + \
(opcodes.pop(0) << 16) + \
(opcodes.pop(0) << 24))
def return_vm(opcodes, ret_list):
match(opcodes, 0)
ret_list.append("return " + parse_uint32_l(opcodes))
def jmp_vm(opcodes, ret_list):
match(opcodes, 1)
ret_list.append("jmp " + parse_uint32_l(opcodes))
def assign_vm(opcodes, ret_list):
match(opcodes, 2)
ret_list.append("[" + parse_uint32_l(opcodes) + "] = " + parse_uint32_l(opcodes))
def set_cond_vm(opcodes, ret_list):
match(opcodes, 3)
ret_list.append("cond = byte [" + parse_uint32_l(opcodes) + "]")
def get_cond_vm(opcodes, ret_list):
match(opcodes, 4)
ret_list.append("byte [" + parse_uint32_l(opcodes) + "] = (byte)cond")
def set_global_vm(opcodes, ret_list):
match(opcodes, 5)
ret_list.append("global = byte [" + parse_uint32_l(opcodes) + "]")
def get_global_vm(opcodes, ret_list):
match(opcodes, 6)
ret_list.append("[" + parse_uint32_l(opcodes) + "] = global")
def cond_plus_global(opcodes, ret_list):
match(opcodes, 7)
ret_list.append("cond += global")
def some_logic_oper_vm(opcodes, ret_list):
match(opcodes, 8)
ret_list.append("cond = ~(global & cond)")
def get_char(opcodes, ret_list):
match(opcodes, 0xa)
ret_list.append("cond = getchar()")
def put_char(opcodes, ret_list):
match(opcodes, 0xb)
ret_list.append("putchar(cond)")
def cond_jmp(opcodes, ret_list):
match(opcodes, 0xc)
ret_list.append("[" + parse_uint32_l(opcodes) + "] ? jmp " + parse_uint32_l(opcodes) + " and --")
def cond_plus_1(opcodes, ret_list):
match(opcodes, 0xd)
ret_list.append("cond += 1")
def global_plus_1(opcodes, ret_list):
match(opcodes, 0xe)
ret_list.append("global += 1")
def cond_eq_global(opcodes, ret_list):
match(opcodes, 0xf)
ret_list.append("cond = global")
def global_eq_cond(opcodes, ret_list):
match(opcodes, 0x10)
ret_list.append("global = cond")
def plus(opcodes, ret_list):
match(opcodes, 0x11)
ret_list.append("cond += " + parse_uint32_l(opcodes))
def assign_reg_vm(opcodes, ret_list):
match(opcodes, 0x12)
ret_list.append("cond = byte [global]")
def assign_creg_vm(opcodes, ret_list):
match(opcodes, 0x13)
ret_list.append("cond = byte [cond]")
def asssign_cond(opcodes, ret_list):
match(opcodes, 0x14)
ret_list.append("cond = " + parse_uint32_l(opcodes))
def asssign_global(opcodes, ret_list):
match(opcodes, 0x15)
ret_list.append("global = " + parse_uint32_l(opcodes))
def gobal_ref_eq_cond(opcodes, ret_list):
match(opcodes, 0x16)
ret_list.append("[global] = cond")
def cond_minus_global(opcodes, ret_list):
match(opcodes, 0x17)
ret_list.append("cond -= global")
def cond_jmp_based_on_cond(opcodes, ret_list):
match(opcodes, 0x18)
ret_list.append("cond ? jmp " + parse_uint32_l(opcodes))
def nop(opcodes, ret_list):
opcodes.pop(0)
ret_list.append("nop")
opcode_dic = {
0 : return_vm,
1 : jmp_vm,
2 : assign_vm,
3 : set_cond_vm,
4 : get_cond_vm,
5 : set_global_vm,
6 : get_global_vm,
7 : cond_plus_global,
8 : some_logic_oper_vm,
0xa : get_char,
0xb : put_char,
0xc : cond_jmp,
0xd : cond_plus_1,
0xe : global_plus_1,
0xf : cond_eq_global,
0x10 : global_eq_cond,
0x11 : plus,
0x12 : assign_reg_vm,
0x13 : assign_creg_vm,
0x14 : asssign_cond,
0x15 : asssign_global,
0x16 : gobal_ref_eq_cond,
0x17 : cond_minus_global,
0x18 : cond_jmp_based_on_cond
}
def disassemble_vm(opcodes):
total_len = len(opcodes)
ret_list = []
while len(opcodes) > 0:
ret_list.append(hex(total_len - len(opcodes) + 0x30))
if opcodes[0] in opcode_dic:
opcode_dic[opcodes[0]](opcodes, ret_list)
else:
nop(opcodes, ret_list)
print "\n".join(ret_list)
print "\n\n"
with open('./p.bin', 'r') as content_file:
content = content_file.read()
content = map(ord, list(content[0x30:0xe2]))
print content
disassemble_vm(content)