FreeBSD System Calls in Assembly

Makketron makketronics at gmail.com
Mon Jul 31 20:00:00 UTC 2017


Hello,
It seems that the  documentation here doesn't apply for 64-bits.

https://www.freebsd.org/doc/en/books/developers-handbook/x86.html

I asked a question on stackoverflow. I thought I should ask it here too
https://stackoverflow.com/questions/45423987/freebsd-64bits-convention-call-documentation

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)??!

Thank you.


More information about the freebsd-questions mailing list