NASM syntax basics
Sections
A NASM program is divided into sections. .text holds code, .data holds initialized data, and .bss holds uninitialized reserves. The section directive switches between them:
section .text mov rax, 1 section .data count dq 0
Labels
A label names a position in the code or a byte of data. Labels end with a colon, and the assembler turns them into addresses:
start: mov rax, 1 jmp start
Operands
Most instructions take two operands: destination first, source second. The destination is where the result lands, and the source is what gets moved or added. Comments start with a semicolon:
mov rax, rbx ; copy rbx into rax add rax, 1 ; rax = rax + 1
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which section of a NASM source file holds initialized data?
Answer
.data
.data holds data with an initial value, such as a string or a number.
-
Which section holds uninitialized reserves?
Answer
.bss
.bss reserves space without storing bytes in the object file.
-
In
mov rax, rbx, which operand is the destination?Answer
rax
NASM writes the destination first and the source second, so the value of rbx lands in rax.
-
What does a semicolon start in a NASM source file?
Answer
A comment
Everything from the semicolon to the end of the line is a comment.
-
What does
global _startdo?Answer
It makes the label visible to the linker
The linker cannot see a local label, so the entry point is exported with global.
-
What does this program print?
section .data msg db "OK", 10 section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, 3 syscall mov rax, 60 xor rdi, rdi syscallAnswer
OK
The data label msg points at O, K and a newline, and write prints all three bytes.
-
What does this program print?
section .data msg db "B", 10 section .text global _start _start: jmp forward mov rsi, msg mov rdx, 2 jmp done forward: mov rsi, msg mov rdx, 2 done: mov rax, 1 mov rdi, 1 syscall mov rax, 60 xor rdi, rdi syscallAnswer
B
The first jump skips the two mov instructions, so the write runs once.
-
What does this program print?
section .data msg db "NASM", 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 syscallAnswer
NASM
$is the current address, so len becomes 5: four letters and the newline. -
What does a label name in NASM?
Answer
A position in the code or in the data
The assembler turns the label into the address of the instruction or the byte it marks.
-
Which section holds the instructions of a program?
Answer
.text
The code lives in .text; the data sections hold bytes, not instructions.
The snippets on this page are fragments, not whole programs: they show the syntax. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev