使用汇编code目标C程序中(X code) [英] using assembly code inside Objective c program (Xcode)
问题描述
有没有办法使用目标C程序中组装code的一种方式。我开发OSX应用程序,并想用汇编code旁边的目标C code。我在网上搜索,发现这但我不能够执行任何这些成功的方法。任何帮助将大大AP preciated。
Is there a way to use assembly code inside Objective C program. I am developing an application for OSX and wanted to use assembly code alongside the Objective C code. I searched the internet and found this But I am not able to implement any of these methods successfully. Any help will be greatly appreciated.
推荐答案
是的,当然。
您可以使用GCC式内联汇编在Objective-C,就像你在C.您还可以在汇编源文件中定义的函数和Objective-C的给他们打电话。这里是内联汇编的一个简单的例子:
You can use GCC-style inline assembly in Objective-C just like you would in C. You can also define functions in assembly source files and call them from Objective-C. Here's a trivial example of inline assembly:
int foo(int x, int y) {
__asm("add %1, %0" : "+r" (x) : "r" (y));
return x;
}
和如何使用一个独立的汇编文件一个类似的小例子:
And a similarly minimal example of how to use a standalone assembly file:
** 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
这篇关于使用汇编code目标C程序中(X code)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!