flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Accessing struct members from a struct in memory

Author
Thread Post new topic Reply to topic
413x1nkp



Joined: 13 Jan 2026
Posts: 4
413x1nkp 13 Jan 2026, 02:31
Hello everyone! I've been trying out FASM, and recently found my way towards macros. Struc caught my attention, but i can't seem to make it work as well as I'd hoped.

I tried searching on this forum for a similar thread, and someone managed to offset the memory by the label within a struc. I tried replicating this, but was met an error instead.

Is there a new way of doing this? Or am I doing something wrong?
Obviously I could just hardcode the offset, but that'd lead to errors if i were to change the struct in the future.

Code:
format ELF64

struc Struc_DA {
    .items dq 0
    .count dq 0
    .capacity dq 0
    align 16
}

section '.text' executable align 16

extrn _exit

public _start

;; expects rdi - ptr to dynamic array
;; expects rsi - expected capacity
;
;; returns garbage
da_reserve:
    push rbp
    mov rbp, rsp
    sub rsp, 16

    lea rax, [rdi]

    ;; fasm error: undefined symbol 'Struc_DA.capacity'.
    cmp qword [rax+Struc_DA.capacity], rsi

    ;; . . .

    leave
    ret

_start:
    lea rdi, [my_da]

    call da_reserve

    mov rdi, [my_da.items]
    call plt _exit


section '.data' writeable align 16
my_da Struc_DA
    
Post 13 Jan 2026, 02:31
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20825
Location: In your JS exploiting you and your system
revolution 13 Jan 2026, 02:59
There needs to be a label defined that instantiates the struc:
Code:
struc Struc_DA {
    .items dq 0
    .count dq 0
    .capacity dq 0
    align 16
}
virtual at 0
        Struc_DA Struc_DA   ; <--- label 'Struc_DA' has the same name as the struc 'Struc_DA'
end virtual    
Post 13 Jan 2026, 02:59
View user's profile Send private message Visit poster's website Reply with quote
413x1nkp



Joined: 13 Jan 2026
Posts: 4
413x1nkp 13 Jan 2026, 03:08
revolution wrote:
There needs to be a label defined that instantiates the struc:
Code:
struc Struc_DA {
    .items dq 0
    .count dq 0
    .capacity dq 0
    align 16
}
virtual at 0
        Struc_DA Struc_DA   ; <--- label 'Struc_DA' has the same name as the struc 'Struc_DA'
end virtual    


Thank you! This worked perfectly :)
Post 13 Jan 2026, 03:08
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.