Search code examples
assemblymacrosx86-64masmmasm64

Define procs whose name is from a macro parameter using MASM 64


Is it possible to define procs where the name is derived from the inputs in a macro, using MASM 64? (ml64.exe)

Eg, something similar to below, where @(value) just places the 'value' in to the source.

proc_creator macro name, value
    foo_@(name) proc
        mov rax, value
        ret
    foo_@(name) endp
endm

proc_creator "example", 1
proc_creator "example2", 2

...

    call foo_example2
    ; use rax

So calling the macro twice defines two procs, foo_example and foo_example2.


Solution

  • Looks like the operator to use is & &, eg:

    proc_creator macro name, value
        foo_&name& proc
            mov rax, value
            ret
        foo_&name& endp
    endm
    
    proc_creator "example", 1
    proc_creator "example2", 2
    
    ...
    
        call foo_example2
        ; use rax
    

    https://learn.microsoft.com/en-us/cpp/assembler/masm/operator-logical-and-masm?view=msvc-170