带有GCC的内联组件

Inline Assembly with GCC

本文关键字:组件 GCC 带有      更新时间:2023-10-16
#include <stdio.h>
int
get_random(void)
{
    asm(".intel_syntax noprefixn"
        "mov eax, 42           n");
    asm("mov eax, 42 n");
}
int
main(void)
{
    return printf("The answer is %d.n", get_random());
}

我正在尝试使用以下CLI命令编译此C++程序:g++ asm.cpp -o asm

错误消息:

/tmp/ccXHbaRO.s: Assembler messages:
/tmp/ccXHbaRO.s:41: Error: no such instruction: `movl %eax,%esi'
/tmp/ccXHbaRO.s:42: Error: no such instruction: `movl $.LC0,%edi'
/tmp/ccXHbaRO.s:43: Error: no such instruction: `movl $0,%eax'

因为我添加了asm(".intel_syntax noprefixn");,我想我不需要添加GCC标志-masm=intel

此外,我在哪里可以找到有关-masm标志的更多信息?有NASM的等效物吗?

您在汇编中编写的代码会逐字逐句地放在编译器的输出中。

这意味着,如果更改格式或其他有关如何解析程序集代码的全局选项,则需要在最后恢复默认选项。

如果你不这样做,编译器在你的部分之后生成的代码将变得无效。