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"

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is the offset of the second 64-bit field in a C struct?

    Answer

    8

    The first field takes bytes 0-7, so the next 64-bit field starts at 8.

  2. Why is a struct with two longs and one char 24 bytes and not 17?

    Answer

    The size is padded to the alignment of its largest field

    Arrays of the struct need each element aligned, so the total is padded to a multiple of 8.

  3. What does resq 1 reserve inside a STRUC?

    Answer

    One quadword

    resq reserves eight bytes per unit, and the STRUC only defines offsets until it is instantiated.

  4. What is the point of naming offsets with EQU?

    Answer

    The field names become readable constants in the code

    EQU gives each field offset a name, so the code reads p + PT_Y instead of p + 8.

  5. Which operand reads the x field of a struct whose address is in RDI?

    Answer

    mov rax, [rdi + PT_X]

    The field is a memory operand at the base plus its byte offset.

  6. What does endstruc close?

    Answer

    A STRUC definition

    STRUC ... endstruc defines the field offsets, and istruc ... iend creates an instance.

  7. What does this program print? default rel PT_X equ 0 PT_Y equ 8 PT_TAG equ 16 PT_SIZE equ 24 section .data p: dq 7 dq 9 db "S" times 7 db 0 section .text global _start _start: mov al, [p + PT_TAG] mov [p + PT_TAG], al mov rax, 1 mov rdi, 1 mov rsi, p + PT_TAG mov rdx, 1 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    S

    The tag byte holds S, and the program writes that one byte.

  8. What is the size of the padded point struct with x, y and a char?

    Answer

    24

    Two quadwords and a byte take 17 bytes, padded to the next multiple of 8, which is 24.

  9. What does mov byte [rdi + PT_TAG], "A" write?

    Answer

    One byte with the value 65

    The byte size prefix and a character constant store one byte, the code of A.

  10. Why must an assembly struct match the C layout?

    Answer

    So C code and assembly code agree on field offsets

    The same memory is read by both sides, and an offset that differs reads the wrong field.

The snippets on this page are fragments, not whole programs: they show the struct layout and the field offsets. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev