Reputation: 83
How can i declare a pointer for the first element in a list of a struct like this:
section .bss
struc agenda
name resb 10
number resd 10
type resw 10
endstruc
Upvotes: 5
Views: 7226
Reputation: 3119
Simply declaring the structure does not reserve memory for it. You need an instance of it. Either:
section .bss
my_agenda resb agenda_size
; or perhaps...
agenda_array resb agenda_size * MAX_COUNT
; or...
section .data
an_agenda istruc agenda
at name db "fred"
at number db "42"
at type db "regular"
iend
section .text
mov esi, an_agenda
mov al, [esi + name]
Something like that?
Heh! Jester has just posted essentially the same thing. He introduces the '.' notation for local labels. Probably a good idea. Without it, name
is a global identifier and can't be reused. It takes slightly more typing - agenda.name
, agenda.number
, agenda.type
. Probably worth it for increased clarity.
Upvotes: 6
Reputation: 58822
As usual, you should have consulted the fine nasm manual before asking. It's not too late to do that now, but I'll quickly give you the important points from there.
struc
itself doesn't allocate a structure, it defines a type. As recommended practice, you should name your fields starting with a dot (.
). Each field label will be equal to its offset in the structure, though a base address can also be added. Having declared your struct, you can allocate initialized instances with the istruc
directive. In the .bss
section, you'd want to use resb
instead, using the struc_size
symbol that the assembler defines for you.
A complete example which declares the type, defines a zero-initialized instance in the bss section and loads the address of the first member may look like this:
struc agenda
.name resb 10
.number resb 10
.type resb 10
endstruc
section .bss
myagenda: resb agenda_size
section .text
mov eax, myagenda + agenda.name
Note: agenda.name
is of course 0
, I have written it out just as an illustration how you'd do it for other members.
Upvotes: 6