Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate GNU GAS macro arguments with other tokens to make single label?

I would like to dynamically create a set of labels in an assembly function using a gas macro. I would like to do something like this:

 .macro set_up_jumptab_entry prefix, from=0, to=10
     .quad \prefix_\item
     .if \to-\from
     set_up_jumptab_entry \prefix,"(\from+1)",\to
     .endif
 .endm
 set_up_jumptab_entry myfunc 0 10

Here \prefix_\item would be something like myfunction_7. Now, I can find lots of examples of recursive invocation, but I haven't found one of just label concatenation involving passed-in macro arguments. Gas is quite poorly documented, so answering this question is difficult for me.

  1. Can you concatenate arguments to macros with other tokens to make single tokens?
  2. What's your favorite gas assembler reference?
like image 728
pgod Avatar asked Jun 07 '10 14:06

pgod


2 Answers

things like

\argA\()\argB :

should create a label composed by argA and argB.

EDIT

Testing, \() seems to be not necessary; the test code was:

    .file   "test.c"

.macro prova argA, argB
\argA\argB :
 .endm
    .text
.globl main
    .type   main, @function
main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    movl    $0, %eax
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
        prova abc, def
        jmp  abcdef
    ret
    .size   main, .-main
    .ident  "GCC: (GNU) 4.3.2"
    .section    .note.GNU-stack,"",@progbits

which is just gcc -S test.c output (lazyness:D) of a minimal C code. (prova means test in italian)

like image 122
ShinTakezou Avatar answered Sep 22 '22 04:09

ShinTakezou


What's your favorite gas assembler reference?

The docs cover this one quite well https://sourceware.org/binutils/docs/as/Macro.html

The string `()' can be used to separate the end of a macro argument from the following text. eg:

.macro opcode base length
    \base\().\length
.endm