Search code examples
gccassemblyx86gnu-assemblerosdev

GNU assembler Macros


I am trying to build a simple macro in GNU assembler (2.26.1) (gcc 5.4.0) but getting some error after several interactions: Invalid character '(' in mnemonic

I've tried to follow the advice at https://sourceware.org/binutils/docs/as/Macro.html with \() or & but no luck.

    .macro macro_gen_irqh isrb, isre
    _irq_entry_\isrb\():
        cli
        call isr_common_handler
        .if  \isre-\isrb
           macro_gen_irqh    "(\isrb+1)",\isre
       .endif
    .endm

If I replace the label _irq_entry_\isrb(): to "(\isrb)": it does work for the enumeration but without the required full label name.

The end result should be:

_irq_entry_0:
   cli
   call isr_common_handler
_irq_entry_1:
.
.
_irq_entry_2:
.
.

Solution

  • If you use the .altmacro directive you can rewrite it this way:

    .altmacro
    
    .macro macro_gen_irqh isrb, isre
    _irq_entry_\isrb:
        call isr_common_handler
        .if \isre-\isrb
            macro_gen_irqh %isrb+1,\isre
        .endif
    .endm
    

    altmacro makes a number of changes, but an important one is that if you use % on a macro parameter it is treated as an expression and the result is returned as a string. I take advantage of that in this line:

    macro_gen_irqh %isrb+1,\isre
    

    The result of macro_gen_irqh 0 1 would be:

    _irq_entry_0:
        call isr_common_handler
    
    _irq_entry_1:
        call isr_common_handler