Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 40 ยท Projects

Project: Hello World the Real Way

Build a real hello world in x86-64 and ARM64 Linux assembly with raw system calls, step by step, then run a matching toy version right here.

In C, printf("Hello, world!\n") hides a surprising amount of machinery. Underneath, a library formats the text, fills a buffer and eventually asks the operating system to put bytes on your terminal. In this project you skip every layer except the last one. You write a program with no C library, no runtime and no main: just a few instructions that talk to the Linux kernel directly.

You will write the program twice, once for x86-64 and once for ARM64, and learn the exact commands to assemble, link, run and inspect it on a real machine. Those blocks are read-only, because a browser cannot assemble native code. Then you will run the same idea in the toy CPU from Meet the Toy CPU, extended with a tiny syscall instruction, and grow it into something more useful: a program that measures a string and one that prints a number.

What you need

On a real machine you need a Linux system (a VM, WSL2 or a server is fine) with nasm for x86-64, or the GNU assembler as and the linker ld for either architecture. For ARM64 code on an x86-64 PC, install your distribution's cross binutils (aarch64-linux-gnu-as and aarch64-linux-gnu-ld) and qemu-user, which runs ARM64 programs on the fly. Nothing here needs root access.

The program has only two jobs. First write 14 bytes to file descriptor 1 (standard output). Then exit, because there is no main to return from: if you forget to ask the kernel to end the process, the CPU simply keeps executing whatever bytes follow your last instruction, and the program typically crashes.

flowchart LR
  A["hello.asm"] -->|"nasm -f elf64"| B["hello.o<br/>object file"]
  B -->|"ld"| C["hello<br/>ELF executable"]
  C -->|"kernel loads it,<br/>jumps to _start"| D["write(1, msg, 14)"]
  D --> E["exit(0)"]

From source file to running process: an assembler, a linker, then the kernel starts at _start.

Step 1: x86-64 Linux with NASM

Save this as hello.asm. It uses Intel syntax, which Assemblers, Syntax Flavors and Object Files explains in detail.

NASM
global _start               ; make _start visible to the linker

section .data
msg:    db "Hello, world!", 10   ; 13 letters and punctuation, then a newline (10)
len:    equ $ - msg              ; assembler computes the length: 14

section .text
_start:
    mov   eax, 1            ; system call number 1 = write
    mov   edi, 1            ; 1st argument: file descriptor 1 (stdout)
    mov   esi, msg          ; 2nd argument: address of the bytes
    mov   edx, len          ; 3rd argument: how many bytes
    syscall                 ; ask the kernel

    mov   eax, 60           ; system call number 60 = exit
    xor   edi, edi          ; 1st argument: exit status 0
    syscall

Every line follows the Linux x86-64 convention. The number of the call goes in rax; the arguments go in rdi, rsi, rdx (then r10, r8, r9), in that order; the result comes back in rax. For write the kernel's definition is write(fd, buffer, count), so file descriptor, address, length. The 32-bit register names such as edi are enough here, because writing a 32-bit register clears the upper half. The program is not a function, so it must not end with ret.

Build and run it:

Terminal
nasm -f elf64 hello.asm -o hello.o    # assemble: text to an object file
ld hello.o -o hello                   # link: pick _start as the entry point
./hello                               # prints: Hello, world!
echo $?                               # prints the exit status: 0

Change xor edi, edi to mov edi, 42, rebuild, and echo $? now prints 42. That is the whole mechanism behind a process's exit code in a shell script.

Look at what you built

Three commands let you check the result without trusting the source file:

Terminal
strace ./hello         # list every system call the process makes
objdump -d -M intel hello    # disassemble the machine code
wc -c hello            # file size: a few kilobytes at most

strace shows exactly two interesting lines: a write with the file descriptor 1 and the byte count 14, and an exit. A C hello world shows dozens of extra calls from the dynamic loader and the C library. In the disassembly your eight instructions are a few dozen bytes of machine code; the rest of the file is headers and padding.

Step 2: ARM64 Linux with GNU as

ARM64 uses a different instruction set and a different system call convention, but the program has the same shape. Save this as hello.s:

ASM
    .global _start

    .section .data
msg:
    .ascii "Hello, world!\n"
    .equ len, . - msg           // length: 14

    .section .text
_start:
    mov   x0, #1                // 1st argument: fd 1 (stdout)
    ldr   x1, =msg              // 2nd argument: address of the bytes
    mov   x2, #len              // 3rd argument: length
    mov   x8, #64               // system call number 64 = write
    svc   #0                    // supervisor call: enter the kernel

    mov   x0, #0                // 1st argument: exit status 0
    mov   x8, #93               // system call number 93 = exit
    svc   #0

On ARM64 Linux the number goes in x8, the arguments in x0 to x5, and the result comes back in x0. The instruction that enters the kernel is svc instead of syscall. The numbers also differ: write is 1 on x86-64 but 64 on ARM64, and exit is 60 on x86-64 but 93 on ARM64, because each architecture's Linux port has its own table. ldr x1, =msg is an assembler convenience that places the address in a nearby constant pool and loads it, since an ARM64 instruction is only 32 bits wide and cannot hold a 64-bit address (see ARM64 (AArch64) Basics). Comments use // here because ; means something else in some ARM assemblers.

Build and run it on an ARM64 Linux machine (a Raspberry Pi, a cloud ARM server, or an ARM Linux VM on an Apple silicon Mac):

Terminal
as hello.s -o hello.o
ld hello.o -o hello
./hello

On an x86-64 PC, use the cross tools and let QEMU run the result:

Terminal
aarch64-linux-gnu-as hello.s -o hello.o
aarch64-linux-gnu-ld hello.o -o hello
qemu-aarch64 ./hello

macOS, the BSDs and Windows use different system call conventions, and Windows does not offer a stable system call interface at all (programs call DLL functions instead), so this exact source is Linux only. The idea carries over: put a number and arguments in the registers your platform specifies, then trap into the kernel. The System Calls lesson goes deeper into user mode, kernel mode and the Windows story.

Step 3: the toy version

The toy CPU has no operating system, so we give it a miniature one. Two additions, both marked in the interpreter at the bottom of each TryIt: a data option that places strings and arrays in memory and defines their names (msg, plus msg_len for the length), and a syscall instruction with the two calls you just used: rax = 1 writes rdx characters starting at the address in rsi to standard output (rdi must be 1), and rax = 60 ends the program with the status in rdi. Each memory cell holds one character, where real memory holds one byte per address.

JS Hello, world with write and exit

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The program printed its text, then the harness line reports status 0 and the instruction count: eight instructions, the same count as the x86-64 listing above if you do not count the label lines. Change mov rdi, 0 in the exit sequence to mov rdi, 42 and the status changes with it.

Measuring the string yourself

Real programs often do not know how long a string is. The C library answers with strlen, which walks the bytes until it finds a zero. Here is the same loop in front of the write call. Because the toy data option terminates every string with a 0 cell, the loop can find the end:

JS Compute the length, then write

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Edit the message and the length follows automatically. The instruction count grows with the string, because the loop executes four instructions for every character: that is why strlen is a classic place for compilers and libraries to use vector instructions, which you meet in Floating Point and SIMD.

Printing a number

write only knows about characters, so to print the number 2025 you must turn it into the characters 2, 0, 2, 5 first. The classic method divides by 10 repeatedly: the remainder is the last digit, and adding 48 (the ASCII code of 0) turns a digit into its character. Digits come out last-to-first, so the program fills a buffer from the end and writes from wherever it stopped.

JS Print a number with div and write

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the loop: xor rdx, rdx before div is not decoration. On x86-64 div divides the 128-bit pair rdx:rax and raises an error if the quotient does not fit, so a stale value in rdx would break the answer. Try changing the number to 7, 90210 or 0. Zero still prints a digit, because the loop is a do-while: it always runs the body once before it tests whether any digits are left.

Real code uses the same two ideas at a larger scale. A C library's printf calls a formatting routine, which fills a buffer much like the one above, and then makes one write system call for the whole buffer, because each system call costs far more than an ordinary instruction.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. On x86-64 Linux, where does a program put the system call number before executing syscall?

    The number goes in rax; the arguments go in rdi, rsi, rdx, r10, r8 and r9. The result comes back in rax.
  2. 2. Why does the hello world program end with an exit system call instead of ret?

    The kernel starts the process at _start without pushing a return address. Without an exit call the CPU would keep executing whatever bytes follow, which usually crashes.
  3. 3. Which pair is correct for write and exit on ARM64 Linux?

    ARM64 Linux uses its own table: write is 64 and exit is 93. The number is passed in x8, the arguments in x0 to x5, and svc enters the kernel.
  4. 4. In the print-a-number program, why is xor rdx, rdx placed before div rbx?

    div uses the register pair rdx:rax as the dividend. A leftover value in rdx would change the quotient or even cause a divide error.

Summary

  • A "no libc" program needs only _start, a write system call and an exit system call.
  • x86-64 Linux: number in rax, arguments in rdi, rsi, rdx, r10, r8, r9, enter with syscall; write = 1, exit = 60.
  • ARM64 Linux: number in x8, arguments in x0 to x5, enter with svc #0; write = 64, exit = 93.
  • Build with nasm -f elf64 (or as) plus ld, run the result, read the exit status with echo $?, and inspect it with strace and objdump -d.
  • The toy CPU's syscall shows the same flow: put a number and arguments in registers, trap, get a result.
  • Printing a number means converting it to characters yourself: divide by 10, add 48, fill a buffer from the end, then write once.

Next: test yourself with 20 graded problems in Practice Exercises with Solutions.