using assembly code inside Objective c program (Xcode)

前端 未结 1 556
野趣味
野趣味 2021-01-03 11:09

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 sear

相关标签:
1条回答
  • 2021-01-03 11:55

    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
    
    0 讨论(0)
提交回复
热议问题