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