-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm.asm
365 lines (308 loc) · 4.91 KB
/
vm.asm
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
; This is VM without top of stack in separate register
; esp - data stack pointer
; ebp - return stacl pointer
; esi - instruction pointer
;%macro donext 0
; lodsd
; bt ax, 0
; jnc near vmdolist
; jmp [eax + vmops]
;%endmacro
;%macro donext 0
; lodsd
; test eax, 1
; jz near vmdolist
; jmp [eax + vmops]
;%endmacro
;%macro donext 0
; mov eax, [esi]
; add esi, 4
; bt ax, 0
; jnc near vmdolist
; jmp [eax + vmops]
;%endmacro
%macro donext 0
mov eax, [esi]
add esi, 4
test eax, 1
jz near vmdolist
jmp [eax + vmops]
%endmacro
extern rp
extern ip
extern dsp
extern exit
extern getchar
extern putchar
global virtual_machine
section .text
virtual_machine:
cld
mov esi, [ip]
mov ebp, [rp]
mov esp, [dsp]
donext
;vmdolist:
; xchg ebp, esp
; push esi
; add esi, eax
; xchg ebp, esp
; donext
vmdolist:
sub ebp, 4
mov [ebp], esi
add esi, eax
donext
vmnoop:
donext
vmexit:
mov esi, [ebp]
add ebp, 4
donext
;vmexit:
; xchg esp, ebp
; pop esi
; xchg esp, ebp
; donext
vmlit:
lodsd
push eax
donext
;vmlit:
; push dword [esi]
; add esi, 4
; donext
;vm0branch:
; pop eax
; test eax, eax
; jz vmbranch
; add esi, 4
; donext
vm0branch:
pop ecx
jecxz vmbranch
add esi, 4
donext
vmbranch:
add esi, [esi]
donext
;vmdrop:
; pop eax
; donext
vmdrop:
add esp, 4
donext
;vmdup:
; pop eax
; push eax
; push eax
; donext
vmdup:
push dword [esp]
donext
vmswap:
pop eax
pop ecx
push eax
push ecx
donext
vmrot:
pop ecx
pop edx
pop eax
push edx
push ecx
push eax
donext
vmover:
pop eax
pop ecx
push ecx
push eax
push ecx
donext
vmcfetch:
pop ecx
xor ecx, 3
movzx edx, byte [ecx]
push edx
donext
vmfetch:
pop eax
push dword [eax]
donext
vmcstore:
pop edx
pop ecx
xor edx, 3
mov [edx], cl
donext
vmstore:
pop edx
pop ecx
mov [edx], ecx
donext
vmand:
pop eax
pop ecx
and eax, ecx
push eax
donext
vmor:
pop eax
pop ecx
or eax, ecx
push eax
donext
vmxor:
pop eax
pop ecx
xor eax, ecx
push eax
donext
vmfromr:
push dword [ebp]
add ebp, 4
donext
vmtor:
pop eax
sub ebp, 4
mov [ebp], eax
donext
vmrfetch:
push dword [ebp]
donext
vmeq:
pop eax
pop ebx
xor eax, ebx
sub eax, 1
sbb eax, eax
push eax
donext
vmplus:
pop eax
add [esp], eax
donext
;vmplus:
; pop eax
; pop ecx
; add eax, ecx
; push eax
; donext
;vmnegate:
; pop eax
; neg eax
; push eax
; donext
vmnegate:
neg dword [esp]
donext
;vmrshift:
; pop ecx
; pop eax
; shr eax, cl
; push eax
; donext
vmrshift:
pop ecx
shr dword [esp], cl
donext
;vmlshift:
; pop ecx
; pop eax
; shl eax, cl
; push eax
; donext
vmlshift:
pop ecx
shl dword [esp], cl
donext
vmspfetch:
mov eax, esp
push eax
donext
vmspstore:
pop esp
donext
vmrpfetch:
push ebp
donext
vmrpstore:
pop ebp
donext
;vmgt:
; pop ecx
; pop eax
; sub eax, ecx
; sar eax, 31
; push eax
; donext
vmgt:
pop ecx
pop eax
sub eax, ecx
cdq
push edx
donext
vmugt:
pop eax
pop ecx
cmp ecx, eax
sbb eax, eax
push eax
donext
vmummult:
pop ecx
pop eax
mul ecx
push eax
push edx
donext
vmumdiv:
pop ecx
pop edx
pop eax
div ecx
push edx
push eax
donext
vmdplus:
pop ecx
pop eax
add [esp + 4], eax
adc [esp], ecx
donext
vmbye:
push dword 0
call exit
donext
vmkey:
call getchar
push eax
donext
vmemit:
call putchar
pop eax
donext
vmopenfile:
vmclosefile:
vmreadline:
vmwriteline:
vmreadfile:
vmwritefile:
vmsystem:
vmreposfile:
vmfilepos:
vmdelfile:
vmfilesize:
donext
section .data
_vmops dd vmnoop, vmexit, vmlit, vmbranch, vm0branch, vmdrop,
dd vmdup, vmswap, vmrot, vmover, vmcfetch, vmfetch, vmcstore, vmstore,
dd vmand, vmor, vmxor, vmfromr, vmtor, vmrfetch, vmeq, vmugt, vmgt,
dd vmplus, vmnegate, vmlshift, vmrshift, vmummult, vmumdiv, vmdplus,
dd vmemit, vmkey, vmbye, vmspfetch, vmspstore, vmrpfetch, vmrpstore,
dd vmopenfile, vmclosefile, vmreadline, vmwriteline, vmreadfile,
dd vmwritefile, vmsystem, vmreposfile, vmfilepos, vmdelfile, vmfilesize
vmops equ _vmops - 1