views:

5106

answers:

3

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

+9  A: 

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;
}
Nils Pipenbrinck
I havn't been able to get this compiled. I've been trying through Apple's XCode. The compiler is actually set to GCC 4 by default. If I compile it using that it complains of an invalid instruction 'usat' and if I set it to using llvm-gcc it complains of an invalid architecture, 'armv6'.
Hans Sjunnesson
try gcc -O3 -march=armv6 test.c
Nils Pipenbrinck
isn't llvm-gcc actually a compiler that targets llvm virtual machine?
artificialidiot
yes, but llvm has a ARM code generator backend, so you can use it to compile real code as well as llvm bytecode.
Nils Pipenbrinck
This works with no trouble compiling with the toolchain. I'd be keen on hearing a bit of a rant on useful inline assembler!
Max Stewart
@Max Stewart It would be awesome if you could post an answer detailing how you got the above example to compile through XCode. I didn't spend too long fiddling with it, but I didn't get it to work properly.
Hans Sjunnesson
be pationed guys, I'll write something up (that said: if you can tell me where you want asnm and where your focus of research us I can tailor my post to your demands):
Nils Pipenbrinck
Hey Hans, I'm actually developing using the open toolchain on Linux - I have no experience with XCode yet so don't really know where you'd start.
Max Stewart
+10  A: 

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*.

Hans Sjunnesson
I think you mean __asm__() instead of plain asm().
Quinn Taylor
A: 

Thumb is recommended for application which do not require heavy float operation. Thumb makes the code size smaller and results also in a faster code execution.

So you should only turn Thumb off for application like 3D games...

catlan
Actually, as a rule, using the Thumb instruction set is significantly slower than using the ARM instruction set. As a rule of thumb (no pun intended), you gain 1/3 in code size and loose 1/3 in speed. See eg. http://www.arm.com/pdfs/Thumb-2CoreTechnologyWhitepaper-Final4.pdf
gc