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
mov eax, sizeof MYSTRUCT
is right way to grab size of a structure.
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
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
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
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