您的位置:首页 > 其它

How to write Assembly code for the iPhone:

2012-05-17 13:06 447 查看


How to write Assembly code for the iPhone:

To write Assembly language code for an iOS device (the iPhone, iPad or newer iPods), 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, because the iPhone (and pretty much all portable devices, smartphones & tablets) use the ARM instruction set. There is a very good but old intro to ARM Assembly
athttp://www.coranac.com/tonc/text/asm.htm. This is a good way to learn the basics of ARM Assembly from scratch, and then you can target specific features for your device
such as NEON or Thumb-2 or multi-cores. There is also a brief introduction to writing ARM Assembly code with GCC at Begin Programming
Assembler with GCC, and there is also the ARM GCC Inline Assembler Cookbook if using the GCC inline assembler.

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 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: