Your first program
The source file
Create hello.asm with the two system calls you saw in lesson 1: sys_write (1) prints bytes, and sys_exit (60) ends the process:
section .data msg db "Hello from assembly", 10 len equ $ - msg section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, len syscall mov rax, 60 xor rdi, rdi syscall
Assemble and link
Assemble to an ELF object, then link it into an executable. The -f elf64 flag selects the 64-bit ELF output format:
nasm -f elf64 -o hello.o hello.asm ld -o hello hello.o
Run it
Run the executable and check the exit status:
./hello echo $?
Hello from assembly 0
Anatomy
global _start makes the entry point visible to the linker. Each mov loads one argument of the system call into a register: rax holds the call number, rdi the first argument, rsi the second, rdx the third. The final syscall never returns, because sys_exit ends the process.
The code on this page was assembled with NASM 3.01 and linked with GNU ld 2.45.1, and the program was run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev