宏替换GAS中的常数
X86 GNU Assembly 上的那个宏有什么问题吗?它表示符号 S 在链接期间未定义。
.macro S size=40
\size
.endm
我用它就像
mov %eax, S
What't wrong with that macro on X86 GNU Assembly? It says the symbol S is undefined during linking.
.macro S size=40
\size
.endm
I'm using it like
mov %eax, S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
宏用于为您经常使用的代码创建模板,而不是输入常数。因此,我不相信汇编器会在表达式中进行宏扩展。由于您只需要一个数字,因此可以使用
.set
来定义常量。另外,如果您通常使用 intel 语法,请确保您了解此代码的作用:它当前将 eax 的值存储在内存中的地址 0x28 处。如果要将数字40放入eax中,则需要反转操作数并在S前面使用美元符号。
Macros are used to create templates for code you frequently use, not to input a constant number. As such, I do not believe the assembler does macro expansion within an expression. Since you simply want a number, you could use
.set
to define a constant.Also, in case you usually use intel syntax, make sure you realize what this code is doing: It currently stores the value of eax in memory at the address 0x28. If you want to put the number 40 in eax, you need to reverse the operands and use a dollar sign in front of S.
您还可以直接用等号
=
指定符号:GNU 手册中说这相当于使用
.set
https://sourceware.org/binutils/docs-2.19/as/Setting-Symbols.html#Setting-符号.equ
是另一个同义词...此类常量的一个典型用例是计算静态字符串的长度,例如 Linux x86_64 hello world 可以写为:
hello_world.S
您可以编译并运行:
GitHub 上游。
%rdx
将包含要写入的字符串的长度。就像任何常规地址标签一样,
$
是必需的,否则您将尝试访问该内存地址而不是移动立即数。You can also directly assign symbols with the equals sign
=
:which the GNU as manual says is equivalent to using
.set
https://sourceware.org/binutils/docs-2.19/as/Setting-Symbols.html#Setting-Symbols.equ
is yet another synonym...One typical use case of such constants, is to calculate the length of static strings, e.g. the Linux x86_64 hello world can be written as:
hello_world.S
which you can compile and run wth:
GitHub upstream.
%rdx
will then contain the length of the string to be written.The
$
is required just like for any regular address label, otherwise you would be trying to access that memory address instead of moving an immediate.