Question

I am currently trying to understand the concept of macros in the assembly language, specifically in GNU assembler, AT&T syntax for IA-32 (x86). The slides from my university say the following:

# How to define a macro:
.macro write string
    movl string, %esi
    call printstr
.endm

# How to use a macro:
write aString

However, this doesn't work for me. I am using gcc to compile my code.

.data
    msg:    .string "The result is %d.\n"

.text
.global main

.macro add_3 n
    movl n, %eax
    addl $3, %eax
.endm

main:
    add_3 $39
    pushl %eax
    pushl $msg
    call printf
    popl %eax
    popl %eax
    movl $1, %eax
    int $0x80

When I try to compile this, I get the following error:

undefined reference to `n'

What exactly am I doing wrong?

Was it helpful?

Solution

To reference arguments inside the macro, prepend the name with a backslash. For example:

.macro  add_3  n
    movl \n + 3, %eax
.endm

GAS manual: https://sourceware.org/binutils/docs/as/Macro.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top