FreeBSD 64bits convention call documentation

Viewed 75

I am running FreeBSD 11.0.

The following from the FreeBSD manual does NOT print the "Hello, World!" message:

section .text
hello db 'Hello, World!, 0Ah
hbytes equ $-hello

_syscall:
    int 80h
    ret

global _start
_start:
    push dword hbytes
    push dword hello
    push dword 1   ; stdout
    mov rax, 4    ; write syscall
    call _syscall
    add rsp, byte 24 ; restore stack
    push word 0      ; return 0
    mov rax, 1       ; exit call
    call _syscall

But this works:

section .text
hello db 'Hello, World!, 0Ah
hbytes equ $-hello

_syscall:
    int 80h
    ret

global _start
_start:
    mov rdi, 1
    mov rsi, hello  ; appears to be magic
    mov rdx, hbytes ; appears to be magic
    mov rax, 4    ; write syscall
    call _syscall

    push word 0      ; return 0
    mov rax, 1       ; exit call
    call _syscall

This raises couple questions:

1) Why doesn't the first approach work?

The UNIX calling convention is push data on the stack. Program does not crash. I just don't get any output, and the program terminates. I am compiling and linking fine.

2) How are we supposed to know about what registers to load, and with what values?

If I was pushing on the stack, it is easy. I look up the C functions and then I know how to push data.

In this case, it works like magic.

3) Where is the documentation for FreeBSD for similar system calls (not utilizing stack)??!

0 Answers
Related