News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

MASM32 macro to provide different address label each time it's called

Started by penguino, July 29, 2014, 10:20:12 AM

Previous topic - Next topic

penguino

I want a macro that can produce a routine with a different address label each time it is called. To illustrate - if I use:

makelabel MACRO name
    LOCAL exit
    jmp exit
test_1 :
    print "Called ", name, "'s internal routine", 13,10
    ret
exit :
ENDM

and call it in a console app with :

main proc
    makelabel "wombat"
    call test_1
    inkey
    exit
main endp

it prints
Called wombat's internal routine
Press any key to continue ...


But obviously, if I try to use the macro again in the same proc the compiler will complain of symbol redefinition. If I could change the line "test_1 :" in the macro to produce an externally accessible label which is, for example "test_" concatenated to the parameter string I send to the memo then I could use it something like:

main proc
    makelabel "wombat"
    makelabel "kangaroo"
    call test_wombat
    call test_kangaroo
    inkey
    exit
main endp

and get output like:

Called wombat's internal routine
Called kangaroo's internal routine
Press any key to continue ...


I have tried replacing the macro line "test_1 :" unsuccessfully as follows:

test_&name& : // Fails compiling macro - syntax error : wombat
test_<name> : // Fails compiling macro - syntax error : wombat
test&name& : // Fails compiling macro - syntax error : in instruction
test<name> : // Fails compiling macro - syntax error : in instruction
test_name : // Fails compiling main as the label is "test_name" not "test_wombat"

Is there a way using MASM32 macros to do what I am attempting?



penguino

Sorry to reply to my own post but I believe (but can't confirm) that in GAS you could do something like:

   .macro makelabel name
   .globl test_\label
test_\label :
   //code here
   .endm

to get the effect I am looking for.... but I want the MASM equivalent.

hutch--

Normally in MASM you write the macro with a line like,


    LOCAL lbl


Use the "lbl" name in the macro then you can call the macro more than once and each instance of the macro will have a unique label.

I did not know you had wombats in NZ.  :biggrin:

Gunther

You have to know the facts before you can distort them.

penguino

Quote from: hutch-- on July 29, 2014, 10:39:09 AM
Normally in MASM you write the macro with a line like,


    LOCAL lbl


Use the "lbl" name in the macro then you can call the macro more than once and each instance of the macro will have a unique label.

I did not know you had wombats in NZ.  :biggrin:

I can't get that to work - if I use LOCAL then I can't access the label from outside the macro.

..and (sadly) we don't have wombats or kangaroos   :(

On another topic altogether, do I have to fill in the verification characters  and answer all the same questions every time I post?

qWord

You can't use string literals as labels. Just pass the unquoted label name to the macro as separated argument.
Regardless that you might describe more detailed what you are trying to do...
MREAL macros - when you need floating point arithmetic while assembling!

hutch--

The questions are only there to slow up the spammers, once you have posted a bit more it will not happen. RE the use of LOCAL labels in a MASM macro, they can only be accessed from within that macro so if its possible with the task you have in mind, redesign the macro so that the access to it remains only within the macro then you can call the macro which of course inlines the macro code in your proc as often as you need the code.

penguino

Quote from: qWord on July 29, 2014, 11:59:06 AM
You can't use string literals as labels. Just pass the unquoted label name to the macro as separated argument.
Regardless that you might describe more detailed what you are trying to do...

Thanks qWord, that has solved the first of my problems. Now using:

makelabel MACRO name, namelabel
    LOCAL exit, lbl
    jmp exit
namelabel :
    print "Called ", name, "'s internal routine", 13,10
    ret
exit :
ENDM

I can do what I want. With regard to why I want to do this, I am trying to convert a FORTH assembler/interpreter from GAS to MASM - as a way to better understand both FORTH and MASM. I could do without the macro but it would add significantly to the typing. It is better explained in the original code at https://github.com/AlexandreAbreu/jonesforth/blob/master/jonesforth.S


jj2007

Tested with MASM (6.14 ... 10.0) and JWasm:

include \masm32\include\masm32rt.inc

makelabel MACRO name
.if 0
@CatStr(<test_>, <name>, <:>)
print "Called &name's internal routine", 13,10
ret
.endif
ENDM

.code
main:
    makelabel wombat
    makelabel kangaroo
    call test_wombat
    call test_kangaroo
    call test_wombat
    call test_kangaroo
    inkey
    exit
end main