Author Topic: FreeBSD 64 bit helloworld example.  (Read 10844 times)

Offline Mathi

  • Jr. Member
  • *
  • Posts: 82
  • Country: in
    • Win32NASM
FreeBSD 64 bit helloworld example.
« on: January 18, 2012, 06:16:53 PM »
I registered for a free online shell account and the box happened to be a FreeBSD amd64 machine.

Tried a helloworld program in nasm.
Finally got solved after i stumbled across this page.

http://thebrownnotebook.wordpress.com/2009/10/27/native-64-bit-hello-world-with-nasm-on-freebsd/

The only difference from the linux version was the difference in the syscall numbers.

Reference:
http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/kern/syscalls.master

Also check the calling convention details from
http://www.x86-64.org/documentation/abi.pdf

A.2 AMD64 Linux Kernel Conventions  (Page 123)
A.2.1 Calling Conventions


Code: [Select]
;; Assemble with
;; nasm -felf64 hello.asm
;; Link with
;; ld -o hello hello.o

section .data
    message db  "Hello World!",10
    MSGLEN equ $-message

section .text
    global _start

    _start:

        ; print the string using write() system call
        mov     rdx, MSGLEN
        mov     rsi, dword message
        mov     rdi, 1
        mov     rax, 4
        syscall

        ; exit from the application here
        mov     rax, 1
        xor     edi, edi
        syscall