using assembly code inside Objective c program (Xcode)

随声附和 提交于 2019-12-30 05:14:07

问题


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.


回答1:


Yes, of course.

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


来源:https://stackoverflow.com/questions/25326307/using-assembly-code-inside-objective-c-program-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!