NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: paml27 on April 07, 2020, 12:36:52 AM

Title: How to reference the fields of a struc?
Post by: paml27 on April 07, 2020, 12:36:52 AM
In the .data section I defined a struc:

%define O_RDWR 00000002

struc mq_attr
  .mq_flags: resd 1
  .mq_maxmsg: resd 1
  .mq_msgsize: resd 1
  .mq_curmsgs: resd 1
endstruc

mq_attributes:
  istruc mq_attr
    at mq_attr.mq_flags, dd O_RDWR
    at mq_attr.mq_maxmsg, dd 100
    at mq_attr.mq_msgsize, dd 1000 ;656
    at mq_attr.mq_curmsgs, dd 0
  iend

In the text (code) section, I followed the directions in the 2.13.02 manual to examine the structure elements, but only the third element shows correctly:

194     mov rax,[mq_attributes+mq_attr.mq_flags]
rax            0x6400000002     429496729602
195     mov rax,[mq_attributes+mq_attr.mq_maxmsg]
rax            0x3e800000064    4294967296100
196     mov rax,[mq_attributes+mq_attr.mq_msgsize]
rax            0x3e8    1000
197     mov rax,[mq_attributes+mq_attr.mq_curmsgs]
rax            0x6e616c6300000000       7953757589370568704

Did I calculate the offsets correctly (e.g. mq_attributes+mq_attr.mq_flags)?  If so, why do elements 1, 2 and 4 not show correctly? 

Thanks very much. 




Title: Re: How to reference the fields of a struc?
Post by: fredericopissarra on April 07, 2020, 10:51:36 AM
Since the structure fields are DWORDs, you should use, for example:

Code: [Select]
; the upper 32 bits will be zeroed!
mov eax,[mq_attributes+mq_attr.mq_flags]
Title: Re: How to reference the fields of a struc?
Post by: paml27 on April 07, 2020, 02:42:20 PM
Thanks, Frederico.  That turned out to be the right answer.  I'm surprised it didn't zero-extend into rax.