9e052883 |
1 | ; 7zAsm.asm -- ASM macros\r |
2 | ; 2022-05-16 : Igor Pavlov : Public domain\r |
3 | \r |
4 | \r |
5 | ; UASM can require these changes\r |
6 | ; OPTION FRAMEPRESERVEFLAGS:ON\r |
7 | ; OPTION PROLOGUE:NONE\r |
8 | ; OPTION EPILOGUE:NONE\r |
9 | \r |
10 | ifdef @wordsize\r |
11 | ; @wordsize is defined only in JWASM and ASMC and is not defined in MASM\r |
12 | ; @wordsize eq 8 for 64-bit x64\r |
13 | ; @wordsize eq 2 for 32-bit x86\r |
14 | if @wordsize eq 8\r |
15 | x64 equ 1\r |
16 | endif\r |
17 | else\r |
18 | ifdef RAX\r |
19 | x64 equ 1\r |
20 | endif\r |
21 | endif\r |
22 | \r |
23 | \r |
24 | ifdef x64\r |
25 | IS_X64 equ 1\r |
26 | else\r |
27 | IS_X64 equ 0\r |
28 | endif\r |
29 | \r |
30 | ifdef ABI_LINUX\r |
31 | IS_LINUX equ 1\r |
32 | else\r |
33 | IS_LINUX equ 0\r |
34 | endif\r |
35 | \r |
36 | ifndef x64\r |
37 | ; Use ABI_CDECL for x86 (32-bit) only\r |
38 | ; if ABI_CDECL is not defined, we use fastcall abi\r |
39 | ifdef ABI_CDECL\r |
40 | IS_CDECL equ 1\r |
41 | else\r |
42 | IS_CDECL equ 0\r |
43 | endif\r |
44 | endif\r |
45 | \r |
46 | OPTION PROLOGUE:NONE \r |
47 | OPTION EPILOGUE:NONE\r |
48 | \r |
49 | MY_ASM_START macro\r |
50 | ifdef x64\r |
51 | .code\r |
52 | else\r |
53 | .386\r |
54 | .model flat\r |
55 | _TEXT$00 SEGMENT PARA PUBLIC 'CODE'\r |
56 | endif\r |
57 | endm\r |
58 | \r |
59 | MY_PROC macro name:req, numParams:req\r |
60 | align 16\r |
61 | proc_numParams = numParams\r |
62 | if (IS_X64 gt 0)\r |
63 | proc_name equ name\r |
64 | elseif (IS_LINUX gt 0)\r |
65 | proc_name equ name\r |
66 | elseif (IS_CDECL gt 0)\r |
67 | proc_name equ @CatStr(_,name)\r |
68 | else\r |
69 | proc_name equ @CatStr(@,name,@, %numParams * 4)\r |
70 | endif\r |
71 | proc_name PROC\r |
72 | endm\r |
73 | \r |
74 | MY_ENDP macro\r |
75 | if (IS_X64 gt 0)\r |
76 | ret\r |
77 | elseif (IS_CDECL gt 0)\r |
78 | ret\r |
79 | elseif (proc_numParams LT 3)\r |
80 | ret\r |
81 | else\r |
82 | ret (proc_numParams - 2) * 4\r |
83 | endif\r |
84 | proc_name ENDP\r |
85 | endm\r |
86 | \r |
87 | \r |
88 | ifdef x64\r |
89 | REG_SIZE equ 8\r |
90 | REG_LOGAR_SIZE equ 3\r |
91 | else\r |
92 | REG_SIZE equ 4\r |
93 | REG_LOGAR_SIZE equ 2\r |
94 | endif\r |
95 | \r |
96 | x0 equ EAX\r |
97 | x1 equ ECX\r |
98 | x2 equ EDX\r |
99 | x3 equ EBX\r |
100 | x4 equ ESP\r |
101 | x5 equ EBP\r |
102 | x6 equ ESI\r |
103 | x7 equ EDI\r |
104 | \r |
105 | x0_W equ AX\r |
106 | x1_W equ CX\r |
107 | x2_W equ DX\r |
108 | x3_W equ BX\r |
109 | \r |
110 | x5_W equ BP\r |
111 | x6_W equ SI\r |
112 | x7_W equ DI\r |
113 | \r |
114 | x0_L equ AL\r |
115 | x1_L equ CL\r |
116 | x2_L equ DL\r |
117 | x3_L equ BL\r |
118 | \r |
119 | x0_H equ AH\r |
120 | x1_H equ CH\r |
121 | x2_H equ DH\r |
122 | x3_H equ BH\r |
123 | \r |
124 | ifdef x64\r |
125 | x5_L equ BPL\r |
126 | x6_L equ SIL\r |
127 | x7_L equ DIL\r |
128 | \r |
129 | r0 equ RAX\r |
130 | r1 equ RCX\r |
131 | r2 equ RDX\r |
132 | r3 equ RBX\r |
133 | r4 equ RSP\r |
134 | r5 equ RBP\r |
135 | r6 equ RSI\r |
136 | r7 equ RDI\r |
137 | x8 equ r8d\r |
138 | x9 equ r9d\r |
139 | x10 equ r10d\r |
140 | x11 equ r11d\r |
141 | x12 equ r12d\r |
142 | x13 equ r13d\r |
143 | x14 equ r14d\r |
144 | x15 equ r15d\r |
145 | else\r |
146 | r0 equ x0\r |
147 | r1 equ x1\r |
148 | r2 equ x2\r |
149 | r3 equ x3\r |
150 | r4 equ x4\r |
151 | r5 equ x5\r |
152 | r6 equ x6\r |
153 | r7 equ x7\r |
154 | endif\r |
155 | \r |
156 | \r |
157 | ifdef x64\r |
158 | ifdef ABI_LINUX\r |
159 | \r |
160 | MY_PUSH_2_REGS macro\r |
161 | push r3\r |
162 | push r5\r |
163 | endm\r |
164 | \r |
165 | MY_POP_2_REGS macro\r |
166 | pop r5\r |
167 | pop r3\r |
168 | endm\r |
169 | \r |
170 | endif\r |
171 | endif\r |
172 | \r |
173 | \r |
174 | MY_PUSH_4_REGS macro\r |
175 | push r3\r |
176 | push r5\r |
177 | push r6\r |
178 | push r7\r |
179 | endm\r |
180 | \r |
181 | MY_POP_4_REGS macro\r |
182 | pop r7\r |
183 | pop r6\r |
184 | pop r5\r |
185 | pop r3\r |
186 | endm\r |
187 | \r |
188 | \r |
189 | ; for fastcall and for WIN-x64\r |
190 | REG_PARAM_0_x equ x1\r |
191 | REG_PARAM_0 equ r1\r |
192 | REG_PARAM_1_x equ x2\r |
193 | REG_PARAM_1 equ r2\r |
194 | \r |
195 | ifndef x64\r |
196 | ; for x86-fastcall\r |
197 | \r |
198 | REG_ABI_PARAM_0_x equ REG_PARAM_0_x\r |
199 | REG_ABI_PARAM_0 equ REG_PARAM_0\r |
200 | REG_ABI_PARAM_1_x equ REG_PARAM_1_x\r |
201 | REG_ABI_PARAM_1 equ REG_PARAM_1\r |
202 | \r |
203 | else\r |
204 | ; x64\r |
205 | \r |
206 | if (IS_LINUX eq 0)\r |
207 | \r |
208 | ; for WIN-x64:\r |
209 | REG_PARAM_2_x equ x8\r |
210 | REG_PARAM_2 equ r8\r |
211 | REG_PARAM_3 equ r9\r |
212 | \r |
213 | REG_ABI_PARAM_0_x equ REG_PARAM_0_x\r |
214 | REG_ABI_PARAM_0 equ REG_PARAM_0\r |
215 | REG_ABI_PARAM_1_x equ REG_PARAM_1_x\r |
216 | REG_ABI_PARAM_1 equ REG_PARAM_1\r |
217 | REG_ABI_PARAM_2_x equ REG_PARAM_2_x\r |
218 | REG_ABI_PARAM_2 equ REG_PARAM_2\r |
219 | REG_ABI_PARAM_3 equ REG_PARAM_3\r |
220 | \r |
221 | else\r |
222 | ; for LINUX-x64:\r |
223 | REG_LINUX_PARAM_0_x equ x7\r |
224 | REG_LINUX_PARAM_0 equ r7\r |
225 | REG_LINUX_PARAM_1_x equ x6\r |
226 | REG_LINUX_PARAM_1 equ r6\r |
227 | REG_LINUX_PARAM_2 equ r2\r |
228 | REG_LINUX_PARAM_3 equ r1\r |
229 | REG_LINUX_PARAM_4_x equ x8\r |
230 | REG_LINUX_PARAM_4 equ r8\r |
231 | REG_LINUX_PARAM_5 equ r9\r |
232 | \r |
233 | REG_ABI_PARAM_0_x equ REG_LINUX_PARAM_0_x\r |
234 | REG_ABI_PARAM_0 equ REG_LINUX_PARAM_0\r |
235 | REG_ABI_PARAM_1_x equ REG_LINUX_PARAM_1_x\r |
236 | REG_ABI_PARAM_1 equ REG_LINUX_PARAM_1\r |
237 | REG_ABI_PARAM_2 equ REG_LINUX_PARAM_2\r |
238 | REG_ABI_PARAM_3 equ REG_LINUX_PARAM_3\r |
239 | REG_ABI_PARAM_4_x equ REG_LINUX_PARAM_4_x\r |
240 | REG_ABI_PARAM_4 equ REG_LINUX_PARAM_4\r |
241 | REG_ABI_PARAM_5 equ REG_LINUX_PARAM_5\r |
242 | \r |
243 | MY_ABI_LINUX_TO_WIN_2 macro\r |
244 | mov r2, r6\r |
245 | mov r1, r7\r |
246 | endm\r |
247 | \r |
248 | MY_ABI_LINUX_TO_WIN_3 macro\r |
249 | mov r8, r2\r |
250 | mov r2, r6\r |
251 | mov r1, r7\r |
252 | endm\r |
253 | \r |
254 | MY_ABI_LINUX_TO_WIN_4 macro\r |
255 | mov r9, r1\r |
256 | mov r8, r2\r |
257 | mov r2, r6\r |
258 | mov r1, r7\r |
259 | endm\r |
260 | \r |
261 | endif ; IS_LINUX\r |
262 | \r |
263 | \r |
264 | MY_PUSH_PRESERVED_ABI_REGS macro\r |
265 | if (IS_LINUX gt 0)\r |
266 | MY_PUSH_2_REGS\r |
267 | else\r |
268 | MY_PUSH_4_REGS\r |
269 | endif\r |
270 | push r12\r |
271 | push r13\r |
272 | push r14\r |
273 | push r15\r |
274 | endm\r |
275 | \r |
276 | \r |
277 | MY_POP_PRESERVED_ABI_REGS macro\r |
278 | pop r15\r |
279 | pop r14\r |
280 | pop r13\r |
281 | pop r12\r |
282 | if (IS_LINUX gt 0)\r |
283 | MY_POP_2_REGS\r |
284 | else\r |
285 | MY_POP_4_REGS\r |
286 | endif\r |
287 | endm\r |
288 | \r |
289 | endif ; x64\r |