Skip to content
systemcoding.dev
Assembly

Write Your First x86-64 Program in NASM on Linux

Write, assemble and link a complete x86-64 program with NASM on Linux. It prints a line, exits with a status code, and uses no C library at all.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Intel, Linux and RISC-V logos.

A complete program that prints a line and exits is about ten instructions of assembly, and it needs no C library, no main and no runtime. That makes it the cleanest way to see what a Linux process really is: a block of bytes the kernel jumps into, which talks to the kernel through the syscall instruction and nothing else. This post builds that program with NASM 2.16 and GNU ld from binutils 2.43 on x86-64 Linux, runs through each line, then breaks it on purpose so you recognize the failure.

This is the very first program of the series. The later posts on calling C library functions and on the stack build on exactly this skeleton.

The whole program

Create a file named hello.asm:

hello.asmasm
global _start

section .rodata
msg:    db "Hello from x86-64", 10    ; 17 characters plus a newline
len:    equ $ - msg                   ; assembler computes 18

section .text
_start:
        mov     eax, 1                ; syscall number 1 = write
        mov     edi, 1                ; fd 1 = standard output
        lea     rsi, [rel msg]        ; address of the bytes to write
        mov     edx, len              ; how many bytes
        syscall                       ; ask the kernel

        mov     eax, 60               ; syscall number 60 = exit
        xor     edi, edi              ; exit status 0
        syscall

Assemble it into an object file, link it into an executable, and run it:

Shell
nasm -f elf64 hello.asm -o hello.o
ld -o hello hello.o
./hello
echo $?
Text
Hello from x86-64
0

The two commands do different jobs. nasm -f elf64 translates the text into machine code and wraps it in the ELF object format that Linux linkers read. ld places the code at a fixed address and sets the executable's entry point to the symbol named _start, which global _start exported from the object. There is no gcc involved, so nothing runs before _start and nothing runs after the final syscall.

Line by line

global _start makes the label _start visible to the linker. The linker looks for that exact name as the default entry point, so if you spell it start you get the warning ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000 and an odd crash.

section .rodata puts the next lines in the read-only data section, and section .text puts code in the executable section. Linux maps them with different permissions, so the string can be read but not modified, and the code can run but not be written.

msg: db "Hello from x86-64", 10 defines bytes: the characters of the string, then the value 10, which is the newline. db stands for "define byte". len: equ $ - msg is not data. It is a constant the assembler computes, where $ means "the current position", so $ - msg is the number of bytes since msg, which is 18.

_start: is the entry label, and the kernel jumps here with rsp pointing at the argument count, then the argument pointers, the environment pointers and the auxiliary vector. We ignore all of that here. A separate post on the initial process stack layout covers it.

The system call convention

A system call is a request to the kernel to do something a user program cannot do itself, such as write to a terminal or end the process. On x86-64 Linux the request is made with the syscall instruction, and the kernel finds what you want in registers, not on the stack.

The first four instructions fill registers. The Linux x86-64 system call convention, documented in the syscall(2) man page, is: the syscall number goes in rax, the arguments go in rdi, rsi, rdx, r10, r8 and r9 in that order, and the return value comes back in rax. Note r10 where the C function convention uses rcx, because the syscall instruction overwrites rcx with the return address and r11 with the saved flags.

For write(fd, buf, count) the number is 1, so eax gets 1, edi gets the file descriptor 1, rsi gets the buffer address and edx gets the length. Writing to edi and edx as 32-bit registers is enough because 32-bit writes zero the upper half. The buffer address needs the full 64 bits, so lea rsi, [rel msg] computes it relative to the instruction pointer. That rel form works whether the program is linked at a fixed address or not.

For exit(status) the number is 60 and the single argument goes in rdi. xor edi, edi sets the status to zero. If you replace it with mov edi, 7 and run echo $?, the shell prints 7, which is how you check that a failing program reports an error.

Watch the system calls

When the program runs but prints nothing, or prints and then crashes, the quickest way to find out which calls it really made is to ask the kernel.

You can see the program making exactly two calls with strace:

Shell
strace ./hello
Text
execve("./hello", ["./hello"], 0x7ffc8b3a0d10 /* 52 vars */) = 0
write(1, "Hello from x86-64\n", 18Hello from x86-64
)     = 18
exit(0)                                 = ?
+++ exited with 0 +++

The pointer and the variable count in the first line differ on your machine. Everything after execve is our program. The line break inside the write entry is the program's own output arriving in the middle of strace's line, which is a useful reminder that the string really is written once, with 18 bytes.

A C program would show dozens of extra system calls here (brk, mmap, openat for the dynamic loader). The absence of all of them is the point of this program.

The mistake everyone makes: forgetting exit

Delete the last three instructions, so the code ends after the first syscall:

asm
_start:
        mov     eax, 1
        mov     edi, 1
        lea     rsi, [rel msg]
        mov     edx, len
        syscall

The processor has no idea the function ended. It keeps executing whatever bytes come next in memory, which here is the end of the .text section followed by padding. The result:

Text
Hello from x86-64
Segmentation fault (core dumped)

A function in C ends with ret, but _start has no caller, so there is nothing to return to. Every program that does not use the C runtime must end with an explicit exit system call (number 60) or exit_group (number 231), which also ends all threads.

Add debug information and step through it

For a debugger to show your source lines, assemble with DWARF debug information:

Shell
nasm -f elf64 -g -F dwarf hello.asm -o hello.o
ld -o hello hello.o
gdb ./hello

Inside GDB, break _start then run stops on the first instruction, and stepi runs one instruction at a time while info registers rax rdi rsi rdx shows the arguments being loaded. After the second stepi, you will see rdi hold 0x1, which is the first evidence that the program matches the table above.

Where this approach stops

A freestanding program like this is perfect for learning and for tiny tools, and it is the wrong shape for anything that calls a C library. Calling printf from assembly requires the C runtime to initialize first, which is the job of gcc and crt1.o. A later post on calling C library functions from x86-64 assembly shows how, and it changes _start to main. Apple's x86-64 macOS uses different syscall numbers and a different object format, so this exact program does not assemble there, and Windows has no stable raw system call interface at all.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.