x86_64 assembly exit system call parameter on macOS Mojave?
Asked Answered
O

1

5

I have the following file:

; hello.s

.section __TEXT,__text
.globl _main
_main:
    movl $0x2000001, %eax
    movl $42, %ebx
    syscall

I try to run it as follows:

# run.sh

as -mmacosx-version-min=10.9 hello.s -o hello.o
ld -macosx_version_min 10.9 -lSystem hello.o -e _main -o hello
./hello
echo $?

The output is:

$ ./run.sh
1

I expect it to be

$ ./run.sh
42

What's wrong here?

Edit:

Based on the answer from zneak, we need to use the %edi register for syscalls, so the working program is:

; hello.s

.section __TEXT,__text
.globl _main
_main:
    movl $0x2000001, %eax
    movl $42, %edi
    syscall
Oconner answered 27/2, 2019 at 21:37 Comment(1)
It’s not a big deal, but next time you want to provide an answer to your question, rather than editing your question to include the answer, feel free to just post your own answer. Go ahead and keep zneak’s as the “accepted” answer (as he really deserves the credit), but feel free to post your own if you think you can further illuminate the topic and help future readers.Legalize
D
8

System calls on 64-bit macOS use the System V ABI, so you need to write your first parameter to %edi instead of %ebx. Just like for normal calls, the argument registers for syscalls are rdi, rsi, rdx, rcx, r8, r9.

Currently, you get 1 because rdi contains the argc parameter of main, and the shell invokes your program with one argument.

Deucalion answered 27/2, 2019 at 21:41 Comment(2)
thanks, the guide I'm reading is way off the mark then. time to find a new guide.Oconner
Good luck! While we're at it, if that's of any interest to you, you can build .s files using clang, which will also invoke the linker for you: clang -o hello hello.s && ./helloDeucalion

© 2022 - 2024 — McMap. All rights reserved.