Define Data in NASM: db, dw, dd, dq, resb and times
Define bytes, words, strings, floats, arrays and zeroed buffers in NASM with db, dw, dd, dq, the res family and times, with exact little-endian layouts.

Everything in an assembly program is bytes, and the data directives are how you decide which bytes go where. NASM has two families: the d directives (db, dw, dd, dq) that emit initialized values, and the res directives (resb, resw, resd, resq) that reserve space with no initial contents. A third tool, times, repeats any instruction or directive. This post shows each with the bytes it produces, on x86-64 with NASM 2.16.03, and ends with the layout mistakes that cause the most bugs. The reference is the NASM manual, chapter 3.
The initialized-data directives
| Directive | Size | Unit name | Example |
|---|---|---|---|
db | 1 byte | byte | db 0x41 |
dw | 2 bytes | word | dw 1000 |
dd | 4 bytes | doubleword | dd 100000 |
dq | 8 bytes | quadword | dq 1 |
dt | 10 bytes | tenbyte | dt 1.5 for an x87 extended value |
do | 16 bytes | oword | do 0 |
Each directive may take several comma-separated values, and each value takes the directive's size. The assembler lays the bytes out in order, little-endian for multi-byte values, and a label before the directive names the first byte.
bits 64
default rel
section .data
b1: db 0x41 ; 41
b2: db 1, 2, 3 ; 01 02 03
w1: dw 0x1234 ; 34 12
d1: dd 0x11223344 ; 44 33 22 11
q1: dq 0x1122334455667788 ; 88 77 66 55 44 33 22 11
neg: dd -1 ; ff ff ff ffLittle-endian means the least significant byte comes first in memory. A debugger shows x/4xb &d1 as 0x44 0x33 0x22 0x11, which surprises anyone who expected the digits in reading order. A negative number is stored in two's complement, so dd -1 is four ff bytes. A value too large for the directive, such as db 300, is truncated with the warning byte data exceeds bounds.
Strings and characters
A string in quotes is a sequence of bytes. NASM does not add a terminator, so a C string needs an explicit zero.
msg: db "hello", 10, 0 ; 68 65 6c 6c 6f 0a 00
chars: db 'A', 'B', 'C' ; same as db "ABC"
wide: dw "AB" ; 41 42, packed as one little-endian word 0x4241
nl: db `line\n` ; backquotes process C escapes: 6c 69 6e 65 0aDouble and single quotes behave the same: no escapes. For a newline you write the number 10 or use backquotes, which accept \n, \t, \0, \x41 and similar. A string longer than the directive's size is split into units, so dw "hello" produces three words and pads the last one with zero. This is a source of bugs for dd "abcd", which packs four characters into one little-endian doubleword that prints in reverse if read as a number.
To get the length of a string, subtract the label from the current position:
msg: db "hello", 10
len equ $ - msg ; 6$ is the address at the start of the line containing it, here the byte after the string. The assembler computes len as a constant, so no space is used for it. Place the equ immediately after the data, or the subtraction includes whatever came between.
Floating-point constants
dd and dq accept floating-point literals and emit IEEE 754 single and double precision:
f32: dd 1.5 ; 00 00 c0 3f
f64: dq 1.5 ; 00 00 00 00 00 00 f8 3f
pi: dq 3.14159265358979 ; double nearest to the literalA literal with a decimal point or an exponent is read as floating point; dd 1 is an integer. For SSE code you often want an array of single-precision values aligned to 16 bytes, which comes in the alignment section below.
Reserving uninitialized space
The res family reserves space in the .bss section, which takes no room in the file because the loader supplies zeroed pages.
| Directive | Reserves |
|---|---|
resb n | n bytes |
resw n | n words, 2n bytes |
resd n | n doublewords, 4n bytes |
resq n | n quadwords, 8n bytes |
section .bss
buffer: resb 4096 ; 4096 bytes
index: resq 1 ; one 64-bit slot
table: resd 256 ; 256 32-bit entries, 1024 bytesNASM warns if you try to place initialized data in .bss: attempt to initialize memory in BSS section: ignored. The converse is also an error: res directives are not allowed in .data in a way that would make the file meaningful, since the section has file contents. Use .bss for buffers and counters that start at zero, and .data for tables with values, so the executable stays small.
Repeating with times
times n repeats the following instruction or directive n times, and n can be an expression:
zeros: times 16 db 0 ; 16 zero bytes in .data
ramp: times 4 dd 7 ; 07 00 00 00 repeated four times
pad: times 510 - ($ - $$) db 0 ; pad the section to 510 bytes
sig: dw 0xaa55 ; boot sector signatureThe last two lines are the classic boot-sector idiom: $$ is the start of the current section, $ - $$ is how many bytes have been emitted so far, and the times line pads the remainder with zeros so the two-byte signature lands at offsets 510 and 511. It only works in a flat binary built with nasm -f bin, where the section starts at offset zero.
You can repeat a whole instruction too: times 8 nop emits eight 0x90 bytes. And you can nest the counting expression to produce sizes the assembler computes, as long as the expression is a constant known on the first pass, so it cannot depend on a label that appears later in the file.
times has no side effects beyond repetition, but it can produce a lot of output by accident. times 100000 dd 0 emits 400 KB in .data, where resd 100000 emits none. Choose by whether the contents matter.
Alignment
Data placed after a db of odd length ends up at an odd address. Many instructions work with unaligned data on x86-64, but SSE aligned loads such as movaps require 16-byte alignment, and misaligned accesses can cross cache lines. NASM's align pads the current section with nops in code and zeros in data to the next multiple of the argument:
section .data
flag: db 1
align 16
vec: dd 1.0, 2.0, 3.0, 4.0 ; 16-byte alignedalignb is the .bss counterpart that reserves instead of filling. The section itself must be aligned at least as strictly as the largest align in it, and NASM raises the section alignment automatically, so align 16 in .data makes the section 16-byte aligned in the object. You can set it explicitly with section .data align=16.
Structures and arrays by hand
NASM has the struc macro for named offsets, but plain constants are often clearer:
; struct point { long x; long y; } -- 16 bytes
POINT_X equ 0
POINT_Y equ 8
POINT_SIZE equ 16
section .data
points:
dq 1, 2 ; point 0: x=1, y=2
dq 3, 4 ; point 1
dq 5, 6 ; point 2
POINT_COUNT equ ($ - points) / POINT_SIZE ; 3
section .text
sum_x:
lea rsi, [points]
xor eax, eax
mov ecx, POINT_COUNT
.next:
add rax, [rsi + POINT_X]
add rsi, POINT_SIZE
dec ecx
jnz .next
retThe function returns 9 in RAX, the sum of the three x values 1, 3 and 5. Using equ constants for field offsets and the count keeps the numbers in one place and lets the assembler compute POINT_COUNT from the table's size, so adding a row to the table updates the count with no other edit. Note that POINT_COUNT must follow the table, because $ has to be the address of its end.
Looking at the bytes
Confirm what you wrote with objdump -s -j .data on the object:
nasm -f elf64 data.asm -o data.o
objdump -s -j .data data.oContents of section .data:
0000 41010203 34124433 22118877 66554433 A...4.D3"..wfUD3
0010 2211ffff ffff ".....The offsets, the bytes and an ASCII rendering are all visible, and each value from the first example sits where its label says. The bytes 88 77 66 55 44 33 22 11 for the quadword confirm the little-endian layout. This check takes seconds and catches stray padding and endianness assumptions before they reach a loop.
Mistakes that bite
The first is forgetting the string terminator. A C function reading db "hello" runs off the end into the next data. The second is confusing dd 1.0 and dd 1: the first is the single-precision bit pattern 0x3f800000, the second the integer. The third is measuring a length with $ - label after other data was emitted in between. The fourth is putting values in .bss, where they are discarded. The fifth is assuming that dw 'AB' stores 0x4142; it stores 0x4241, because the first character goes in the low byte.


