Hello, Assembly World in Haiku x64

Vegard Wærp
2 min readJun 8, 2019

Note: This post builds on my previous post. If you are new to assembly, read that first.

Having created a simple Haiku Hello, World program in x86 assembly, and then made some shellcode for Haiku, the next step in my journey to learn a bit more Assembly is to create a simple Hello, World application in x64 Assembly.

I’m still using Haiku Beta1, but this time I am using the x86_64 version.

When writing Haiku assembly that calls external functions, a big difference between x86 and x86_64 is that arguments are passed on the stack for x86, while they are passed in registers for x86_64. When trying to find out which registers to use, some googling and trial showed that the registers to use where the same as the one used by Linux and *BSD:

Standard function calls:

  • Integers and memory addresses: RDI, RSI, RDX, RCX, R8, and R9
  • Floating point arguments: XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6 and XMM7
  • Additional arguments are passed on the stack

Syscalls:

  • For syscalls, the R10 register is used instead of RCX

In addition to the passing of arguments, another difference between 32 bits and 64 bits on Haiku is that the syscall opcode is used instead the 99 interrupt, and the syscall number is put in the rax register.

With this info, the last thing we have to check is that the syscall numbers are the same for Haiku x86_64 as they were for Haiku x86.

The file with the syscall numbers , syscalls.S.inc, is generated using the following command:

git clone https://git.haiku-os.org/haiku
cd haiku
./configure --target-arch x86_64
cd src/system/libroot
jam

After running the command, we see that the syscall numbers are the same as for Haiku x86, which I uploaded to this Github gist.

Now we should have all we need for porting our “Hello, World” program to x86_64, and after applying the needed changes, we come up with this:

And when we run it, it prints “Hello, world from Haiku x64 syscall”

Hello, World in Haiku x86_64 assembly

--

--