How to write ARM Assembly code for Android or iPhone:
How to write ARM Assembly code for Android or iPhone:
To write Assembly language code for ARM (Android, iPhone or iPad), you can either:
- write inline asm statements in C/C++/Objective-C code, or
- write standalone Assembly functions in a '.s' file and simply add it in your XCode sources, or
- write standalone Assembly functions for an external assembler. You write Assembly code in a '.s' file and generate a '.o' object file 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 many Assembly functions then I'd recommend a standalone Assembly file for GCC, or an external assembler such as FASMARM.
Once you have setup your assembler environment, you need to learn how to write ARM Assembly code, since iPhones and pretty much all portable devices, smartphones & tablets use the ARM instruction set. Some good intro tutorials to learn ARM Assembly are:
- A very good but old intro at Coranac's Whirlwind Tour of ARM Assembly,
- A recent intro including Thumb-2 instructions at DaveSpace's Introduction to ARM,
- Another good intro at BraveGNU's Embedded Programming with the GNU Toolchain.
- A brief introduction at WebShaker's Begin Programming Assembler with GCC,
- And if you specifically want to use GCC inline assembler then you should read Ethernut's ARM GCC Inline Assembler Cookbook.
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 (700 pages long) that explains the instruction timing, etc for your specific CPU. There is also a recent ARM Cortex-A Programmer's Guide, containing useful info and comparisons of Cortex-A8, Cortex-A9, Cortex-A5 and Cortex-A15 CPUs.
It is important to understand that many ARM CPU's include the NEON Advanced SIMD coprocessor (aka NEON or Media Processing Engine), and so if you expect to run operations that can take advantage of SIMD architecture (eg: heavily data parallel tasks), then you should make it a big priority to learn how to use NEON effectively! As mentioned above, the official ARM Architecture Reference Manual and ARM Cortex-A8 Reference Manual are the most important sources of info, but there are other places for quicker info such as:
- The List of NEON Instructions,
- The official ARM Tech Forum,
- An example using NEON for optimization at Hilbert-Space.de,
- Another blog that includes some NEON for iPhone by Wandering Coder,
- An official ARM blog with an intro on NEON at "Coding For NEON,
- ARM's Fastest memcpy() implementation,
- A forum post with an Even faster memcpy() implementation,
- An experimental ARM Cortex-A8 cycle counter online tool,
- A discussion on How to efficient shrink an image by 50% or 25%,
- Some hints on how to use NEON for Floating Point Optimization and Assembly Code Optimization,
- Many good ARM Assembly links collected by dpt,
- A list of many Bit Twiddling Hacks that might help you reduce some "if" statements in your SIMD code, etc.
- A 270-page tutorial for beginners on Intel x86 and ARM assembly topics and compiler / reverse engineering Quick Introduction to Reverse Engineering for Beginners by Dennis Yurichev.

浙公网安备 33010602011771号