在Objective C程序(Xcode)中使用汇编代码

arj*_*mar 3 assembly xcode objective-c xcode5

有没有一种方法可以在Objective C程序中使用汇编代码。我正在为OSX开发应用程序,并希望将汇编代码与Objective C代码一起使用。我搜索了互联网,发现了这一点,但是我无法成功实现任何这些方法。任何帮助将不胜感激。

Ste*_*non 5

当然是。

您可以像在C语言中一样在Objective-C中使用GCC风格的内联汇编。您还可以在汇编源文件中定义函数,然后从Objective-C调用它们。这是内联汇编的一个简单示例:

int foo(int x, int y) {
    __asm("add %1, %0" : "+r" (x) : "r" (y));
    return x;
}
Run Code Online (Sandbox Code Playgroud)

还有一个关于如何使用独立程序集文件的类似的最小示例:

** myOperation.h **
int myOperation(int x, int y);

** myOperation.s **
.text
.globl _myOperation
_myOperation:
    add %esi, %edi  // add x and y
    mov %edi, %eax  // move result to correct register for return value
    ret

** foo.c **
#include "myOperation.h"   // include header for declaration of myOperation
...
int x = 1, y = 2;
int z = myOperation(x, y); // call function defined in myOperation.s
Run Code Online (Sandbox Code Playgroud)