【发布时间】:2013-11-14 21:47:43
【问题描述】:
我目前正在尝试理解汇编语言中的宏的概念,特别是在 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'
我到底做错了什么?
【问题讨论】:
-
在宏中使用参数名称时,请尝试在参数名称前加上反斜杠。 IE。
movl \n, %eax -
确实有效,非常感谢!
-
这个问题一直悬而未决,所以我正在回答它。
标签: assembly gcc x86 macros gnu-assembler