Skip to main content

Hello World (first programs) · Intermediate · 18 min

Hello World in x86-64 assembly with NASM and Linux system calls

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 (amd64) · NASM 3.01 · GNU ld (binutils)

You will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.

On this page

Every program you have ever run ends up as machine code: short instructions the processor reads straight from memory. Assembly language is that machine code written as words instead of bytes, one instruction per line. There is no compiler deciding things for you. You choose which register holds which value, and you ask the operating system for help directly.

In this tutorial you write Hello World for 64-bit Intel and AMD processors (x86-64) on Linux, with no C library at all. Printing a line and exiting are two requests to the Linux kernel, called system calls. You assemble the source with NASM, the Netwide Assembler, link it with ld from GNU binutils, and then look at the bytes you produced.

Assembly is not something most developers write every day. It is used for boot loaders and the first instructions an operating system runs, for hand-tuned inner loops in video codecs and cryptography libraries, and above all for reading: security analysts read disassembly when they take apart malware, and engineers read it in crash dumps when the source is not available. Writing a small program yourself is the quickest way to make that reading possible.

You work in a throwaway folder, ~/hello-asm-demo, and delete it at the end.

Step 1 · Check your processor and install the tools

This program only runs on an x86-64 processor running Linux. Check first:

Bash
uname -m
case "$(uname -m)" in
  x86_64) echo "x86-64: you can follow this tutorial" ;;
  *) echo "This processor is $(uname -m), not x86_64. See the arm64 note below."; false ;;
esac

Then install NASM, binutils (which provides ld, objdump and readelf) and GCC, which you use once at the end to compare sizes with a C program:

Bash
sudo apt update
sudo apt install -y nasm binutils gcc libc6-dev
nasm -v
ld --version | head -n 1

Ubuntu 26.04 packages NASM 3.01. The newest upstream release is 3.02, from June 2026; nothing in this tutorial depends on the difference.

On an arm64 machine (Apple silicon Macs, Raspberry Pi, many cloud servers and most Android phones), this code will not run. The processor understands a different instruction set, so every instruction changes: the system call number goes in register x8 instead of rax, the arguments go in x0, x1 and x2, the instruction is svc #0 instead of syscall, and even the numbers differ (write is 64 and exit is 93 on arm64 Linux). NASM does not produce arm64 code at all; you would use the GNU assembler as instead. On an Apple silicon Mac, run an x86-64 Linux virtual machine, or read along.

On macOS on an Intel processor, the idea is the same but the details are not: NASM must output Mach-O files (-f macho64) instead of ELF, the system call numbers are different, and Apple does not promise those numbers stay the same between releases, so real macOS programs go through its system library. On Windows, use WSL with Ubuntu on an x86-64 PC.

Check it worked

The first command prints x86_64 and the x86-64: you can follow this tutorial line. nasm -v prints something like NASM version 3.01, and the ld line starts with GNU ld. If the processor check printed aarch64 or arm64, stop here and use the note above.

Step 2 · Write hello.asm

Create the demo folder and the source file. In NASM, a semicolon starts a comment that runs to the end of the line:

Bash
mkdir -p ~/hello-asm-demo
cd ~/hello-asm-demo
cat > hello.asm <<'ASM'
; hello.asm: print one line and exit, using Linux system calls only

section .data
msg:    db "Hello, world!", 10    ; the bytes to print; 10 is the newline
len:    equ $ - msg               ; how many bytes that is (14)

section .text
global _start

_start:
        mov rax, 1                ; system call 1 is write
        mov rdi, 1                ; argument 1: file descriptor 1, standard output
        mov rsi, msg              ; argument 2: address of the first byte
        mov rdx, len              ; argument 3: number of bytes
        syscall                   ; ask the kernel to do it

        mov rax, 60               ; system call 60 is exit
        mov rdi, 0                ; argument 1: exit status 0 means success
        syscall
ASM

A program is split into sections. The kernel loads each one into memory with its own permissions:

  • section .data holds data the program can read and change. .text holds the instructions and is loaded as read-only and executable.
  • msg: is a label, a name for an address in memory. db means "define bytes": the 13 characters of the string, then a byte with value 10, which is the newline character. There is no zero at the end; the kernel is told the length instead.
  • len: equ $ - msg defines a constant. $ means "the current address", so $ - msg is the distance from the start of the string to here: 14 bytes. NASM works it out, so you never count by hand.
  • global _start makes the label _start visible to the linker. _start is the name ld looks for as the program's first instruction. In C, the C library provides _start and it calls your main; here there is no C library, so you write _start yourself.

Registers and the system call rules

The processor has 16 general-purpose registers, each 64 bits wide: rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp and r8 to r15. They are the processor's working storage, far faster than memory. mov rax, 1 copies the number 1 into rax.

Linux on x86-64 has a fixed rule, its system call interface, for how a program asks the kernel for something:

  • Put the system call number in rax. Linux numbers them in a table: write is 1 and exit is 60.
  • Put the arguments in rdi, rsi, rdx, r10, r8 and r9, in that order.
  • Run the syscall instruction. The processor switches to the kernel, which does the work and puts the result in rax.
  • The kernel may overwrite rcx and r11. Do not keep anything you need in them across a syscall.

So the first block is the same request that C's write(1, msg, 14) makes, and the second is exit(0). Without the exit call, the processor would carry on reading whatever bytes come after your code and treat them as instructions. You see what happens then in the last step.

Check it worked

cat ~/hello-asm-demo/hello.asm shows the file, starting with the ; hello.asm: comment and ending with the second syscall.

Step 3 · Assemble, link and run

Two programs turn the source into something Linux runs. NASM turns each instruction into bytes and writes an object file. ld then decides where everything goes in memory and writes an executable:

Bash
cd ~/hello-asm-demo
nasm -f elf64 -o hello.o hello.asm
ld -o hello hello.o
./hello
echo "exit status: $?"
  • -f elf64 asks for a 64-bit ELF object file, the format Linux uses for programs and libraries. Without -f, NASM falls back to its plain bin format, which assumes 16-bit code, so it rejects the 64-bit registers in this file and stops before ld ever runs.
  • ld -o hello hello.o links the single object file into an executable called hello. ld marks the output as executable, so you do not need chmod +x.
  • $? holds the exit status of the last command: the number you put in rdi before the exit call.

Now ask what you built:

Bash
cd ~/hello-asm-demo
readelf -h hello | grep -E 'Class|Type|Machine|Entry'
ldd hello || true

readelf -h prints the ELF header: a 64-bit file (ELF64), a plain executable (EXEC), for the x86-64 machine, and the entry point address where _start landed. ldd reports not a dynamic executable because the program loads no shared libraries. The || true is there because ldd exits with an error status for such files.

Check it worked

The program prints Hello, world! and the next line is exit status: 0. readelf shows ELF64, EXEC (Executable file) and Advanced Micro Devices X86-64. To see the exit status change, edit hello.asm, set mov rdi, 0 to mov rdi, 42, assemble, link and run again: the status reads 42. Put it back to 0 before you continue.

Step 4 · Print the line three times with a loop

Assembly has no for or while. A loop is a counter, a compare and a jump back. Write a second program:

Bash
cd ~/hello-asm-demo
cat > hello3.asm <<'ASM'
; hello3.asm: print the line three times

section .data
msg:    db "Hello, world!", 10
len:    equ $ - msg

section .text
global _start

_start:
        mov rbx, 3                ; loop counter, kept in rbx
.again:
        mov rax, 1                ; write
        mov rdi, 1                ; to standard output
        mov rsi, msg
        mov rdx, len
        syscall

        dec rbx                   ; counter = counter - 1
        jnz .again                ; jump back if the counter is not zero

        mov rax, 60               ; exit
        mov rdi, 0
        syscall
ASM
nasm -f elf64 -o hello3.o hello3.asm
ld -o hello3 hello3.o
./hello3

What is new:

  • mov rbx, 3 sets the counter. It lives in rbx because syscall leaves rbx alone. If you used rcx, the kernel would overwrite it on every write, and the loop would go wrong.
  • .again: is a local label. The leading dot ties it to the last ordinary label, _start, so another function can have its own .again without a clash.
  • The four mov lines are repeated inside the loop because write puts its result (the number of bytes written) in rax, replacing the 1 you set.
  • dec rbx subtracts one. Like most arithmetic instructions, it also sets the processor's flags, including the zero flag when the result is 0.
  • jnz .again means "jump if not zero": it reads that flag and jumps back while the counter is still above zero.

Check it worked

The program prints Hello, world! on three lines and nothing else. Change mov rbx, 3 to mov rbx, 5, rebuild with the nasm and ld lines, and you get five.

Step 5 · Read the machine code with objdump

objdump -d disassembles: it reads the bytes back and prints the instructions they stand for. -M intel shows them in Intel syntax, the same operand order NASM uses (destination first):

Bash
cd ~/hello-asm-demo
objdump -d -M intel hello3

Each line has three parts: the address in memory, the bytes in hexadecimal, and the instruction. Things to look for:

  • syscall is always the two bytes 0f 05. There are two of them, one inside the loop and one for exit.
  • Your mov rax, 1 may show as mov eax,0x1 with the bytes b8 01 00 00 00. NASM shortens it, because writing a 32-bit register clears the top half of the 64-bit one, and the shorter form saves bytes.
  • jnz appears as jne. They are two names for the same instruction, and objdump picks the second. Its bytes include a small negative number: the distance back to .again.
  • The label .again appears as _start.again, which shows how NASM names local labels.

This is the view a security analyst works from when there is no source code: addresses, bytes and instructions, with the meaning reconstructed by hand.

Now compare the size with the same program in C:

Bash
cd ~/hello-asm-demo
printf '#include <stdio.h>\nint main(void) { puts("Hello, world!"); return 0; }\n' > hello.c
gcc -O2 -o hello-c hello.c
./hello-c
wc -c hello hello-c

The C version is larger, and it is not the whole story: it also loads the C library (libc.so.6, well over a megabyte) when it starts, and runs start-up code before main. Most of the size of the assembly version is ELF headers and padding that line sections up on memory page boundaries, not your code; the code itself is the few dozen bytes objdump showed.

Check it worked

objdump shows a <_start>: heading, two lines containing 0f 05 syscall, and a jne line. wc -c reports the assembly hello as smaller than hello-c: something like 9 KB against around 16 KB, though exact sizes vary with the linker version.

Step 6 · Recognise the mistakes beginners make

Make two mistakes on purpose so you know them when they happen by accident.

A misspelt name. Write a copy of the program that refers to lenn instead of len, and assemble it:

Bash
cd ~/hello-asm-demo
cat > typo.asm <<'ASM'
section .data
msg:    db "Hello, world!", 10
len:    equ $ - msg
section .text
global _start
_start:
        mov rax, 1
        mov rdi, 1
        mov rsi, msg
        mov rdx, lenn
        syscall
        mov rax, 60
        mov rdi, 0
        syscall
ASM
nasm -f elf64 -o typo.o typo.asm || echo "nasm failed with status $?"

NASM prints an error naming the file, line 10 and the symbol lenn, and writes no object file. Fix the spelling at that line number.

Forgetting to exit. Write a program that prints but never calls exit:

Bash
cd ~/hello-asm-demo
cat > noexit.asm <<'ASM'
section .data
msg:    db "Hello, world!", 10
len:    equ $ - msg
section .text
global _start
_start:
        mov rax, 1
        mov rdi, 1
        mov rsi, msg
        mov rdx, len
        syscall
ASM
nasm -f elf64 -o noexit.o noexit.asm
ld -o noexit noexit.o
./noexit || echo "exit status: $?"

The line prints, then the processor runs on into the zero bytes after your code, treats them as an instruction that writes to a memory address the program does not own, and the kernel stops it with a segmentation fault. Status 139 is 128 plus 11, the number of the SIGSEGV signal. C programs never hit this, because the C library's start-up code calls exit for you when main returns. (It uses exit_group, system call 231, which also ends any other threads; for a single-threaded program exit does the same job.)

Two more to know about:

  • Mixing up 32-bit and 64-bit code. Older tutorials use int 0x80 with eax = 4 for write. That is the 32-bit interface, with different numbers and different registers. In a 64-bit program use syscall and the 64-bit numbers.
  • Skipping -f elf64. Without it, NASM uses its default bin format, which starts in 16-bit mode. It then rejects mov rax, 1 and the other 64-bit instructions with an error something like instruction not supported in 16-bit mode, and no object file is made for ld to link.

Check it worked

You have seen NASM's error naming lenn, and ./noexit printed Hello, world! followed by exit status: 139. Your shell may also print Segmentation fault (core dumped).

Step 7 · Clean up

Delete the demo folder:

Bash
cd ~
rm -rf ~/hello-asm-demo

To remove the tools too, run sudo apt remove nasm. Leave binutils and GCC installed if you compile anything else; many other packages depend on them.

Check it worked

ls ~/hello-asm-demo reports No such file or directory.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.