Arrays & Indexing
An array is a sequence of equally sized elements stored next to each other in memory. Element i of an array that starts at address base, with elements of size bytes, lives at:
address(i) = base + i * sizeThat formula maps directly onto x86's addressing mode [base + index*scale], with scale = 1, 2, 4 or 8 — exactly the sizes of db, dw, dd and dq elements.
Declaring and indexing
section .data
temps: dd 12, 15, 9, 21, 18 ; five 4-byte integers
TEMP_COUNT equ ($ - temps) / 4
section .text
global _start
_start:
mov edi, TEMP_COUNT
call print_uint ; number of elements
mov edi, [temps] ; element 0
call print_uint
mov ecx, 3
mov edi, [temps + rcx*4] ; element 3: scale 4 for dd
call print_uint
mov edi, [temps + (TEMP_COUNT - 1) * 4] ; last element, computed by NASM
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
ret5
12
21
18
Note: The index register must be 64 bits wide (
rcx, notecx) inside the brackets. Writingecxearlier is fine: it zero-extends intorcx.
Printing a whole array
From now on, examples print arrays on one line with a helper, print_array. It takes the address of an array of unsigned quadwords in rdi and the element count in rsi. Inside, it is the digit loop of print_uint wrapped in a loop over the elements, with one twist: each number ends with a space, except the last, which ends with a newline. Because it calls syscall inside its loop, it keeps its pointer and counter in the callee-saved rbx and r12 (and saves them first).
section .data
primes: dq 2, 3, 5, 7, 11, 13, 17, 19
PRIME_COUNT equ ($ - primes) / 8
section .text
global _start
_start:
mov rdi, primes
mov esi, PRIME_COUNT
call print_array
mov rdi, primes + 2 * 8 ; a "slice": start at element 2 ...
mov esi, 3 ; ... and print three elements
call print_array
mov eax, 60
xor edi, edi
syscall
; ---- print_array: print rsi unsigned quadwords starting at rdi on one line ----
print_array:
push rbx
push r12
mov rbx, rdi ; rbx = address of the next element
mov r12, rsi ; r12 = elements left
sub rsp, 32 ; digit buffer, as in print_uint
.element:
test r12, r12
jz .finish
mov rax, [rbx] ; the number to convert
lea rsi, [rsp + 31]
mov byte [rsi], ' ' ; a space after each number ...
cmp r12, 1
jne .convert
mov byte [rsi], 10 ; ... except the last one, which ends the line
.convert:
mov ecx, 10
.digit:
xor edx, edx
div rcx
add dl, '0'
dec rsi
mov [rsi], dl
test rax, rax
jnz .digit
lea rdx, [rsp + 32]
sub rdx, rsi ; length of digits + separator
mov eax, 1
mov edi, 1
syscall
add rbx, 8 ; next quadword
dec r12
jmp .element
.finish:
add rsp, 32
pop r12
pop rbx
ret2 3 5 7 11 13 17 19
5 7 11
Sum, minimum and maximum
The basic array loop: an index from 0 to count − 1, one element per iteration. Here three results are computed in one pass:
section .data
sales: dq 420, 380, 515, 290, 610, 475, 330
SALE_COUNT equ ($ - sales) / 8
section .text
global _start
_start:
xor ebx, ebx ; sum
mov r12, [sales] ; min = first element
mov r13, r12 ; max = first element
xor ecx, ecx ; i = 0
.loop:
mov rax, [sales + rcx*8]
add rbx, rax
cmp rax, r12
jae .not_min
mov r12, rax ; new minimum
.not_min:
cmp rax, r13
jbe .not_max
mov r13, rax ; new maximum
.not_max:
inc ecx
cmp ecx, SALE_COUNT
jb .loop
mov rdi, rbx
call print_uint ; total
mov rdi, r12
call print_uint ; smallest
mov rdi, r13
call print_uint ; largest
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
ret3020
290
610
Pointer-style loops
Instead of an index you can move a pointer through the array and stop at an end address. This is what compilers often generate, because the address computation disappears from the loop:
section .data
values: dq 1, 2, 3, 4, 5, 6
values_end: ; the address just past the last element
section .text
global _start
_start:
mov rsi, values ; p = &values[0]
.double:
shl qword [rsi], 1 ; *p *= 2
add rsi, 8 ; p++
cmp rsi, values_end
jb .double
mov rdi, values
mov esi, (values_end - values) / 8
call print_array
mov eax, 60
xor edi, edi
syscall
; ---- print_array: print rsi unsigned quadwords starting at rdi on one line ----
print_array:
push rbx
push r12
mov rbx, rdi ; rbx = address of the next element
mov r12, rsi ; r12 = elements left
sub rsp, 32 ; digit buffer, as in print_uint
.element:
test r12, r12
jz .finish
mov rax, [rbx] ; the number to convert
lea rsi, [rsp + 31]
mov byte [rsi], ' ' ; a space after each number ...
cmp r12, 1
jne .convert
mov byte [rsi], 10 ; ... except the last one, which ends the line
.convert:
mov ecx, 10
.digit:
xor edx, edx
div rcx
add dl, '0'
dec rsi
mov [rsi], dl
test rax, rax
jnz .digit
lea rdx, [rsp + 32]
sub rdx, rsi ; length of digits + separator
mov eax, 1
mov edi, 1
syscall
add rbx, 8 ; next quadword
dec r12
jmp .element
.finish:
add rsp, 32
pop r12
pop rbx
ret2 4 6 8 10 12
Reversing in place
Two indices walk towards each other and swap elements until they meet:
section .data
digits: dq 1, 2, 3, 4, 5, 6, 7
COUNT equ ($ - digits) / 8
section .text
global _start
_start:
xor ecx, ecx ; left = 0
mov edx, COUNT - 1 ; right = count - 1
.swap:
cmp rcx, rdx
jae .done ; stop when left >= right
mov rax, [digits + rcx*8]
mov r8, [digits + rdx*8]
mov [digits + rcx*8], r8
mov [digits + rdx*8], rax
inc rcx
dec rdx
jmp .swap
.done:
mov rdi, digits
mov esi, COUNT
call print_array
mov eax, 60
xor edi, edi
syscall
; ---- print_array: print rsi unsigned quadwords starting at rdi on one line ----
print_array:
push rbx
push r12
mov rbx, rdi ; rbx = address of the next element
mov r12, rsi ; r12 = elements left
sub rsp, 32 ; digit buffer, as in print_uint
.element:
test r12, r12
jz .finish
mov rax, [rbx] ; the number to convert
lea rsi, [rsp + 31]
mov byte [rsi], ' ' ; a space after each number ...
cmp r12, 1
jne .convert
mov byte [rsi], 10 ; ... except the last one, which ends the line
.convert:
mov ecx, 10
.digit:
xor edx, edx
div rcx
add dl, '0'
dec rsi
mov [rsi], dl
test rax, rax
jnz .digit
lea rdx, [rsp + 32]
sub rdx, rsi ; length of digits + separator
mov eax, 1
mov edi, 1
syscall
add rbx, 8 ; next quadword
dec r12
jmp .element
.finish:
add rsp, 32
pop r12
pop rbx
ret7 6 5 4 3 2 1
Two-dimensional arrays
A table with ROWS × COLS elements is stored row by row (row-major order, like C). Element [r][c] is at index r * COLS + c:
COLS equ 4
section .data
grid: dq 1, 2, 3, 4
dq 5, 6, 7, 8
dq 9, 10, 11, 12
section .text
global _start
_start:
; grid[2][1]
mov eax, 2 ; row
imul eax, eax, COLS
add eax, 1 ; + column
mov rdi, [grid + rax*8]
call print_uint ; 10
; print row 1: it starts at index 1 * COLS
mov rdi, grid + 1 * COLS * 8
mov esi, COLS
call print_array
mov eax, 60
xor edi, edi
syscall
; ---- print_array: print rsi unsigned quadwords starting at rdi on one line ----
print_array:
push rbx
push r12
mov rbx, rdi ; rbx = address of the next element
mov r12, rsi ; r12 = elements left
sub rsp, 32 ; digit buffer, as in print_uint
.element:
test r12, r12
jz .finish
mov rax, [rbx] ; the number to convert
lea rsi, [rsp + 31]
mov byte [rsi], ' ' ; a space after each number ...
cmp r12, 1
jne .convert
mov byte [rsi], 10 ; ... except the last one, which ends the line
.convert:
mov ecx, 10
.digit:
xor edx, edx
div rcx
add dl, '0'
dec rsi
mov [rsi], dl
test rax, rax
jnz .digit
lea rdx, [rsp + 32]
sub rdx, rsi ; length of digits + separator
mov eax, 1
mov edi, 1
syscall
add rbx, 8 ; next quadword
dec r12
jmp .element
.finish:
add rsp, 32
pop r12
pop rbx
ret
; ---- 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
ret10
5 6 7 8
Bounds are your job
Nothing checks indices. [sales + rcx*8] with rcx = 100 simply reads whatever lies 800 bytes after sales: other variables, or unmapped memory and a crash. Always compare the index with the count (unsigned, with jb/jae, so that negative indices are rejected too).
Test yourself
An array of dd elements starts at nums. Which operand reads element rcx?
[nums + rcx][nums + rcx*2][nums + rcx*4][nums + rcx*8]
In a 3 × 5 row-major table of quadwords, what is the byte offset of element [2][3]?
- 2*3*8 = 48
- (2*5 + 3)*8 = 104
- (3*3 + 2)*8 = 88
- (2 + 3)*8 = 40
Why should a bounds check use jae rather than jge?
- It's faster
- An unsigned comparison also rejects negative indices, which look like huge unsigned numbers
- jge doesn't exist
- There's no difference