Addressing
Addresses, pointers, arrays, structures, LEA, and scaled-index operands.
Memory operands are address calculations. Once you can see a base address, an index, a scale, and a displacement, arrays and structures stop looking like special features.
Element size sets the stride
Adjacent bytes are one address apart, words are two, dwords are four, and qwords are eight. MASM's declarations make that layout visible before any instruction runs.
seqa byte 1, 2, 3
seqb word 10, 20, 30
seqc dword 100, 200, 300
seqd qword 1000, 2000, 3000
Keep in mind: Array address = base + index × element size.
Use a scaled index for traversal
The x64 addressing form [base + index * scale + displacement] performs the common array calculation directly. Here rsi is the base, rcx is the index, and eight is the qword stride.
lea rsi, seqd
xor rcx, rcx
start:
mov rdx, [rsi + rcx * 8]
inc rcx
cmp rcx, lengthof seqd
jne start
Keep in mind: The legal hardware scales are 1, 2, 4, and 8—the common element sizes.
Copy a run of elements
The string instructions also work for numeric arrays. rep movsq copies rcx qwords from rsi to rdi. cld selects forward traversal.
lea rsi, seqg
lea rdi, seqh
mov rcx, lengthof seqg
cld ; clear direction flag
rep movsq
mov r13, seqh[0 * 8]
mov r14, seqh[1 * 8]
mov r15, seqh[2 * 8]
Keep in mind: rsi is the source pointer, rdi is the destination pointer, and rcx is the count.
Keep these rules
- Addresses are arithmetic, not opaque handles.
- Scale an index by the size of one element.
- lea computes an address; mov reads or writes the value at an address.