views:

731

answers:

3

I would like to begin writing ARM assembler and running it on the iPhone. This is not with the intent of using in an app to be released to the app store. Basically I would like to solve problems on ProjectEuler using ARM and the iPhone. Just for hobby and educational purposes. How can I go about doing this? I have not been able to come up with a way get a project running using any hand written arm.

Thanks

+4  A: 

You can use gcc to make asm inlines with asm, or just get a gnu as for arm and write code in separate files. You should have no problems with later linking them up to your project, but I'd suggest you to use C/ObjC code to wrap up your asm stubs, as writng the whole iPhone application in assembler is somewhat hard (you need to be pretty good in ObjC runtime internals).

You might be interested in using custom Makefiles, however Xcode projects should be sufficient for most of the taks too.

Farcaller
The iPhone dev tools include an assembler, no need to go out and get one separately. You can include .s files in your XCode project and it will "just work".
Stephen Canon
yes, that *is* gnu as ;)
Farcaller
+1  A: 

Note also that there is nothing wrong with including assembly in app store submissions. It's only using frameworks that are not public they frown on.

They don't care how the binary is generated as long as it works, looks decently OK, and follows the aforementioned rule.

Kendall Helmstetter Gelner
+2  A: 

I haven't been able to find any information about how to write assembly code specifically for the iPhone, but like the other people said here, you can either:

1) write inline asm statements in C/C++/ObjC code, or 2) write standalone assembly functions in a '.s' file and simply add it in your XCode sources, or 3) write standalone assembly functions for an external assembler such as NASM. You write assembly code in a '.s' file and generate a '.o' object file using NASM and link the object file with your project in XCode.

So if you are just trying to write a few assembly instructions then inline assembler would be the easiest way, but if you plan on writing several assembly functions then I'd recommend a standalone assembly file. And if you plan on writing many assembly functions with macros and want cross-platform compatibility, etc, then I'd recommend using NASM to compile your assembly code into an object file. Because I am using the XCode gcc assembler and it is quite lacking compared to NASM. You can get NASM for free at http://www.nasm.us/

Once you have setup your assembler environment, you need to learn how to write ARM Assembly code! Because the iPhone (and many other portable devices and smartphones) use the ARM instruction set. There is a very good but old intro to ARM assembly at http://www.coranac.com/tonc/text/asm.htm.

When it comes to assembly programming, the official instruction set reference manual is usually the main source of information for everything you will write, so you should go to the ARM website and download the ARM and Thumb-2 Quick Reference Card (6 pages long) as well as the 2 full documents for your exact CPU.

For example, the iPhone 3GS and iPhone 4 both have an ARMv7-A Cortex-A8 CPU, so you can download the ARM Architecture Reference Manual ARMv7-A and ARMv7-R Edition (2000 pages long) that tells you exactly which instructions are available and exactly how they work, and the Cortex™-A8 Technical Reference Manual that explains the instruction timing, etc for your specific CPU.

Shervin Emami