为什么Xcode命令行nasm程序集发出错误:不带大小后缀的指令助记符的未知使用

时间:2019-02-10 20:53:46

标签: xcode assembly nasm

我的目标是使用Xcode(10.2-beta)编写Mac OS的C和汇编语言(14.1);我想使用NASM而不是默认的GNU编译器,GAS语法非常糟糕。

顺便说一句,尽管我将Xcode配置为使用NASM,如下所示,但我怀疑它没有在使用它!

我已经使用GNU编译器构建了我的项目。运行正常。

// main.c
# include <stdio.h>

int myOperation(int a, int b);

int main(int argc, char * argv[]) {
    // insert logic here
    int x = 10;
    int y = 12;
    int z = myOperation(x, y);
    printf("The sum of %d and %d is %d\n", x, y, z);
    return 0;
}

//  assembly.s
.text
.globl _myOperation

_myOperation:
    add %esi, %edi
    mov %edi, %eax
    ret

我安装了XCode命令行工具,并更改了构建规则以编译NASM文件。 C文件是相同的:

//  assembly.s
.text
.globl _myOperation

_myOperation:
    add edi, esi
    mov eax, edi
    ret

构建规则 Xcode build rule

我收到以下错误:

/Users/rodrigomattososilveira/projects/asm/NASM Tutorial/Tutorial_01AA/Tutorial_01AA/assembly.s:12:5: error: unknown use of instruction mnemonic without a size suffix
    add edi, esi
    ^
/Users/rodrigomattososilveira/projects/asm/NASM Tutorial/Tutorial_01AA/Tutorial_01AA/assembly.s:13:5: error: unknown use of instruction mnemonic without a size suffix
    mov eax, edi
    ^
Command CompileC failed with a nonzero exit code

我签出的一些链接:

1 个答案:

答案 0 :(得分:1)

我在this Reddit posting上找到了一个提示:

  

看起来gas可以采用Intel语法,但是必须使用 .intel_syntax 标志进行指定。

现在,此代码编译:

.intel_syntax // THIS DID THE TRICK

.text
.global _myOperation

_myOperation:
    add edi, esi
    mov eax, edi
    ret