Working with Arrays
Arrays are the most common data structure in computing. In assembly they're just contiguous bytes in memory — no array type, no .length property, no bounds checking. You get a starting address and a convention about element size. Everything else is on you.
Defining Arrays in the Data Section
db, dw, dd, and dq define arrays of bytes, words, dwords, and qwords respectively.
section .data bytes db 0x41, 0x42, 0x43, 0x44 ; 4 bytes: 'A', 'B', 'C', 'D' words dw 1000, 2000, 3000 ; 3 words (16-bit each) dwords dd 100000, 200000, 300000, 400000 ; 4 dwords (32-bit each) qwords dq 10, 20, 30, 40, 50 ; 5 qwords (64-bit each) qw_len equ 5 ; track the length yourself
section .bss buffer resb 256 ; reserve 256 uninitialized bytes results resd 10 ; reserve 10 uninitialized dwordsNote that you'll have to track array lengths yourself with equ constants or by convention. The assembler lays values out contiguously and nothing flags where one array ends and the next starts.
Accessing Elements with Base + Index * Scale
Base + index*scale was practically built for array access.
section .text global _start
_start: ; Load the 3rd element (index 2) of the qwords array lea rbx, [qwords] ; rbx = base address mov rcx, 2 ; index = 2 mov rax, [rbx + rcx*8] ; scale = 8 for qwords -> rax = 30Scale matches element size: 1 for bytes, 2 for words, 4 for dwords, 8 for qwords. Mismatch the scale and you get a quiet bug that reads from the wrong addresses.
Iterating Over an Array
A typical iteration uses a counter register as the index:
; Sum all elements in the qwords array lea rbx, [qwords] ; base address xor rax, rax ; accumulator = 0 xor rcx, rcx ; index = 0
.sum_loop: cmp rcx, qw_len ; have we processed all elements? jge .sum_done ; if index >= length, exit loop add rax, [rbx + rcx*8] ; accumulator += array[index] inc rcx ; index++ jmp .sum_loop
.sum_done: ; rax now contains 10+20+30+40+50 = 150Or you can walk a pointer through the array directly:
; Pointer-based iteration lea rsi, [qwords] ; rsi points to first element lea rdi, [qwords + qw_len*8] ; rdi points past last element xor rax, rax ; accumulator = 0
.ptr_loop: cmp rsi, rdi ; have we reached the end? jae .ptr_done ; addresses are unsigned: use jae, not jge add rax, [rsi] ; add current element add rsi, 8 ; advance pointer by element size jmp .ptr_loop
.ptr_done: ; rax = 150Same result either way, and on modern x86 at much the same speed: the *8 in [rbx + rcx*8] is handled by the address-generation hardware, so there's no separate multiply to save. Pick whichever reads better. Compilers emit both forms.
Storing Results Back to Arrays
Writing back uses the same addressing — the memory operand just moves to the destination side:
; Double every element in place lea rbx, [qwords] xor rcx, rcx
.double_loop: cmp rcx, qw_len jge .double_done mov rax, [rbx + rcx*8] ; load element shl rax, 1 ; multiply by 2 (shift left) mov [rbx + rcx*8], rax ; store back inc rcx jmp .double_loop
.double_done:Multi-Dimensional Arrays
C stores 2D arrays in row-major order: row 0 first, then row 1, and so on. The formula for matrix[row][col] is base + (row * num_cols + col) * element_size.
section .data ; 3x4 matrix of dwords matrix dd 1, 2, 3, 4 dd 5, 6, 7, 8 dd 9, 10, 11, 12 rows equ 3 cols equ 4
section .text ; Access matrix[2][1] -> should be 10 mov rax, 2 ; row = 2 imul rax, cols ; row * num_cols = 8 add rax, 1 ; + col = 9 lea rbx, [matrix] mov eax, [rbx + rax*4] ; scale=4 for dwords -> eax = 10In C, indexing past the end of an array is undefined behaviour: the language doesn't check, and the compiler is free to assume it never happens (sanitisers such as -fsanitize=address add checks only when you ask). In assembly, no such concept exists at the instruction level. The CPU computes whatever address you ask for and runs with it. If the page is mapped with the right permissions, the access just goes through — even if you've stumbled onto another variable, the stack, or heap metadata. That gap is the root of every buffer overflow vulnerability.
Here's the .data section from the top of the lesson, starting at dwords. NASM packs it with no gaps, so qwords begins the byte after dwords[3] ends. The start address is illustrative. Step rcx from 0 to 7 through mov eax, [rbx + rcx*4] and watch what happens once it passes 3.
mov eax, [rbx + rcx*4] past the end of dwords
Starts at 0x40200a; small numbers under each byte are the last two hex digits of its address.
rcx = 0 reads dwords[0]: a0 86 01 00 is 0x000186a0, 100000.
Uninitialized Arrays in BSS
The .bss section reserves memory without inflating the executable on disk. resb, resw, resd, resq reserve bytes, words, dwords, qwords.
section .bss input_buf resb 1024 ; 1024-byte input buffer int_array resd 100 ; room for 100 dwords ptr_table resq 64 ; room for 64 pointersThe OS zeroes these sections when the program loads. They typically hold input buffers, which makes their size a security-critical decision — a buffer too small for the data being read into it is a buffer overflow waiting to happen.
Security Perspective: Array Bounds as the Root of Buffer Overflows
Take a function that reads user input into a stack-allocated buffer:
; Dangerous: no length check on input sub rsp, 64 ; allocate 64-byte buffer on stack mov rax, 0 ; sys_read mov rdi, 0 ; stdin lea rsi, [rsp] ; buffer on stack mov rdx, 256 ; BUG: reading up to 256 bytes into 64-byte buffer! syscallsys_read will cheerfully shovel up to 256 bytes into a 64-byte buffer. The leftover bytes spill over whatever sits above the buffer on the stack — saved registers, the return address, and beyond. Classic stack buffer overflow.
The fix is dull and obvious: the read length must not exceed the buffer size.
sub rsp, 64 mov rax, 0 mov rdi, 0 lea rsi, [rsp] mov rdx, 64 ; FIXED: read length matches buffer size syscallThis pattern — programmer manually keeps every array access in-bounds — is the foundational challenge of secure low-level code. For the full exploitation arc, see Anatomy of a Stack Frame.
What is Next
Strings are just arrays of characters, and x86 ships dedicated instructions to crunch them efficiently. Next up: the REP prefix and the string-instruction family.