FreeBSD 64bits Conditional Calling Documentation

I am running FreeBSD 11.0.

The following from the FreeBSD manual does NOT print "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

      

A couple of questions arise:

1) Why doesn't the first approach work?

The UNIX calling convention is push data on the stack. The program doesn't crash. I just don't get any output and the program exits. I compile and link perfectly.

2) . How are we supposed to know which registers are loaded and which values?

If I push to the stack it is easy. I go through C functions and then I know how to pass data.

In this case, it works like magic.

3) Where is the FreeBSD documentation for such system calls (not using the stack) ?!

+3


source to share





All Articles