Search code examples
assemblyx86gnugnu-assembler

Macro substituting a constant number in GAS


What't wrong with that macro on X86 GNU Assembly? It says the symbol S is undefined during linking.

.macro S size=40
\size
.endm

I'm using it like

mov %eax, S

Solution

  • Macros are used to create templates for code you frequently use, not to input a constant number. As such, I do not believe the assembler does macro expansion within an expression. Since you simply want a number, you could use .set to define a constant.

    .set S, 40
    mov %eax, S
    

    Also, in case you usually use intel syntax, make sure you realize what this code is doing: It currently stores the value of eax in memory at the address 0x28. If you want to put the number 40 in eax, you need to reverse the operands and use a dollar sign in front of S.

    mov $S, %eax