Hello world in Linux x86-64 assembly

A “hello world” program writes to stdout (calling write) then exits (calling exit). The assembly program hello.s below does that on Linux x86-64.

global _start

section .text

_start:
  mov rax, 1        ; write(
  mov rdi, 1        ;   STDOUT_FILENO,
  mov rsi, msg      ;   "Hello, world!\n",
  mov rdx, msglen   ;   sizeof("Hello, world!\n")
  syscall           ; );

  mov rax, 60       ; exit(
  mov rdi, 0        ;   EXIT_SUCCESS
  syscall           ; );

section .rodata
  msg: db "Hello, world!", 10
  msglen: equ $ - msg

To run it:

$ nasm -f elf64 -o hello.o hello.s
$ ld -o hello hello.o
$ ./hello
Hello, world!

The first important document is the x86-64 ABI specification, maintained by Intel. (Weirdly, the official location for the ABI specification is some random dude’s personal GitHub account. Welcome to the sketchy world of assembly.) The ABI specification describes system calls in the abstract, as it applies to any operating system. Importantly:

The second document is the Linux 64-bit system call table. This specifies the system call number for each Linux system call. For our example, the write system call is 1 and exit is 60.

Finally, you want the man pages for the system calls, which tell you their signature, e.g.:

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

Armed with this, we know to:

I just released Vidrio, a free app for macOS and Windows to make your screen-sharing awesomely holographic. Vidrio shows your webcam video on your screen, just like a mirror. Then you just share or record your screen with Zoom, QuickTime, or any other app. Vidrio makes your presentations effortlessly engaging, showing your gestures, gazes, and expressions. #1 on Product Hunt. Available for macOS and Windows.

With Vidrio

With generic competitor

More by Jim

Tagged #programming, #unix, #assembly. All content copyright James Fisher 2018. This post is not associated with my employer. Found an error? Edit this page.