1
votes

It appears that the usual approach to calling printf from aarch64 asm code that works just fine on Linux does not work on MacOS running on the Apple M1.

Is there any documentation that explains what has changed?

I find that the parameters that I put in x0..x2 are getting garbled in the printf output.

1
Show your code. Haw can we answer without seeing your code?0___________
printf does not take arguments in x1 or x2...Siguza
@Siguza Of course it does. Do you not know what printf is??asdfasdf1001
I found developer.apple.com/documentation/xcode/… which does document some differences from the usual ARM64 conventions. Of course, you can also compile a printf call with your C compiler on MacOS and see how it looks.Nate Eldredge
In particular, if I'm reading right, it expects all variadic arguments to be passed on the stack? I didn't know that. If that's so then @Siguza is right: the format string pointer would go in x0 and everything else on the stack.Nate Eldredge

1 Answers

0
votes

The Darwin arm64 ABI passes all varags arguments on the stack, each padded to the next multiple of 8 bytes.

Here's a simple example:

.globl _main
.align 2
_main:
    stp x29, x30, [sp, -0x10]!    

    mov x8, 66
    str x8, [sp]
    adr x0, Lstr
    bl _printf

    mov w0, 0
    ldp x29, x30, [sp], 0x10
    ret

Lstr:
    .asciz "test: %x\n"