在 GNU 汇编宏中引用操作数/参数



我目前正在尝试理解汇编语言中宏的概念,特别是在GNU汇编程序,IA-32(x86)的AT&T语法中。我大学的幻灯片如下:

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

但是,这对我不起作用。我正在使用 gcc 来编译我的代码。

.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

当我尝试编译它时,出现以下错误:

undefined reference to `n'

我到底做错了什么?

若要引用宏内的参数,请在名称前面加上反斜杠。例如:

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

气体手册:https://sourceware.org/binutils/docs/as/Macro.html

相关内容

最新更新