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.
相关文章推荐
- How to Write Doc Comments for the Javadoc Tool
- Write Code as If You Had to Support It for the Rest of Your Life
- How to write the better Makefile for cpputest
- (转载)XML Tutorial for iOS: How To Choose The Best XML Parser for Your iPhone Project
- (OK) how to compile the source code for Android-x86 (Android 7.1.1)
- How To Choose The Best XML Parser for Your iPhone Project
- (转)How to Update Your Apps for the 4-Inch iPhone 5 Display
- How To Compile OpenSSL for the iPhone
- How to Use the Format Fortran Indent Plugin for Code::Blocks ?
- How to Write Doc Comments for the Javadoc Tool
- How To Choose The Best XML Parser for Your iPhone Project
- Documentation:Streaming HowTo/Streaming for the iPhone
- how to download the souce code for trdp?
- How To Choose The Best XML Parser for Your iPhone Project
- iphone app 开发:HOWTO build the toolchain for Iphone-dev
- How to write the fast code/ high performance in C#
- Write Code As If You Had to Support It for the Rest of Your Life
- how to download the source tree for a specific Android code-line
- How To Choose The Best XML Parser for Your iPhone Project
- How to Write Doc Comments for the Javadoc Tool