-
Notifications
You must be signed in to change notification settings - Fork 0
/
x86-32_engine_asm.s
131 lines (112 loc) · 2.75 KB
/
x86-32_engine_asm.s
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
[BITS 32]
global m4_x86_32_engine_run_asm
global m4_x86_32_engine_call_runtime_word
global m4_x86_32_engine_callback_target_0
global m4_x86_32_engine_callback_target_1
global m4_x86_32_engine_callback_target_2
global m4_x86_32_engine_callback_target_3
global m4_x86_32_engine_callback_target_4
global m4_x86_32_engine_callback_target_5
global m4_x86_32_engine_callback_target_6
global m4_x86_32_engine_callback_target_7
extern m4_x86_32_engine_get_ctx
%macro save_state 1
mov %1, ebx ; use %1 to calculate stack depth
sub %1, [esi+28] ; subtract stack base. same calculation as "depth" word
shr %1, 2 ; express as elements instead of bytes
add ebx, 4 ; m4_stack_t has *data point 1 past the end
mov [esi+0], ebx ; store *data in stack struct
mov [esi+8], %1 ; store len in stack struct
mov [esi+12], edx ; store data-space pointer
%endmacro
m4_x86_32_engine_run_asm:
push ebx
push edi
push esi
mov esi, [esp+12+4] ; main struct
mov ebx, [esi+0] ; stack data
sub ebx, 4
mov edi, [esi+32] ; edi value from struct
mov edx, [esi+12] ; data-space pointer
call [esp+12+8]
add ebx, 4
mov [ebx], eax
save_state eax
pop esi
pop edi
pop ebx
ret
m4_x86_32_engine_call_runtime_word:
save_state eax
mov ebx, esp ; use ebx to store stack pointer misalignment
and ebx, 15 ; isolate the misalignment
xor esp, ebx ; align the stack pointer
push esi ; push pointer to stack struct (2nd fn param)
push dword [ecx+4]; push param (1st fn param)
call [ecx+0] ; call the function
add esp, 8
or esp, ebx ; misalign the stack pointer again
mov ebx, [esi+0] ; get the new *data
sub ebx, 4
mov edx, [esi+12] ; get new data-space pointer
ret
m4_x86_32_engine_callback_target_0:
xor ecx, ecx
jmp callback_handler
m4_x86_32_engine_callback_target_1:
mov ecx, 1
jmp callback_handler
m4_x86_32_engine_callback_target_2:
mov ecx, 2
jmp callback_handler
m4_x86_32_engine_callback_target_3:
mov ecx, 3
jmp callback_handler
m4_x86_32_engine_callback_target_4:
mov ecx, 4
jmp callback_handler
m4_x86_32_engine_callback_target_5:
mov ecx, 5
jmp callback_handler
m4_x86_32_engine_callback_target_6:
mov ecx, 6
jmp callback_handler
m4_x86_32_engine_callback_target_7:
mov ecx, 7
callback_handler:
call m4_x86_32_engine_get_ctx
push ebx
push edi
push esi
mov esi, eax ; struct
mov ebx, [esi+0] ; stack data
mov eax, [esi+20]
mov al, [eax+ecx]
push eax
shr al, 1
xor edi, edi
.L0:
mov edx, [esp+16+4+edi]
mov [ebx+edi], edx
add edi, 4
dec al
jnz .L0
add ebx, edi
mov eax, [ebx-4] ; top stack value
sub ebx, 8
mov edi, [esi+32] ; edi value from struct
mov edx, [esi+12] ; data-space pointer
shl ecx, 2
add ecx, [esi+24]
call [ecx]
pop ecx
test cl, 1
jnz .L1
add ebx, 4
mov [ebx], eax
.L1:
save_state ecx
pop esi
pop edi
pop ebx
ret