-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfasmcpp.win64.asm
252 lines (216 loc) · 3.63 KB
/
fasmcpp.win64.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
use64
org WIN64_LOW_MEMORY_FIXED_ADDRESS
; symbols to export
dq _assemble
dq _run
dq _input_filename
dq _output_filename
dq _fasm_memory
; symbols to import
fasmError dq ?
fasmOpen dq ?
fasmRead dq ?
fasmWrite dq ?
fasmClose dq ?
fasmLseek dq ?
fasmDisplayBlock dq ?
; code
_assemble: ; Assembly* assembly, void* memory, uint32_t size, const char* predefinitions
mov [rsp+8], rcx
mov [rsp+16], rdx
mov [rsp+24], r8
mov [rsp+32], r9
push rbp
push rbx
push rdi
push rsi
mov rax, [rsp+4*8+8]
mov [assembly], rax
; We split memory just like the Win32 assembler interface: 25% memory, 75% additional memory
mov eax, [rsp+4*8+16]
mov [memory_start], eax
mov ebx, [rsp+4*8+24]
mov ecx, ebx
shr ebx, 2
sub ecx, ebx
add ebx, eax
mov [memory_end], ebx
mov [additional_memory], ebx
add ebx, ecx
mov [additional_memory_end], ebx
mov eax, esp
and eax, not 0FFFh
add eax, 1000h-10000h
mov [stack_limit], eax
mov eax, [rsp+4*8+32]
mov [initial_definitions], eax
mov [input_file], _input_filename
mov [output_file], _output_filename
mov [symbols_file], 0
mov [passes_limit], 100
mov [rsp_backup], rsp
call preprocessor
call parser
call assembler
call formatter
exit:
call show_display_buffer
mov rsp, [rsp_backup]
pop rsi
pop rdi
pop rbx
pop rbp
ret
_run:
push rbx
push rbp
push rdi
push rsi
push r12
push r13
push r14
push r15
mov [rsp_backup], rsp
call rcx
mov rsp, [rsp_backup]
pop r15
pop r14
pop r13
pop r12
pop rsi
pop rdi
pop rbp
pop rbx
ret
macro pusha
{
push rcx
push rdx
push r8
push r9
push r10
push r11
}
macro popa
{
pop r11
pop r10
pop r9
pop r8
pop rdx
pop rcx
}
macro fix_stack
{
mov r12, rsp
sub rsp, 8*4
and rsp, not 0xF
}
macro restore_stack
{
mov rsp, r12
}
fatal_error:
assembler_error:
mov rcx, [assembly]
mov edx, [rsp]
fix_stack
call [fasmError]
jmp exit
create: ; in: edx = filename - out: ebx = handle, cf = error
open: ; in: edx = filename - out: ebx = handle, cf = error
pusha
fix_stack
mov rcx, [assembly]
call [fasmOpen]
restore_stack
popa
or eax, eax
jz open_error
mov ebx, eax
clc
ret
open_error:
stc
ret
read: ; in: ebx = handle, ecx = size, edx = buffer - out: cf = error
pusha
fix_stack
mov r9d, ecx
mov r8d, edx
mov edx, ebx
mov rcx, [assembly]
call [fasmRead]
restore_stack
popa
ret
write: ; in: ebx = handle, ecx = size, edx = buffer - out: cf = error
pusha
fix_stack
mov r9d, ecx
mov r8d, edx
mov edx, ebx
mov rcx, [assembly]
call [fasmWrite]
restore_stack
popa
ret
close: ; in: ebx = handle
pusha
fix_stack
mov rcx, [assembly]
mov edx, ebx
call [fasmClose]
restore_stack
popa
ret
lseek: ; in: ebx = handle, edx = offset, al = mode (0: begin, 1: current, 2: end) out: eax
pusha
fix_stack
movzx r9d, al
mov r8d, edx
mov edx, ebx
mov rcx, [assembly]
call [fasmLseek]
restore_stack
popa
ret
get_environment_variable: ; in: esi = name, edi = buffer, ecx = size
ret
display_block: ; in: esi = message, ecx = size
pusha
fix_stack
mov r8d, ecx
mov edx, esi
mov rcx, [assembly]
call [fasmDisplayBlock]
restore_stack
popa
ret
make_timestamp:
jmp code_cannot_be_generated
initial_defs db 0
_input_filename db "input", 0
input_filename_end:
_output_filename db "output", 0
output_filename_end:
purge pusha, popa
include 'linux/x64/modes.inc'
include 'version.inc'
include 'errors.inc'
include 'symbdump.inc'
include 'preproce.inc'
include 'parser.inc'
include 'exprpars.inc'
include 'assemble.inc'
include 'exprcalc.inc'
include 'formats.inc'
include 'avx.inc'
include 'x86_64.inc'
include 'tables.inc'
include 'messages.inc'
; data
assembly dq ?
rsp_backup dq ?
include 'variable.inc'
_fasm_memory: