“Hello World” in assembly language on Linux

Apr. 22, 2015

One of my side-projects this summer is to become minimally literate in assembly language. So I began by reading up a tutorial on the x86-64 and the Linux ABI.

And here for your reading pleasure is hello.s. For this example we need to know only of the following bits of the ABI:

  • The stack grows downwards (towards lower addresses) with %rsp pointing to the bottom of the stack.
  • The call instruction decrements %rsp by 8, stores the address of the instruction after itself on the stack and then jumps to the address pointed to by its operand.
  • The ret instruction jumps to the address pointed to by %rsp and increments %rsp by 8 before resuming execution.
  • Integer or pointer parameters to functions are passed in registers %rdi, %rsi, %rdx, %rcx, %r8 and %r9 in that order and only if there are more than six such parameters is the stack used. An integer or pointer return value is returned in %rax.

The program above calls printf and so has to be linked to the C library. I spent all afternoon feeding strange incantations to the GNU linker ld to create a working executable. While I finally did get something which worked, I did not feel confident that I was doing exactly right. Finally I wimped out and called gcc to do the linking, after seeing that even the famous GHC did this. Here’s the Makefile

Next target, learning more about the instruction set, specially the SIMD instructions.