I have a question about MASM macro programming, which is relatively advanced (to me anyway), but I really hope someone might know the answer of it, because situations arise quite often where I'd like to know how to do this.

Ok, here follows an example to explain it all:

We have one basic macro, like this:



Macro1 MACRO param1, param2
push param1
push param2
...
...
ENDM



We also have another macro, that takes two string parameters, merges them, and the executes the result as a macro, like this:



Macro2 MACRO param1, param2
temp_cmd textequ @CatStr(<param1>, <param2>)
temp_cmd
ENDM



So, if I call Macro2 like this, all is ok, and a call to Macro1 is assembled and executed inside Macro2:



Macro2 <Macro1 eax,>, <ebx>



BUT, if I call the macro like this, there is trouble:



P1 textequ <Macro1 eax,>
P2 textequ <ebx>
Macro2 P1, P2


Then the compiler says:
error A2008: syntax error : P1P2


The compiler has obviously concatenated the two symbol names "P1" and "P2" into a single string "P1P2", and after this it cannot longer understand/dereference it. :(


So, after this messy example, here comes the simple question:
Is there any way to "dereference" or "evaluate" macro symbols manually at any point in the code, one level at the time? Sort of like how the % operator works for constants?

In the example above, I would need to dereference P1 and P2 "one level down", to <Macro1 eax,> and <ebx> at the point of the @CatStr line (the reason for putting them in P1 and P2 in the first place is that the compiler sometimes give you the stupid "line too long" error when calling a macro with the actual values, if they are too long).

If there isn't a way to dereference/evaluate a macro symbol "one level" down, is there at least a way to dereference it completely, all the way to the final value?

Any tips would be greatly appreciated.

Thanks!
Posted on 2002-12-22 17:33:27 by dELTA
Macro2 <%P1>, <%P2>

The square brackets ensure the arguements are still strings, and the % forces evaluation of the symbol into its value. Another solution - which I like:
Macro2 MACRO param1, param2

temp_cmd [b]CATSTR[/b] param1, param2
temp_cmd
ENDM
Better yet :)
Macro2 MACRO param1, param2

param1 param2
ENDM
As you can now see, the second macro is not needed:
P1 P2
...is all that is required. :grin:
Posted on 2002-12-22 19:03:41 by bitRAKE
As well, your text equate has a COMMA in it. So using it you get:

Macro2 Macro1 eax,,ebx

Post more of what your tyring to do/learn, and im sure we can help take the confusion out ;) . Macros are very useful but the are also annoying like Java-Script to learn and understand on your own.

As well read chapter 9 in the Masm reference on macro's (think its chapter 9 anyways). It is a good resource.

:alright:
NaN
Posted on 2002-12-22 21:14:47 by NaN
Yet another "solution":



Macro1 MACRO param1, param2
push param1
push param2
ENDM

Macro2 MACRO param1, param2
temp_cmd textequ @CatStr(%param1, %param2 )
temp_cmd
ENDM

P1 textequ <Macro1 eax,>
P2 textequ <ebx>

Macro2 P1, P2

Posted on 2002-12-23 01:25:39 by japheth
Doh! I only tried to put the %-operator outside the brackets, not inside. :) Now it works like a charm. Thanks for all the tips and help guys!


NaN:
I was planning on doing the evaluation first inside Macro2 (like in japheth's solution), not in the call to Macro2. Then the commas would not be a problem. But I see what you mean when the call is made like this:

Macro2 %P1, %P2

Then parameter 2 to Macro2 would be empty, and Macro2 would instead be passed an extra parameter (containing the value of P2) that would be truncated, right?



Also, thanks for the workaround solutions guys, but it was the evaluation operator I was looking for, the code was just a simple example to explain what I meant. :)


And to let you know why I wanted to do this at all:

I was experimenting with passing macros as parameters to other macros (sort of like function pointers in C), but at one point this caused the call to the Macro2-equivalent macro in my code to get a "line too long" error from the compiler (due to unhealthily long names of the macros that were passed as parameters *ashamed* :)). So, then I resorted to the solution with putting the macro names inside P1, P2 and such, which in turn led me to the "P1P2 problem". Also, I have a general uncertainty as to where and when things are really evaluated during the processing of MASM macros. Due to the (as mentioned above) sometimes javascript like type of situations and problems that can arise when "generating macros with macros" like this, it can be quite necessary to have a good knowledge about exactly when evaluations happen, and I still haven't got a good feel for this. :(

Thanks again guys!
Posted on 2002-12-23 11:08:51 by dELTA