在GNU汇编程序宏中引用操作数/参数 [英] Referencing operands/parameters in GNU assembler macros

查看:63
本文介绍了在GNU汇编程序宏中引用操作数/参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试理解汇编语言中的的概念,尤其是在IA-32(x86)的GNU汇编器,AT& T语法中.我大学的幻灯片说:

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

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

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'

我到底在做什么错?

推荐答案

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

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

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

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

这篇关于在GNU汇编程序宏中引用操作数/参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆