Arrays
Array layout
An array is contiguous memory: element i sits at base + i times element size. NASM addressing mode does the multiply for you with the scale factor 1, 2, 4, or 8:
section .data table dd 10, 20, 30, 40 ; four 32-bit ints section .text mov esi, 2 mov eax, [table + esi*4] ; eax = 30
Indexing
The index register can be any general-purpose register, and the scale matches the element size: 1 for bytes, 2 for words, 4 for dwords, 8 for qwords. Compute the index in a register first when it is not a loop counter:
mov esi, 3 mov al, [letters + esi] ; letters[3] mov rax, [bigs + esi*8] ; bigs[3] of qwords
Bounds
There is no bounds check: indexing past the end reads or writes whatever follows in memory. Compare the index against the length yourself, and decide what the program does when it is out of range.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does element i of an array sit?
Answer
At base + i times the element size
Elements are contiguous, so the address moves by one element size for each index step.
-
Which scale indexes an array of 32-bit integers?
Answer
4
A dword is four bytes, so the scale is 4.
-
Which scale indexes an array of qwords?
Answer
8
A qword is eight bytes, and 8 is one of the allowed scales.
-
What does
[table + rsi*4]read?Answer
The dword at table + rsi*4
The brackets make it a memory read at base plus index times scale.
-
What happens when an index runs past the end of an array?
Answer
The program reads or writes the next memory
x86 has no bounds check; the address simply points at whatever follows.
-
What does the scale factor do in an address?
Answer
It multiplies the index by 1, 2, 4 or 8
The scale matches the element size, so the index counts elements rather than bytes.
-
What does this program print?
default rel section .data letters db "abcd", 10 section .text global _start _start: mov esi, 3 mov al, [letters + rsi] mov [letters], al mov rax, 1 mov rdi, 1 mov rsi, letters mov rdx, 1 syscall mov rax, 60 xor rdi, rdi syscallAnswer
d
letters[3] is d, and the program copies it to the front and writes one byte.
-
What does this program print?
default rel section .data table dd 10, 20, 30, 40 digit db "0", 10 section .text global _start _start: mov esi, 2 mov eax, [table + rsi*4] xor rdx, rdx mov ecx, 10 div ecx add al, "0" mov [digit], al mov rax, 1 mov rdi, 1 mov rsi, digit mov rdx, 2 syscall mov rax, 60 xor rdi, rdi syscallAnswer
3
table[2] is 30, and 30 divided by 10 gives the digit 3.
-
What is the exit status of the first array program on this page?
Answer
0
rdi is cleared before sys_exit, so the status is 0.
-
Which register can serve as an index?
Answer
Any general-purpose register
The index field takes any general-purpose register except RSP.
The snippets on this page are fragments, not whole programs: they show the array layout and the addressing modes. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev