Structures

Struct layout

A struct is a block of memory with fields at fixed offsets. To match C, order fields and pad them the way the C compiler does: eight-byte alignment for 64-bit fields on x86-64:

; struct point { long x; long y; char tag; } struc Point .x: resq 1 .y: resq 1 .tag: resb 1 endstruc

EQU offsets

Without STRUC, name the offsets by hand. The constant is the byte offset of the field, and the size constant is the total struct size:

PT_X equ 0 PT_Y equ 8 PT_TAG equ 16 PT_SIZE equ 24 ; padded to 8-byte alignment

Field access

Access a field with base register plus offset. With a pointer in RDI, the field reads and writes look like any other memory operand:

mov rax, [rdi + PT_X] mov [rdi + PT_Y], rax mov byte [rdi + PT_TAG], 'A'

Next: Macros

Article author: Arthur Isaev