Page 1 of 1

Why NASM macros don't generate code?

Posted: Sat Jan 02, 2010 5:37 am
by ~
Whether I use NASM or YASM I'm having a problem in which macros don't seem to generate anything for some reason.

I have tried putting them before the rest of the code but they keep. Even YASM reports them as undefined symbols if I try to use them in some code like this (because of the local label named .__1):

Code: Select all

   .__1:
   ___MACRO_push_rsp
   ___MACRO_pop_rsp

   mov eax,ebp
   cmp dword[eax+1_],2
   je .__1
They aren't reported as undefined if I use a global label (without a dot at the beginning of if).


My macros are something like this:

Code: Select all

%macro ___MACRO_push_rsp 0
 push esp


%endmacro



%macro ___MACRO_pop_rsp 0
 pop esp


%endmacro


But they don't generate code for some reason. It's as if they weren't included in the source files and if they are called more than once it's like defining a label and is reported as a redefinition.

It doesn't happen if I put the macros in a source file with macros only (no other definitions or db, dw, dd variables, etc.) and also if I put them before anything else in the program includes. Maybe it's like C with some definitions which need to be defined before first use?


Does anybody know what could be done or some further information I coud provide to solve this?

Re: Why NASM macros don't generate code?

Posted: Sat Jan 02, 2010 7:17 pm
by thepowersgang
Did you remember to actually call the macros?
Just defining a macro does nothing to the final output, it needs to be invoked.

Example:

Code: Select all

%macro FRAME_INIT 0
push ebp
mov ebp, esp
%endmacro
%macro FRAME_CLOSE
pop ebp
%endmacro

function:
    FRAME_INIT
    ... do stuff
    FRAME_CLOSE
    ret

Re: Why NASM macros don't generate code?

Posted: Mon Jan 04, 2010 2:18 am
by qw
I don't find anything wrong. You are defining the macro's before using them, arent't you? Maybe you'd better post the complete code.