The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Queue on January 18, 2011, 12:25:14 AM

Title: sizeof, size, type and structs
Post by: Queue on January 18, 2011, 12:25:14 AM
Is there a ''proper'' choice for the following situation:

MYSTRUCT struct
ValueOne DWORD ?
ValueTwo DWORD ?
MYSTRUCT ends

mov eax, MYSTRUCT
mov eax, sizeof MYSTRUCT
mov eax, size MYSTRUCT
mov eax, type MYSTRUCT

---

I ask because it seems that any work. Personally, I've been using:
mov eax, MYSTRUCT
and it seems to be working fine, but I'd like feedback to make sure this won't cause me trouble in certain situations.

If it's not obvious, I expect eax to get loaded with the size of MYSTRUCT.

Queue
Title: Re: sizeof, size, type and structs
Post by: Antariy on January 18, 2011, 12:54:16 AM

mov eax, sizeof MYSTRUCT


is right way to grab size of a structure.
Title: Re: sizeof, size, type and structs
Post by: redskull on January 18, 2011, 12:58:08 AM
Things are not created equal when you deal with arrays.  TYPE will return the size of each element, LENGTHOF will give you the total elements, and SIZEOF will give the total size.  Also, as a matter of personal taste, I would not use the direct "MOV eax, MYSTRUCT" method, as it makes thing less readable; there's no way to tell whether MYSTRUCT is a type, and thus moving the size, or a instance of that type, and moving in the actual address.  Generally I stick with SIZEOF.

-r
Title: Re: sizeof, size, type and structs
Post by: Queue on January 18, 2011, 01:02:37 AM
Quote from: redskull on January 18, 2011, 12:58:08 AM
Things are not created equal when you deal with arrays.  TYPE will return the size of each element, LENGTHOF will give you the total elements, and SIZEOF will give the total size.  Also, as a matter of personal taste, I would not use the direct "MOV eax, MYSTRUCT" method, as it makes thing less readable; there's no way to tell whether MYSTRUCT is a type, and thus moving the size, or a instance of that type, and moving in the actual address.  Generally I stick with SIZEOF.

-r
That certainly makes a lot of sense. Thanks both of you; sizeof it is.

Queue
Title: Re: sizeof, size, type and structs
Post by: dedndave on January 18, 2011, 01:36:49 AM
MYSTRUCT struct
ValueOne DWORD ?
ValueTwo DWORD ?
MYSTRUCT ends

creates a new data type

typically, we would define the data like this, however...
MYSTRUCT struct
ValueOne DWORD ?
ValueTwo DWORD ?
MYSTRUCT ends

        .DATA?

MyStruc MYSTRUCT <>

        .CODE

mov eax, sizeof MYSTRUCT

or
mov eax,sizeof MyStruc
either way works - i prefer to reference the type MYSTRUCT for the size

once you have defined the data that way, you may access members of the structure...
mov edx,MyStruc.ValueOne
Title: Re: sizeof, size, type and structs
Post by: redskull on January 18, 2011, 01:42:26 AM
To point out what I was saying before

MyStructElement MYSTRYCT <0,1>

MyStructArray MYSTRUCT <1,2>,<2,3>

mov eax, TYPE MyStructArray ; '8'
mov eax, TYPE MyStructElement ; '8'
mov eax, SIZEOF MyStructArray ; '16'
mov eax, SIZEOF MyStructElement ; '8'

etc