LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 8 Part 1 of 2
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

Question

How do you recognize array access in assembly?

Answer

Look for scaled-index addressing base(,%index,scale), where the scale (1/2/4/8) equals the element size in bytes.

Scaled-index addressing (%rdi,%rsi,4): address = base + index*scale, where scale (1/2/4/8) equals the element size, and a single memory read yields Ai.

* Scaled-index addressing base(,index,scale): the scale (1/2/4/8) equals the element size in bytes. One memory access = contiguous array element MEM[&A[0] + i*scale]. *

# eax = A[i] (int array, 4 bytes per element)
movl (%rdi,%rsi,4), %eax

# rax = A[i] (long/pointer array, 8 bytes per element)
movq (%rdi,%rsi,8), %rax

Common scales and what they mean:

Scale Element size Type
1 1 byte char, byte
2 2 bytes short
4 4 bytes int, float
8 8 bytes long, double, pointer

Multidimensional array A[i][j] with M columns:

# index = i*M + j, then scale by element size
imul $M, %rdi, %rax
add  %rsi, %rax
movl (%rcx,%rax,4), %eax

vs pointer array (array of pointers to rows):

# First dereference: get row pointer
mov  (%rdi,%rsi,8), %rax
# Second dereference: get element
mov  (%rax,%rdx,4), %eax

Two memory accesses = multilevel (pointer array). One = contiguous 2D array.

Go deeper:

A two-dimensional array stored as a one-dimensional array of one-dimensional arrays (rows).
A two-dimensional array stored as a one-dimensional array of one-dimensional arrays (rows).
No machine-readable author provided. Dcoetzee assumed (based on copyright claims). · Public domain · Wikimedia Commons
or press any other key

Question

How do you recognize struct field access in assembly?

Answer

Look for fixed constant offsets off one base pointer (0(%rax), 4(%rax), 12(%rax)) — each offset is a field's position within the struct.

Fixed constant offsets off one base pointer: int a@0, int b@4, long c@8 (movq), int i@16, with alignment padding; mixed sizes off one base signal a struct, not an array.

* Fixed constant offsets off one base pointer (0, 4, 8, 16), often with mixed access sizes (movl for int, movq for long) and alignment padding — the signature of struct field access versus a scaled array index. *

# Access struct field at offset 12
movl 12(%rax), %edx

# Access struct field at offset 0 (first field)
movl (%rax), %edx

Example struct:

struct rec {
    int a;       // offset 0
    int b;       // offset 4
    long c;      // offset 8 (aligned)
    int i;       // offset 16
};

How to identify structs vs arrays:

  • Struct: Different constant offsets, potentially different instruction sizes (movl for int at offset 0, movq for long at offset 8)
  • Array: Scaled index with uniform element size

Tip: If you see multiple accesses to the same base register with different fixed offsets (0(%rax), 4(%rax), 12(%rax)), it's a struct. Map the offsets to figure out field sizes and padding.

Go deeper:

Journal sheet from 1880 United States census, showing tabular data with rows of data, each a record corresponding to a single person.
Journal sheet from 1880 United States census, showing tabular data with rows of data, each a record corresponding to a single person.
1880 US Census · Public domain · Wikimedia Commons
or press any other key