ASSEMBLY / CHAPTER 04
Record layout, stride, and byte order
MasterCalculate field addresses from an explicitly stated layout.
By the end of this chapter
- Separate record stride from field offset
- Decode little-endian multi-byte values
- Avoid assuming a native language layout
The idea
An address calculation needs a layout contract: the size of one record, the byte offset of the required field, and its width. The hardware does not know field names or compiler padding. A record array with stride sixteen and a dword at offset eight requires base plus index times sixteen plus eight, regardless of how the record was described in source code.
x86 stores multi-byte integers in little-endian order, placing the least significant byte at the lowest address. A load interprets adjacent bytes according to its width. These examples state a custom layout rather than assuming a particular C struct packing rule. Check the record count before forming an address used for access, and keep valid extent and field width visible when reviewing every load.
; RDI points to 3 readable records, each exactly 16 bytes.
; Dword count fields at offsets 8, 24, 40 contain 5, 7, 11.
; Other fields are irrelevant to this trace.
mov ecx, 2
imul rcx, 16
mov eax, [rdi+rcx+8]RAX = 11.
Record index two begins at byte offset 32; its count field is at offset 40.
Try it yourself
Find a dword field at offset four in a 24-byte record array, including a bound check for zero records.
Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.
CHECK YOUR UNDERSTANDING