Defining strings and data
Data definitions
Each directive defines data of a size: db is a byte, dw a word (2 bytes), dd a doubleword (4), dq a quadword (8). Strings are just lists of bytes:
section .data letter db "A" word_val dw 0x1234 number dd 1000000 big dq 1000000000000 name db "NASM", 0
EQU constants
EQU names a constant. It is not a variable: the value is fixed at assembly time and cannot change at run time:
SYS_EXIT equ 60 SYS_WRITE equ 1 BUF_SIZE equ 1024
TIMES padding
TIMES repeats an instruction or a data definition a fixed number of times, which is the usual way to zero a buffer or pad to an alignment:
section .bss buf resb 1024 ; 1024 reserved bytes section .data zeros times 64 db 0 ; 64 zero bytes
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many bytes does
ddreserve for one value?Answer
4
dd is a doubleword, four bytes; db is one, dw two and dq eight.
-
What does
times 64 db 0produce?Answer
64 zero bytes
times repeats the data definition 64 times, so the block is 64 zero bytes.
-
What is the difference between EQU and a memory variable?
Answer
EQU is fixed at assembly time and takes no memory
An EQU symbol is replaced by its value when the file is assembled.
-
Which directive reserves uninitialized space?
Answer
resb
resb reserves bytes without writing them into the file.
-
How many bytes does the string
db "NASM", 0take?Answer
5
Four letters plus the explicit zero byte make five bytes.
-
What does
dq 1000000000000hold?Answer
Eight bytes
dq is a quadword, eight bytes, which is what such a large number needs.
-
What does this program print?
default rel section .data msg db "hi", 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
hi
The bytes are h, i and the newline, and the visible text is hi.
-
What does this program print?
default rel section .data letter db "A" zero db 0 section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, letter mov rdx, 1 syscall mov rax, 60 xor rdi, rdi syscallAnswer
A
letter holds the single byte 65, and one byte of it is written.
-
Which section does
resbusually belong to?Answer
.bss
Reserved, uninitialized space is the point of .bss.
-
What does
db 65write into the file?Answer
One byte with the value 65
db stores one byte, and the number 65 is that byte.
The snippets on this page are fragments, not whole programs: they show the data directives. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev