-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.v
505 lines (501 loc) · 9.65 KB
/
execute.v
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
module main
import encoding.binary
// FIXME: The executing opcodes NOT yet implemented!
fn execute(entry_point_address u32, code_size u32, code_part []u8, exe_memory []u8, cpu_regs &CpuRegs) {
println_warning("The executing opcodes NOT yet implemented!")
mut current_point_offset := u32(0)
for current_point_offset < code_size
{
mut opcode_size := u8(1)
match code_part[current_point_offset]
{
// ADD Eb, Gb
0x00
{
opcode_size = 2
match code_part[current_point_offset + 1]
{
// ADD [EBX], BL
0x1B
{
asmstr := "ADD [EBX], BL"
println_debug(" ${asmstr}")
}
// ADD [ESI], DH
0x36
{
asmstr := "ADD [ESI], DH"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// ADD Ev, Gv
0x01
{
opcode_size = 2
match code_part[current_point_offset + 1]
{
// ADD EBX, EBX
0xDB
{
asmstr := "ADD EBX, EBX"
println_debug(" ${asmstr}")
}
// ADD EBX, ESI
0xDE
{
asmstr := "ADD EBX, ESI"
println_debug(" ${asmstr}")
}
// ADD ESI, ESI
0xF6
{
asmstr := "ADD ESI, ESI"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// ADD Gb, Eb
0x02
{
opcode_size = 2
match code_part[current_point_offset + 1]
{
// ADD BL, [EBX]
0x1B
{
asmstr := "ADD BL, [EBX]"
println_debug(" ${asmstr}")
}
// ADD DH, [ESI]
0x36
{
asmstr := "ADD DH, [ESI]"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// ADD Gv, Ev
0x03
{
opcode_size = 2
match code_part[current_point_offset + 1]
{
// ADD EBX, EBX
0xDB
{
asmstr := "ADD EBX, EBX"
println_debug(" ${asmstr}")
}
// ADD EBX, ESI
0xDE
{
asmstr := "ADD EBX, ESI"
println_debug(" ${asmstr}")
}
// ADD ESI, ESI
0xF6
{
asmstr := "ADD ESI, ESI"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// ADD AL, Ib
0x04
{
opcode_size = 2
value_imm := code_part[current_point_offset + 1].hex()
asmstr := "ADD AL, 0x${value_imm}"
println_debug(" ${asmstr}")
}
// ADD AX, Iv
0x05
{
opcode_size = 3
value_imm := binary.little_endian_u16(code_part[(current_point_offset + 1)..(current_point_offset + 3)])
asmstr := "ADD AX, 0x${value_imm}"
println_debug(" ${asmstr}")
}
// PUSH ES
0x06
{
opcode_size = 1
asmstr := "PUSH ES"
println_debug(" ${asmstr}")
}
// POP ES
0x07
{
opcode_size = 1
asmstr := "POP ES"
println_debug(" ${asmstr}")
}
// OR Eb, Gb
0x08
{
opcode_size = 2
match code_part[current_point_offset + 1]
{
// OR [EBX], BL
0x1B
{
asmstr := "OR [EBX], BL"
println_debug(" ${asmstr}")
}
// OR [ESI], DH
0x36
{
asmstr := "OR [ESI], DH"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// OR Ev, Gv
0x09
{
opcode_size = 2
match code_part[current_point_offset + 1]
{
// OR EBX, EBX
0xDB
{
asmstr := "OR EBX, EBX"
println_debug(" ${asmstr}")
}
// OR ESI, ESI
0xF6
{
asmstr := "OR ESI, ESI"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// OR Gb, Eb
0x0A
{
opcode_size = 2
match code_part[current_point_offset + 1]
{
// OR BL, [EBX]
0x1B
{
asmstr := "OR BL, [EBX]"
println_debug(" ${asmstr}")
}
// OR DH, [ESI]
0x36
{
asmstr := "OR DH, [ESI]"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// OR Gv, Ev
0x0B
{
opcode_size = 2
match code_part[current_point_offset + 1]
{
// OR EBX, EBX
0xDB
{
asmstr := "OR EBX, EBX"
println_debug(" ${asmstr}")
}
// OR ESI, ESI
0xF6
{
asmstr := "OR ESI, ESI"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// OR AL, Ib
0x0C
{
opcode_size = 2
value_imm := code_part[current_point_offset + 1].hex()
asmstr := "OR AL, 0x${value_imm}"
println_debug(" ${asmstr}")
}
// OR AX, Iv
0x0D
{
opcode_size = 3
value_imm := binary.little_endian_u16(code_part[(current_point_offset + 1)..(current_point_offset + 3)])
asmstr := "OR AX, 0x${value_imm}"
println_debug(" ${asmstr}")
}
// PUSH CS
0x0E
{
opcode_size = 1
asmstr := "PUSH CS"
println_debug(" ${asmstr}")
}
// XOR Gv, Ev
0x33
{
opcode_size = 2
match code_part[current_point_offset + 1]
{
// XOR EBX, EBX
0xDB
{
asmstr := "XOR EBX, EBX"
println_debug(" ${asmstr}")
}
// XOR ESI, ESI
0xF6
{
asmstr := "XOR ESI, ESI"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// PUSH EAX
0x50
{
asmstr := "PUSH EAX"
println_debug(" ${asmstr}")
}
// PUSH ECX
0x51
{
asmstr := "PUSH ECX"
println_debug(" ${asmstr}")
}
// PUSH EDX
0x52
{
asmstr := "PUSH EDX"
println_debug(" ${asmstr}")
}
// PUSH EBX
0x53
{
asmstr := "PUSH EBX"
println_debug(" ${asmstr}")
}
// PUSH ESP
0x54
{
asmstr := "PUSH ESP"
println_debug(" ${asmstr}")
}
// PUSH EBP
0x55
{
asmstr := "PUSH EBP"
println_debug(" ${asmstr}")
}
// PUSH ESI
0x56
{
asmstr := "PUSH ESI"
println_debug(" ${asmstr}")
}
// PUSH EDI
0x57
{
asmstr := "PUSH EDI"
println_debug(" ${asmstr}")
}
// POP EAX
0x58
{
asmstr := "POP EAX"
println_debug(" ${asmstr}")
}
// POP ECX
0x59
{
asmstr := "POP ECX"
println_debug(" ${asmstr}")
}
// POP EDX
0x5A
{
asmstr := "POP EDX"
println_debug(" ${asmstr}")
}
// POP EBX
0x5B
{
asmstr := "POP EBX"
println_debug(" ${asmstr}")
}
// POP ESP
0x5C
{
asmstr := "POP ESP"
println_debug(" ${asmstr}")
}
// POP EBP
0x5D
{
asmstr := "POP EBP"
println_debug(" ${asmstr}")
}
// POP ESI
0x5E
{
asmstr := "POP ESI"
println_debug(" ${asmstr}")
}
// POP EDI
0x5F
{
asmstr := "POP EDI"
println_debug(" ${asmstr}")
}
// Immedaite Grp1 Ev, Iv
0x81
{
opcode_size = 6
match code_part[current_point_offset + 1]
{
// SUB ESP, Iv
0xEC
{
value_imm := binary.little_endian_u16(code_part[(current_point_offset + 2)..(current_point_offset + 4)]).hex()
asmstr := "SUB ESP, 0x${value_imm}"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// MOV Ev, Gv
0x89
{
match code_part[current_point_offset + 1..current_point_offset + 3]
{
// MOV [ESP + offset], EAX
[u8(0x44), 0x24]
{
opcode_size = 4
asmstr := 'MOV [ESP + 0x${code_part[current_point_offset + 3].hex()}], EAX'
println_debug(' ${asmstr}')
}
// MOV [ESP + offset], EBX
[u8(0x5C), 0x24]
{
opcode_size = 4
asmstr := 'MOV [ESP + 0x${code_part[current_point_offset + 3].hex()}], EBX'
println_debug(' ${asmstr}')
}
else
{
println_error('Invalid or unrecognized ModR/M byte!')
vin32_exit(exit_failure)
}
}
}
// NOP
0x90
{
opcode_size = 1
asmstr := "NOP"
println_debug(" ${asmstr}")
}
// MOV Eb, Ib
0xC6
{
match code_part[current_point_offset + 1..current_point_offset + 3]
{
// MOV [ESP + offset], Ib
[u8(0x44), 0x24]
{
opcode_size = 5
offset := code_part[current_point_offset + 3].hex()
value_imm := code_part[current_point_offset + 4].hex()
asmstr := "MOV [ESP + 0x${offset}], 0x${value_imm}"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
// MOV Ev, Iv
0xC7
{
match code_part[current_point_offset + 1..current_point_offset + 3]
{
// MOV [ESP + offset], Iv
[u8(0x44), 0x24]
{
opcode_size = 8
offset := code_part[current_point_offset + 3].hex()
value_imm := binary.big_endian_u32(code_part[(current_point_offset + 4)..(current_point_offset + 8)]).hex()
asmstr := "MOV [ESP + 0x${offset}], 0x${value_imm}"
println_debug(" ${asmstr}")
}
else
{
println_error("Invalid or unrecognized ModR/M byte!")
vin32_exit(exit_failure)
}
}
}
else
{
println_error("Invalid or unrecognized opcode!")
vin32_exit(exit_failure)
}
}
current_point_offset += opcode_size
}
}