Search code examples
assemblyx86macrosx86-64gnu-assembler

How to replace the value of symbol in x86 assembly using gnu asembler macro? Compose register names like xmm0 from xmm(i)


.macro macro_name
  .set i, 0
  .rept 8
    movss  (i * 4)(%rsi), %xmm(i)
    set i , i + 1
  .endr
.endm

I wrote this macro to load 8 single precession floating point numbers into xmm registers to build the argument for a function.
The expanded macro should look like this.

movss (%rsi), %xmm0
movss 0x4(%rsi), %xmm1
...
movss 0x1c(%rsi), %xmm7

The line defined in the macro : movss (i * 4)(%rsi), %xmm(i) : The first expansion (i * 4) works as expected but, the second expansion (i) doesn't work. It just concatenates like %xmm(i).


Solution

  • You can do it using .altmacro and a helper like so:

    .altmacro
    .macro help addr reg
    movss \addr(%rsi), %xmm\reg
    .endm
    .macro macro_name
      .set i, 0
      .rept 8
        help %(i * 4), %(i)
        .set i , i + 1
      .endr
    .endm