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 w
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.
So, here is a quick description of how to actually include ARM asm code into an Xcode project, this is tested in Xcode versions up to 4.3. For my specific project, I wanted to define a function in ASM code saved with a filename like "decode_arm.s". I already have a C implementation of the same function that gets compiled when run under the simulator, so the conditional cpp logic here ensures that the ARM ASM code is only compiled into the project when actually compiling for the device.
// This file implements the following C functions for the ARM platform.
// Both ARM6 and ARM7 devices are supported by this implementation.
//
// maxvid_decode_c4_sample16()
#if defined(__arm__)
# define COMPILE_ARM 1
# if defined(__thumb__)
# define COMPILE_ARM_THUMB_ASM 1
# else
# define COMPILE_ARM_ASM 1
# endif
#endif
#if defined(COMPILE_ARM)
# define USE_GENERATED_ARM_ASM 1
#endif // COMPILE_ARM
#if defined(USE_GENERATED_ARM_ASM)
.section __TEXT,__text,regular
.section __TEXT,__textcoal_nt,coalesced
.section __TEXT,__const_coal,coalesced
.section __TEXT,__picsymbolstub4,symbol_stubs,none,16
.text
.align 2
.globl _maxvid_decode_c4_sample16
.private_extern _maxvid_decode_c4_sample16
_maxvid_decode_c4_sample16:
@ args = 0, pretend = 0, frame = 0
@ frame_needed = 1, uses_anonymous_args = 0
stmfd sp!, {r4, r5, r6, r7, lr}
add r7, sp, #12
stmfd sp!, {r8, r10, r11}
(ASM CODE HERE)
ldmfd sp!, {r8, r10, r11}
ldmfd sp!, {r4, r5, r6, r7, pc}
.subsections_via_symbols
#else
// No-op when USE_GENERATED_ARM_ASM is not defined
#endif
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/Objective-C code to wrap up your asm stubs, as writing 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.
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.