我如何 运行 带有汇编插入的 C 代码

How can I run C-code with assembly inserts

我有带汇编插入的 C 程序。 这是代码: main.c:

#include <stdio.h>

int f() {
    int a = 0, b;
    __asm__ (".intel_syntax noprefix\n\t"
             "mov edx, 1\n\t"
             :"=r"(b)
             :"r"(a)
             :"eax"
            );
    return b;
}

int  main() {
    printf("%d", f());
}

我已经找到了如何编译这段代码 (gcc -std=c11 -S main.c -o main),但是当我 运行它 (./main) - 这是终端中的一个问题: bash: ./main: 权限被拒绝。

当我尝试不使用 -S 标志进行编译时,我遇到了很多不合理的错误:

/tmp/ccw3H0Mb.s: Assembler messages:
/tmp/ccw3H0Mb.s:23: Error: Instruction does not exist: «movl%edx,-4(%rbp)»
/tmp/ccw3H0Mb.s:24: Error: Instruction does not exist: «movl4(%rbp),%eax»
/tmp/ccw3H0Mb.s:46: Error: Instruction does not exist: «movl[=12=],%eax»
/tmp/ccw3H0Mb.s:48: Error: Instruction does not exist: «movl%eax,%esi»
/tmp/ccw3H0Mb.s:49: Error: Garbage «(%rip)» after expression
/tmp/ccw3H0Mb.s:50: Error: Instruction does not exist: «movl[=12=],%eax»
/tmp/ccw3H0Mb.s:52: Error: Instruction does not exist: «movl[=12=],%eax»

错误是什么?我如何才能将它正确地修复到 运行 我的代码中? 谢谢! :)

我所需要的只是将 -masm=intel 添加到编译命令中,当然,我删除了 -S 标志(感谢 fuz).这很重要,因为我使用的是 Intel 语法,所以 gcc 必须了解它。

现在看起来像:

gcc -std=c11 -masm=intel main.c -o main