Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 468
Hans Sjunnesson Avatar asked Oct 26 '08 13:10

Hans Sjunnesson


People also ask

How does inline assembly work?

In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a higher-level language such as C or Ada.

What is inline assembly in Arduino?

An inline assembly statement is a string of characters which specifies assembler code. The string can contain any instructions recognized by the assembler, including directives, but we will not discuss assembler directives here.


2 Answers

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.

like image 104
Hans Sjunnesson Avatar answered Sep 20 '22 11:09

Hans Sjunnesson


I write quite a bit of ARM Cortex-A8 assembly-code. The CPU on the iPhone is an ARM11 (afaik) so the core instruction set is the same.

What exactly are you looking for? I could give you some examples if you want.


EDIT:

I just found out that on the iPhone you have to use the llvm-gcc compiler. As far as I know it should understand the inline assembler syntax from GCC. If so all the ARM inline assembler tutorials will work on the iPhone as well.

Here is a very minimal inline assembler function (in C). Could you please tell me if it compiles and works on the iphone? If it works I can rant a bit how to do usefull stuff in ARM inline assembler, especially for the ARMv6 architecture and the DSP extensions.

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

should be equivalent to:

inline int saturate_to_255 (int a) {   if (a < 0) a =0;   if (a > 255) a = 255;   return a; } 
like image 28
Nils Pipenbrinck Avatar answered Sep 23 '22 11:09

Nils Pipenbrinck