How do I get a macro that to call another macro dependeding on one of the parameters passed to it? :confused: :confused:

e.g. A basic example:


MACRO1 MACRO a,b,c
mov eax,a
and eax,b
mov c,eax

ENDM

MACRO2 MACRO a,b,c
mov eax,a
or eax,b
mov c,eax

ENDM

MACRO3 MACRO a,b,c
mov eax,a
xor eax,b
mov c,eax

ENDM


BASEMACRO MACRO a,b,c,d,mac
mac a,b,c <-- Here to replace 'mac' with its parameter and assmble with that macro????
xor d,eax

ENDM



.CODE
BASEMACRO edx,ecx,ebx,DWORD PTR [esi + 0],MACRO1
BASEMACRO edx,ecx,ebx,DWORD PTR [esi + 4],MACRO2
BASEMACRO edx,ecx,ebx,DWORD PTR [esi + 8],MACRO3



Where I have 'mac' in the BASEMACRO macro I would like masm to assemble the macro defined by the 'mac' parameter??????????

I have tried the basic obvious but any help or sugestions???
Thanks
Posted on 2002-05-16 22:26:24 by huh
BASEMACRO MACRO a,b,c,d,mac

[b]&[/b]mac a,b,c
xor d,eax
ENDM
or...
BASEMACRO MACRO a,b,c,d,mac

[b]%[/b] mac a,b,c
xor d,eax
ENDM
Read chapter 9 and tell me why? :)
Posted on 2002-05-16 22:42:39 by bitRAKE
I have trouble understanding all these '%' and '&' in macros. I think the MASM help is insufficient since it doesn't explain what these operators are used for exactly (Or I have missed it). What is that 'chapter 9' you always talks about? That can't be the MASM help, or is it?
Posted on 2002-05-21 08:09:30 by gliptic
The chapter 9 is the MASM Manual - if you Google for MASM and MACRO and ENDM, you should be able to find it easily. The % and & operators forces early expansion of macros to their values - rather than using the macro name. This is required when using macros inside other operations, or other situations where MASM would not know what to do. :) Using them when not needed is usually not an error, but you should play with macros to get the hang of it.
Posted on 2002-05-21 08:19:52 by bitRAKE
Thank you for the google-link!

I've done my own ctxt macro which only keeps unique strings. For example, if you write:

mov eax,ctxt("abc")
mov ecx,ctxt("abc")

...it will expand to:

.data
str_0 db "abc",0

.code
mov eax,offset str_0
mov ecx,offset str_0

However, if you put in a ',' in the string, it won't work and MASM says syntax error whatever I do. I'll see if I can solve this using chapter 9.
Posted on 2002-05-22 00:39:31 by gliptic
gliptic, use FORC to itterate through the characters in the string and encode them different. In my macro that does this, I create a global label based on the string, example: cSTR("abc") would produce a global label 'cSTR_abc'. For some characters their encoding is different, example: cSTR("ab_c") produces a global label of 'cSTR_ab__c'. I haven't got around to adding it, but I'd like to also search for substrings and maximize alignment. I use one set of macros for development and another for final build, so the slow down isn't a factor.
Posted on 2002-05-22 07:19:26 by bitRAKE
Okay, maybe I'll do that, but I found it easier to name the strings 'str_0', 'str_1', etc. Of course your way is better for debugging and such.
Posted on 2002-05-22 09:17:46 by gliptic
gliptic, how do you know that 'str_0' contains "abc"?
Do you create another global label?
Posted on 2002-05-22 09:25:43 by bitRAKE
Yes, I have a global variable which contains a list of all the strings. I know this might not be the best solution. How do you check if a certain symbol exists? Can you do that with IFNB?
Posted on 2002-05-23 00:51:46 by gliptic
IFDEF, IFNDEF or testing OPATTR.
Posted on 2002-05-23 07:02:41 by bitRAKE
Okay, that was just as I thought.
Posted on 2002-05-23 08:09:18 by gliptic