Search code examples
cassemblymacrosgnu-assembler

Calling a table of function using repeat blocks or macros in GNU assembly


If I wanted to generate code that looks like this:

CALL FUNC0
CALL FUNC1
CALL FUNC2
CALL FUNC3
CALL FUNC4
CALL FUNC5
...
CALL FUNC19

How can I use a repeat block generate such code, using something similar to the code below:

.equ SYMBOL, 0
.rept 20
   CALL FUNC(SMYBOL)
   .equ SYMBOL, (SYMBOL+1)
.endr

Macros are fine too.

I'd like to know how to do this in C as well.


Solution

  • You can use a macro do this:

    .altmacro
    .macro call_funcs count
        .if \count
            call_funcs %(count-1)
        .endif
        CALL    FUNC\count
    .endm
    
    call_funcs 20
    

    The .altmacro directive enables the use of % to evaluate count - 1 before passing recursively as a macro argument. Otherwise you get CALL FUNC20-1-1-1-1 which not what you want.

    As far as I know there's no way to what you want in C. For that I would write a program that generates the C code you need.