| 1 |
; NASM macro set to make interfacing to 32-bit programs easier |
| 2 |
; Also cool little macros to make NASM emulate some MASM things. |
| 3 |
; |
| 4 |
; Originally included in NASM. Modifications by Peter Johnson, 1999. |
| 5 |
; |
| 6 |
|
| 7 |
%imacro proc 1 ; begin a procedure definition |
| 8 |
%push proc |
| 9 |
global %1 |
| 10 |
%1: push ebp |
| 11 |
mov ebp, esp |
| 12 |
%assign %$arg 8 |
| 13 |
;%assign %$argnum 0 |
| 14 |
%define %$procname %1 |
| 15 |
%endmacro |
| 16 |
|
| 17 |
%imacro arg 0-1 4 ; used with the argument name as a label |
| 18 |
%00 equ %$arg |
| 19 |
;%assign %$argnum %$argnum+1 |
| 20 |
;.arg%$argnum equ %1 |
| 21 |
%assign %$arg %1+%$arg |
| 22 |
%endmacro |
| 23 |
|
| 24 |
%imacro endproc 0 |
| 25 |
%ifnctx proc |
| 26 |
%error Mismatched `endproc'/`proc' |
| 27 |
%else |
| 28 |
; mov esp, ebp |
| 29 |
; pop ebp |
| 30 |
%ifdef LEGACY_ENDPROC |
| 31 |
ret |
| 32 |
%endif |
| 33 |
;__end_%$procname: ; useful for calculating function size |
| 34 |
; global %{$procname}_arglen |
| 35 |
;%{$procname}_arglen equ %$arg-8 |
| 36 |
;%assign %$i 1 |
| 37 |
;%rep %$argnum |
| 38 |
; global %{$procname}_arg%$i |
| 39 |
;%{$procname}_arg%$i equ %{$procname}.arg%$i |
| 40 |
;%assign %$i %$i+1 |
| 41 |
;%endrep |
| 42 |
%pop |
| 43 |
%endif |
| 44 |
%endmacro |
| 45 |
|
| 46 |
; redefine ret instructions for in-proc cases |
| 47 |
%imacro ret 0-1 |
| 48 |
%ifnctx proc |
| 49 |
ret %1 |
| 50 |
%else |
| 51 |
mov esp, ebp |
| 52 |
pop ebp |
| 53 |
ret %1 |
| 54 |
%endif |
| 55 |
%endmacro |
| 56 |
|
| 57 |
%imacro retf 0-1 |
| 58 |
%ifnctx proc |
| 59 |
retf %1 |
| 60 |
%else |
| 61 |
mov esp, ebp |
| 62 |
pop ebp |
| 63 |
retf %1 |
| 64 |
%endif |
| 65 |
%endmacro |
| 66 |
|
| 67 |
%imacro retn 0-1 |
| 68 |
%ifnctx proc |
| 69 |
retn %1 |
| 70 |
%else |
| 71 |
mov esp, ebp |
| 72 |
pop ebp |
| 73 |
retn %1 |
| 74 |
%endif |
| 75 |
%endmacro |
| 76 |
|
| 77 |
; invoke calls a C function |
| 78 |
; defaults to word (16 bit) size parameters |
| 79 |
%macro invoke 1-* |
| 80 |
%rotate -1 |
| 81 |
;%define invoketype word |
| 82 |
%rep (%0-1) |
| 83 |
; %ifidni %1,dword |
| 84 |
; %define invoketype dword |
| 85 |
; %elifidni %1,word |
| 86 |
; %define invoketype word |
| 87 |
; %elifidni %1,byte |
| 88 |
; %define invoketype byte |
| 89 |
; %else |
| 90 |
%ifidni %1, cs |
| 91 |
o16 push %1 |
| 92 |
%elifidni %1, ds |
| 93 |
o16 push %1 |
| 94 |
%elifidni %1, es |
| 95 |
o16 push %1 |
| 96 |
%elifidni %1, fs |
| 97 |
o16 push %1 |
| 98 |
%elifidni %1, gs |
| 99 |
o16 push %1 |
| 100 |
%elifidni %1, word cs |
| 101 |
o16 push %1 |
| 102 |
%elifidni %1, word ds |
| 103 |
o16 push %1 |
| 104 |
%elifidni %1, word es |
| 105 |
o16 push %1 |
| 106 |
%elifidni %1, word fs |
| 107 |
o16 push %1 |
| 108 |
%elifidni %1, word gs |
| 109 |
o16 push %1 |
| 110 |
%else |
| 111 |
push %1 |
| 112 |
%endif |
| 113 |
; %endif |
| 114 |
%rotate -1 |
| 115 |
%endrep |
| 116 |
call %1 |
| 117 |
%if (%0!=1) |
| 118 |
add esp, byte %{1}_arglen |
| 119 |
%endif |
| 120 |
%endmacro |
| 121 |
|