How do I use the ARM assembler in XCode?

arm, assembly, ios, xcode

Solution

I finally found the answer myself. It's actually not that hard. I only solved it for the 32-bit ARM version though.

useless.h:

void useless();

useless.s:

#ifdef __arm__


    .syntax        unified
    .globl         _useless
    .align         2
    .code          16
    .thumb_func    _useless

_useless:
    //.cfi_startproc
    bx    lr
    //.cfi_endproc

// CFI means Call Frame Information
// Optionally. Use for better debug-ability.


#endif

useless.c:

#ifndef __arm__

void useless()
{
}

#endif

Notes:

The CLANG ARM Assembler syntax is a bit different from what you see in example all over the web. Comments start with `//` and `/* multiline comments */` are also supported. It also understands the standard C preprocessor. The function has to be defined as a Thumb function, if you specify an arm function (`.code 32`) the program will just crash. The line `.thumb_func _useless` can be ommited and it works still. I have no Idea what it means. If you omit the `.code 16` line, the program crashes.

about the `#ifdef`. For ARMv7, `__arm__` is defined. For ARMv8, i.e. the 64bit-variant on the iPhone 5S, `__arm__` is not defined, but `__arm64__` is defined instead. The above code does not work for the 64bit-ARM-version. Instead, the implementation from `useless.c` will be used. (I didn't forget ARMv7s, I just don't have a device with that arch in my hands currently, so I cannot test.)

Problem

Just for educational purposes, I would like to add a function to an existing iPhone app, written in ARM assembly. I don't need a tutorial on ARM assembly in general, because I already read too many of them. I just don't know how to actually run the code! What I would like to do is something like: useless.h: ``` void useless(); ``` useless.s: ``` useless: bx lr ``` If this also works on the simulator it would be fine... On the simulator, the .s file would not compile, so I should maybe do something like: useless.s: ``` #if I_AM_ARM useless: bx lr #endif ``` useless.c: ``` #if !I_AM_ARM void useless() { } #endif ``` I know the syntax I use is broken, but how do I write it correctly? (Breaking an app on the simulator just because I want to try some inline assembly is no option...) The second-best option would be to use inline assembly, but I would strongly prefer non-inline assembly. Thanks! Edit: I want to learn ARM assembly, so I would like to find a method to compile ARM assembly code, and to EXECUTE ARM assembly code.

Original source