Author Topic: About %xdefine  (Read 10440 times)

Offline edwardcarlfox

  • Jr. Member
  • *
  • Posts: 7
About %xdefine
« on: August 20, 2012, 11:06:17 AM »
Hello everyone, I am trying to make some macro to aid programming in assembly. And what I am trying to do is, to define some macro, which will define a function header (including the label, some standard routines and give names to each parameter), and also some define local variables. Here are the codes:

Code: [Select]
%macro FUNCTION 1-*
%1:
push ebp
mov ebp, esp
%assign i 1
%rotate 1
%rep (%0 - 1) / 2
%xdefine %2 %1 [ebp + i * 4]
%assign i i + 1
%rotate 2
%endrep
%define no_paras ((%0 - 1) / 2)
%endmacro

%macro LOCALS 2-*
sub esp, %0 / 2 * 4
%assign i 1
%rep %0 / 2
%xdefine %2 %1 [ebp - i * 4]
%assign i i + 1
%rotate 2
%endrep
%define no_locals (%0 / 2)
%endmacro

When calling the macros, one would write something like this:
Code: [Select]
FUNCTION func1, dword, a, dword, b
LOCALS dword, c, dword, d
where func1 is the function name (the label), a and b are parameters, c and d are local variables. I "define" the parameters and variables using %xdefine, as you can see in the macro.

So now the problem is, when I call the macro the second time, and using the same names for parameters and variables like this:
Code: [Select]
FUNCTION func1, dword, a
mov a, eax

FUNCTION func2, dword, a
mov a, eax
I get some crazy stuff:
Code: [Select]
func1:
%line 3+0 C:\Users\edwardcarlfox\Desktop\ASMSrc\test.asm
 push ebp
 mov ebp, esp
%line 4+1 C:\Users\edwardcarlfox\Desktop\ASMSrc\test.asm
mov dword [ebp + 1 * 4], eax

 func2:
%line 6+0 C:\Users\edwardcarlfox\Desktop\ASMSrc\test.asm
 push ebp
 mov ebp, esp
%line 7+1 C:\Users\edwardcarlfox\Desktop\ASMSrc\test.asm
mov [ebp + 1 * 4] dword [ebp + 1 * 4] [ebp + 1 * 4], eax
Technically, if I call the macro the second time, the name "a" will be redefined, so it will have a new value (although here the old and new values are the same). I cannot seem to find any error that causes this problem. But if I every time use different names, I have no problem.

Could some one see the problem? Or maybe I used some bad solution for this?

PS: I know there are already standard macros to define parameters and local variables, but I just want to make my own. So please don't tell me to use %arg and so on. I want to find out where I did wrong, or it is just a bug in the NASM preprocessor. And I also want to learn preprocessing in NASM.

Thanks in advanced!

Offline edwardcarlfox

  • Jr. Member
  • *
  • Posts: 7
Re: About %xdefine
« Reply #1 on: August 20, 2012, 12:42:39 PM »
Never mind, I found the problem. It is, in the second time when the macro is called, the name "a" is already defined, so the actual parameters to the macro are
Code: [Select]
FUNCTION, dword, dword [ebp + 1 * 4]
and because of this, I got some weird output. Now I need to find out a walkaround to define the names used in a function before it returns.