0

我有带有汇编插入的 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: Permission否认。

当我尝试在没有 -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$0,%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$0,%eax»
/tmp/ccw3H0Mb.s:52: Error: Instruction does not exist: «movl$0,%eax»

错误是什么?如何修复它以正确运行我的代码?谢谢!:)

4

1 回答 1

0

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

现在看起来像:

gcc -std=c11 -masm=intel main.c -o main
于 2020-11-02T21:11:29.410 回答