TExcelTAD
Home › x86-64 Assembly › Sections & Data Directives

Sections & Data Directives

A program is more than instructions: it needs constants, strings, tables and space for variables. NASM's data directives place bytes into the output file, and sections decide where in memory those bytes end up and what you may do with them.

The three common sections

Section Contents Permissions Takes space in the file?
.text instructions read + execute yes
.data initialized variables read + write yes
.rodata constants (strings, tables) read only yes
.bss uninitialized variables, zero-filled at start read + write no, only its size is recorded

You can switch sections as often as you like; NASM collects everything for each section together.

Defining data: db, dw, dd, dq

Directive Unit Example
db byte (1) db 65, 'B', "text", 0
dw word (2) dw 1000, -1
dd doubleword (4) dd 100000, dd 1.5 (a 32-bit float)
dq quadword (8) dq 1 << 40, dq 3.14159 (a 64-bit double), dq some_label

Each directive accepts a comma-separated list; a string in db produces one byte per character. The label in front names the address of the first byte.

nasm · try it yourself
section .data
small:      db 200
medium:     dw 50000
large:      dd 3000000000
huge:       dq 9000000000000000000
mixed:      db 'A', "BC", 10       ; 4 bytes: 65 66 67 10

section .text
global _start
_start:
    movzx edi, byte [small]
    call print_uint
    movzx edi, word [medium]
    call print_uint
    mov edi, [large]            ; a 32-bit load zero-extends into rdi
    call print_uint
    mov rdi, [huge]
    call print_uint
    movzx edi, byte [mixed + 2] ; third byte: 'C'
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
200
50000
3000000000
9000000000000000000
67

Note: The label has no size. [small] is just an address, which is why each load needs byte/word or a register of the right size. Reading qword [small] would happily read 8 bytes, most of which belong to the next variables.

Constants with equ

name equ value defines an assembly-time constant. It takes no memory: wherever you use the name, NASM substitutes the number.

nasm · try it yourself
SECONDS_PER_MINUTE  equ 60
MINUTES_PER_HOUR    equ 60
SECONDS_PER_DAY     equ 24 * MINUTES_PER_HOUR * SECONDS_PER_MINUTE

section .rodata
banner:     db "Seconds per day:", 10
banner_len  equ $ - banner

section .text
global _start
_start:
    mov eax, 1
    mov edi, 1
    mov rsi, banner
    mov edx, banner_len
    syscall

    mov edi, SECONDS_PER_DAY
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
Seconds per day:
86400

$ is the address of the current position and $ the start of the current section. $ - banner is therefore the number of bytes since banner — the classic way to get a string's length. It must come directly after the data it measures.

Repeating with times

times N repeats an instruction or directive N times — handy for tables and padding:

nasm · try it yourself
section .data
dashes:     times 20 db '-'     ; 20 dash characters
            db 10               ; and a newline
dash_len    equ $ - dashes
squares:    dq 0, 1, 4, 9, 16
count       equ ($ - squares) / 8   ; number of quadwords

section .text
global _start
_start:
    mov eax, 1
    mov edi, 1
    mov rsi, dashes
    mov edx, dash_len
    syscall

    mov edi, count
    call print_uint
    mov rdi, [squares + 4 * 8]  ; element 4
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
--------------------
5
16

Reserving space: resb, resw, resd, resq

In .bss you don't give values, only sizes. resb 64 reserves 64 bytes, resq 10 reserves ten quadwords. The kernel fills the whole section with zeros when the program starts, and it costs nothing in the executable file.

nasm · try it yourself
section .bss
buffer:     resb 16             ; 16 zero bytes
total:      resq 1              ; one quadword, starts as 0

section .text
global _start
_start:
    mov rdi, [total]            ; zero at startup
    call print_uint

    mov byte [buffer], 'O'      ; fill the buffer at run time
    mov byte [buffer + 1], 'K'
    mov byte [buffer + 2], 10
    mov eax, 1
    mov edi, 1
    mov rsi, buffer
    mov edx, 3
    syscall

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
0
OK

Read-only data is really read-only

Strings and tables that never change belong in .rodata. The linker maps that section without write permission, so a bug that tries to modify a constant crashes immediately instead of corrupting data silently:

nasm · try it yourself
section .rodata
pi_digits:  db "31415"

section .text
global _start
_start:
    mov byte [pi_digits], '4'   ; writing to read-only memory
    mov eax, 60
    xor edi, edi
    syscall
Program terminated with signal SIGSEGV (11)

Test yourself

Which directive reserves uninitialized space in .bss?

  1. db 0
  2. resb 100
  3. times 100
  4. equ 100

How many bytes does nums: dd 1, 2, 3 occupy?

  1. 3
  2. 6
  3. 12
  4. 24

What does count equ 5 occupy in memory?

  1. 1 byte
  2. 8 bytes
  3. Nothing: it's an assembly-time constant
  4. It depends on the section