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.

Next: Structures

Article author: Arthur Isaev

Banner image