Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 31 Jul 2017 15:59:58 -0400
From:      Makketron <makketronics@gmail.com>
To:        freebsd-questions@freebsd.org
Subject:   FreeBSD System Calls in Assembly
Message-ID:  <CACAG1gq7LuvJu5LtWCwfM91YcxWrs2u1J9WzuNu7gGmj2+GmDg@mail.gmail.com>

Next in thread | Raw E-Mail | Index | Archive | Help
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.



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?CACAG1gq7LuvJu5LtWCwfM91YcxWrs2u1J9WzuNu7gGmj2+GmDg>