Home > OS >  How can I expand the macro in ARM assembly?
How can I expand the macro in ARM assembly?

Time:12-02

.macro f x,y
    ...
.endm
....
    f x10,x11

How can I expand the usage of macro f x10,x11? gcc -E doesn't seem to help, FWIW.

CodePudding user response:

Assuming from your mention of gcc that you are using the GNU assembler, you can generate a listing file containing .macro expansions. It's not mentioned in the manual but does show up in as --help:

  -a[sub-option...]   turn on listings
                          Sub-options [default hls]:
                          c      omit false conditionals
                          d      omit debugging directives
                          g      include general info
                          h      include high-level source
                          l      include assembly
                          m      include macro expansions
                          n      omit forms processing
                          s      include symbols
                          =FILE  list to FILE (must be last sub-option)

It would seem like -am would be what you want, but in fact that produces an empty listing file. However, -alm does work.

So if your source code is

    .macro square dst, src
    mul \dst, \src, \src
    .endm
    
    square x0, x1
    

you can do

as -alm=foo.lst foo.s

and get the following in foo.lst:

AARCH64 GAS  foo.s          page 1


   1                        .macro square dst, src
   2                        mul \dst, \src, \src
   3                        .endm
   4                        
   5                        square x0, x1
   5 0000 207C019B  >  mul x0,x1,x1
   6                        

The macro expansion is shown on line 5.


Regarding your comment about gcc -E, it helps to understand that the GNU toolchain provides two entirely separate and independent ways to use macros in assembly:

  • You can pass the source through the C preprocessor before feeding it to the assembler. This allows you to use #define style macros, as in Frant's answer. You can do both passes automatically by naming your file like foo.S with a capital S, and using gcc to assemble it, like gcc -c foo.S.

  • The .macro style macros are processed by the assembler itself, and have nothing to do with the C preprocessor. That's why gcc -E didn't have any effect.

  • Related