Search code examples
assemblymacrosx86-64gnu-assembler

How can I concatenate a label not in a macro definition? (in GAS assembler)


I want to do this:

i=0
.rept 3
    add rcx,[a?(i*2+1)]
    i=i+1
.endr

It should output this:

add rcx,[a1]
add rcx,[a3]
add rcx,[a5]

I can't find any solution to this problem. I don't want it to be inside a macro definition.


Solution

  • I do not believe you can solve this without using macros or some other type of pre-processing. The GNU assembler method of doing this could look something like:

    .altmacro
    
    .macro addmac val
        add rcx, [a\val]
    .endm
    
    .macro loopmac
        i=0
        .rept 3
            addmac %(i*2+1)
            i=i+1
        .endr
    .endm
    

    You then use the loopmac macro like this:

    loopmac
    

    You'll need the .altmacro directive to be able to process %(i*2+1) properly:

    7.4 .altmacro

    You can write ‘%expr’ to evaluate the expression expr and use the result as a string.

    Rather than using .rept you could use .irp and simplify the macro to:

    .macro loopmac
        .irp i,1,3,5
            add rcx, [a\i]
        .endr
    .endm
    

    The last example doesn't need .altmacro processing.