Search code examples
iphoneassemblyinline-assembly

How do I do inline assembly on the IPhone?


How is it done? What steps do I need to take and what pitfalls and gotchas are there to consider?


Solution

  • I've gotten this to work, thanks to some inside help over at the Apple Devforums, you should sign up if you're a dedicated IPhone developer.

    First thing's first, it's __asm__(), not plain asm().

    Secondly, by default, XCode generates a compilation target that compiles inline assembly against the ARM Thumb instruction set, so usat wasn't recognized as a proper instruction. To fix this, do "Get Info" on the Target. Scroll down to the section "GCC 4.0 - Code Generation" and uncheck "Compile for Thumb". Then this following snippet will compile just fine if you set the Active SDK to "Device"

    inline int asm_saturate_to_255 (int a) {
      int y;
      __asm__("usat %0, #8, %1\n\t" : "=r"(y) : "r"(a));
      return y;
    }
    

    Naturally, now it won't work with the IPhone Simulator. But TargetConditionals.h has defines you can #ifdef against. Namely TARGET_OS_IPHONE and TARGET_IPHONE_SIMULATOR.