This is a basic setup required to run assembly code on iPhone. iPhone processor has ARM64 architecture. The system call numbers are different from macOS, but rest of the process is similar. There is no easy way to assemble, load and run a standalone assembly file on iPhone. Instead the approach I use here is to create a stub Objective-C project and call into a function written in assembly from Objective-C or C code.
1. Create an iOS Objective-C project.
2. Create a file hello.s
with the following content.
.global _hello
_hello:
mov x0, 42
ret
Here we move the number 42
to x0
register and return the function. This is similar to int hello()
in C.
3. In AppDelegate.h
declare an extern
function like:
extern int hello(void);
4. Next we invoke this function from AppDelegate.m
like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"hello %d", hello());
return YES;
}
This will print hello 42
to the console.
Replace hello.s
with other assembly prototypes and run it like a normal iOS project. This requires running the project directly on an iPhone because of the ARM64 architecture. Maybe it will run on Apple Silicon mac, but on Intel mac, since the simulator architecture is not ARM64, the build will fail.