【问题标题】:Referencing operands/parameters in GNU assembler macros在 GNU 汇编器宏中引用操作数/参数
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2021-09-12
    相关资源
    最近更新 更多