Introduction#
Note
This page will probably be moved to the guide section when it is done.
About NASM & x86-64#
NASM (Netwide Assembler) is a popular, open-source assembler for the x86 and x86-64 architectures, supporting multiple output formats and platforms.
Key Features:
Intel-style syntax (destination first:
mov dest, src)- Multi-Platform Support:
Runs on Windows, Linux, macOS, and other Unix-like systems
Supports various output formats: ELF, COFF, Mach-O, Win32/64, raw binary
Powerful macro system with conditional assembly support
x86-64 Specifics:
64-bit registers (RAX, RBX, RCX, RDX, RSI, RDI, RBP, RSP, R8-R15)
64-bit memory addressing
SSE/AVX extensions available
Example: Hello World in NASM#
1section .data
2 message db 'Hello, World!', 0xA
3 length equ $ - message
4
5section .text
6 global _start ; Entry point for Linux
7
8_start:
9 ; Write to stdout
10 mov rax, 1 ; sys_write
11 mov rdi, 1 ; stdout
12 mov rsi, message
13 mov rdx, length
14 syscall
15
16 ; Exit
17 mov rax, 60 ; sys_exit
18 xor rdi, rdi
19 syscall
Data Directives#
db ; Define byte (8-bit)
dw ; Define word (16-bit)
dd ; Define doubleword (32-bit)
dq ; Define quadword (64-bit)
resb ; Reserve bytes
equ ; Define constant
Sections in Assembler#
section .data ; Initialized global/static data
section .bss ; Uninitialized data (Zeroed)
section .text ; Code
section .rodata ; Read-only data
1 .text Section (Code)#
Contains executable instructions
Read-only and executable
section .text ; Start of code section
global _start ; Make _start visible to linker
_start: ; Entry point
mov rax, 1 ; Instructions go here
ret
2 .data Section (Initialized Data)#
Read-write memory
Variables with initial values
Loaded from executable file
section .data
number dd 42 ; Initialized to 42
msg db 'Hello', 0 ; String with null terminator
array times 10 db 0 ; Array of 10 zeros
3 .bss Section (Uninitialized Data)#
Read-write memory
Variables without initial values
Not stored in executable (saves space)
Automatically zeroed by OS
section .bss
buffer resb 1024 ; Reserve 1024 bytes (uninitialized)
count resd 1 ; Reserve 1 dword
4 .rodata Section (Read-Only Data)#
Read-only memory
Constants, strings, lookup tables
Cannot be modified (segfault if you try)
section .rodata
pi dq 3.1415926535
const_string db "Constant!", 0
Multiple Sections#
Yes! You can have multiple sections of the same type:
; Multiple .data sections
section .data
var1 dd 10
section .text
mov rax, [var1]
section .data ; Another .data section
var2 dd 20 ; This continues where previous .data left off
section .text ; Another .text section
add rax, [var2]
Building with NASM#
Basic compilation and linking for 64-bit systems:
Linux#
# Basic assembly and linking
nasm -f elf64 source.s -o source.o # Compile source.s into source.o
ld source.o -o output # Link source.o and create binary
# With gcc (recommended for C interoperability)
nasm -f elf64 program.asm -o program.o
gcc program.o -o program
Windows#
# 64-bit Windows
nasm -f win64 program.asm -o program.obj
golink /entry:_start program.obj kernel32.dll
macOS#
# 64-bit macOS
nasm -f macho64 program.asm -o program.o
ld program.o -o program -macosx_version_min 10.15 -lSystem