TExcelTAD
Home › x86-64 Assembly › Structures

Structures

A structure groups fields of different sizes into one block of memory, like a C struct. The CPU knows nothing about structures: a field is just "base address + offset". NASM's struc directive saves you from computing those offsets by hand.

Defining a structure with struc

nasm
struc product
    .id:        resd 1      ; 4 bytes, offset 0
    .stock:     resd 1      ; 4 bytes, offset 4
    .price:     resq 1      ; 8 bytes, offset 8 (price in cents)
    .name:      resb 16     ; 16 bytes, offset 16
endstruc

struc doesn't emit any bytes. It defines symbols: product.id = 0, product.stock = 4, product.price = 8, product.name = 16, and product_size = 32, the total size. You use them as offsets: [rbx + product.price].

nasm · try it yourself
struc product
    .id:        resd 1
    .stock:     resd 1
    .price:     resq 1
    .name:      resb 16
endstruc

section .text
global _start
_start:
    mov edi, product.id
    call print_uint
    mov edi, product.stock
    call print_uint
    mov edi, product.price
    call print_uint
    mov edi, product.name
    call print_uint
    mov edi, product_size
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
0
4
8
16
32

Creating instances with istruc

istruc / at / iend define an initialized instance in a data section. Each at line names a field and gives its value; NASM pads everything to the right offsets:

nasm · try it yourself
struc product
    .id:        resd 1
    .stock:     resd 1
    .price:     resq 1
    .name:      resb 16
endstruc

section .data
lamp:
    istruc product
        at .id,     dd 1001
        at .stock,  dd 12
        at .price,  dq 2499
        at .name,   db "Desk lamp", 0
    iend

section .text
global _start
_start:
    mov rbx, lamp               ; pointer to the structure
    mov edi, [rbx + product.id]
    call print_uint
    mov edi, [rbx + product.stock]
    call print_uint
    mov rdi, [rbx + product.price]
    call print_uint

    lea rsi, [rbx + product.name]
    mov eax, 1
    mov edi, 1
    mov edx, 9                  ; "Desk lamp"
    syscall

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
1001
12
2499
Desk lamp

Note: Inside istruc product, at .id is short for the full field name at product.id; you will see both styles. Fields you leave out (like the rest of .name) are filled with zeros.

Arrays of structures

With product_size known, element i of an array of products starts at base + i * product_size. Here we compute the total stock value Σ stock × price of an inventory:

nasm · try it yourself
struc product
    .id:        resd 1
    .stock:     resd 1
    .price:     resq 1
    .name:      resb 16
endstruc

section .data
inventory:
    istruc product
        at .id,     dd 1
        at .stock,  dd 12
        at .price,  dq 2499
        at .name,   db "Desk lamp", 0
    iend
    istruc product
        at .id,     dd 2
        at .stock,  dd 40
        at .price,  dq 350
        at .name,   db "Notebook", 0
    iend
    istruc product
        at .id,     dd 3
        at .stock,  dd 5
        at .price,  dq 8900
        at .name,   db "Chair", 0
    iend
ITEM_COUNT  equ ($ - inventory) / product_size

section .text
global _start
_start:
    mov edi, ITEM_COUNT
    call print_uint

    xor eax, eax                ; total = 0
    mov rsi, inventory          ; pointer to the current item
    mov ecx, ITEM_COUNT
.item:
    mov edx, [rsi + product.stock]
    imul rdx, [rsi + product.price]
    add rax, rdx
    add rsi, product_size       ; next item
    dec ecx
    jnz .item

    mov rdi, rax
    call print_uint             ; total value in cents

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
3
88488

That's 12 × 2499 + 40 × 350 + 5 × 8900 = 29988 + 14000 + 44500 = 88488 cents.

Passing structures to functions

Functions almost always receive a pointer to a structure, not a copy. This restock(item, amount) function updates a field in place, and a find_by_id function returns a pointer (or 0):

nasm · try it yourself
struc product
    .id:        resd 1
    .stock:     resd 1
    .price:     resq 1
    .name:      resb 16
endstruc

section .data
inventory:
    istruc product
        at .id,     dd 7
        at .stock,  dd 3
        at .price,  dq 1200
    iend
    istruc product
        at .id,     dd 9
        at .stock,  dd 0
        at .price,  dq 450
    iend
ITEM_COUNT  equ ($ - inventory) / product_size

section .text
global _start
_start:
    mov edi, 9
    call find_by_id             ; rax = pointer to product 9
    mov rbx, rax
    mov rdi, rbx
    mov esi, 25
    call restock                ; add 25 to its stock
    mov edi, [rbx + product.stock]
    call print_uint

    mov edi, 42
    call find_by_id             ; no such product
    mov rdi, rax
    call print_uint             ; 0 = "null pointer"

    mov eax, 60
    xor edi, edi
    syscall

; find_by_id(id) -> pointer to the product, or 0
find_by_id:
    mov rax, inventory
    mov ecx, ITEM_COUNT
.check:
    cmp [rax + product.id], edi
    je .found
    add rax, product_size
    dec ecx
    jnz .check
    xor eax, eax                ; not found
.found:
    ret

; restock(item, amount): item->stock += amount
restock:
    add [rdi + product.stock], esi
    ret

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
25
0

Alignment and padding

C compilers align each field to its own size (an 8-byte field starts at a multiple of 8). The product layout above was chosen so that this happens naturally. If you put a 1-byte field before an 8-byte one, add padding yourself (for example alignb 8 inside the struc) so that your structure matches what C code expects:

nasm
struc sensor
    .active:    resb 1      ; offset 0
    alignb 8                ; skip to offset 8, like a C compiler would
    .reading:   resq 1      ; offset 8
endstruc                    ; sensor_size = 16

Test yourself

What does struc ... endstruc emit into the output file?

  1. The structure's bytes
  2. Nothing: it only defines offset symbols and name_size
  3. A pointer
  4. A label in .bss

How do you get to element 3 of an array of product structures starting at rbx?

  1. rbx + 3
  2. rbx + 3 * product_size
  3. rbx + product.id * 3
  4. [rbx]*3

Why do functions usually receive a pointer to a structure?

  1. Structures can't be copied
  2. A pointer fits in one register and lets the function modify the original
  3. The ABI forbids anything else
  4. Pointers are faster to print