What is the proper way to pass arguments to printf with multiple variables using ARM64 Apple Silicon? Example: fOutputStr: .asciz "Element[%d] = %d\n" // formated output string
for an output of: Element[4] = 9 This code (see below) works on Raspberry Pi 5, on my Mac Studio, I am getting this output,
Element[9] = 1871655168
What do I need to do to use printf from an assembly language call with multiple variables?
I am using the following code. ` .align 2 // memory alignment model for 64-bit ARMc
#if defined(APPLE) .global _main // Provide program starting address to linker Mac OS _main: #else .global main // Raspian and Linux main: #endif
// stack frame work setup START
stp x29, x30, [sp, -16]!
str x20, [sp, -16]!
mov x29, sp
// stack frame work setup END
// setup printf call
#if defined(APPLE) adrp x0, fOutputStr@PAGE add x0, x0, fOutputStr@PAGEOFF #else ldr x0, =fOutputStr #endif
mov w1, #4
mov w2, #9
print_brk:
#if defined(APPLE)
stp X0, X1, [SP, #-16]!
stp X2, X3, [SP, #-16]!
bl _printf
ldp X2, X3, [SP], #16
ldp X0, X1, [SP], #16
#else bl printf #endif
done: // closing stack frame work ldr x20, [sp],16 ldp x29, x30, [sp],16
// exit
mov w0, wzr
ret
.data
.align 4
// intArrayPtr: .word 3,7,5,2,4,8 // word, each value is offset by 4 fOutputStr: .asciz "Element[%d] = %d\n" // formated output string
Some more hints and tips in this space:
- The easiest way to determine the actual calling conventions for a given function is to create a small project in Xcode that calls the function and then looking at the resulting assembly.
- The Product > Perform Action > Assemble command is super helpful in that case.
- But you can also run the program and choose Debug > Debug Workflow > Always Show Disassembly [1].
- Apple documents its calling conventions in Xcode > Application binary interfaces. This is basically Arm’s Procedure Call Standard with a few tweaks.
- And one of this tweaks affects varags.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] It says something about me that I have a hot key for that (-: