-->

Compile Arm Assembly directly in XCode

2020-07-20 05:15发布

问题:

What is the best way to go about compiling arm assembly code into xcode. I have those assembly files which are generated. Is there a way i can just include the .s file directly into the c code that i have. Or i will need to run an preprocessor first which would generate the .o file which i can link with my files. If that is the case, how do you do it in XCode.

回答1:

If you could post the exact compiler error xcode is spitting out then I might be able to come up with a better solution but my guess as of now is that you are forgetting to add the _ prefix do you functions in assembly.

 .globl _add_in_asm
_add_in_asm:
      add r0,r0,#1
      bx lr

Now in the C source file

#include <stdio.h>

extern int add_in_asm(int i);  

int main(int argc, char* argv[]) {
     printf("result:%i\n", add_in_asm(10));
     return 0;
}

The program should print

 result:11


回答2:

It appears that you just need to add the .s file into your project. Converting it into inline assembly for C is possible, but also fairly difficult, so I recommend against it.



回答3:

If you are having linking problems, remember Mach-O will be looking for the assembly methods to be prefixed with underscore. @A Person's example shows this, but does not point it out.

C-declaration

extern int add_in_asm(int i);

links against ASM method:

_add_in_asm


回答4:

That is the first google result and whilst the answer is good, the actual way for me to do it is (Xcode 7.3.1; using real device)

  • under Xcode add an empty assembly file and use the code as above but add align statement

    .globl _add_in_asm .align 4 _add_in_asm: add r0,r0,#1 bx lr

  • in the AppDelegate.m

    add

    include <stdio.h> and

    under the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch.

    extern int add_in_asm(int i); printf("result:%i\n", add_in_asm(10));

Having said that and after it was successfully run, it does not work after I close Xcode and restart it ... well one has to use real device and when restart, it would try to use emulator!!!!

`    
/Users/xxx/Documents/myMac-git/testasm/my-testasm.s:18:11: error: invalid operand for instruction
  add r0,r0,#1
      ^
/Users/xxx/Documents/myMac-git/testasm/my-testasm.s:19:7: error: unrecognized instruction mnemonic
  bx lr
  ^
Command     /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1

`

If you see above message use real devices!



标签: iphone