### Author Topic: are you sure that 4+4+1=9 ?  (Read 13282 times)

#### TouEnMasm

• Member
• Posts: 1762
##### are you sure that 4+4+1=9 ?
« on: September 06, 2012, 11:04:06 PM »

I have tested the STORAGE_PROPERTY_QUERY structure here his:
Quote
STORAGE_PROPERTY_QUERY   STRUCT
PropertyId DWORD ?
QueryType DWORD ?
AdditionalParameters BYTE 1 dup (?)
STORAGE_PROPERTY_QUERY      ENDS
For each individual part of the structure the c++ agree with the size
stock = sizeof(STORAGE_PROPERTY_QUERY);      12
stock = sizeof(mesure.PropertyId);                     4
stock = sizeof(mesure.QueryType);                     4
stock = sizeof(mesure.AdditionalParameters);     1

4+4+1=12    ???
I suppose that an alignment or a round is made for the byte.
How to do the same thing with masm ??????

Fa is a musical note to play with CL

#### TouEnMasm

• Member
• Posts: 1762
##### Re: are you sure that 4+4+1=9 ?
« Reply #1 on: September 07, 2012, 12:50:24 AM »

to know where are added the three bytes for alignment ,I try to get the adress of each member of the structure.

Quote
DWORD TestSize(STORAGE_PROPERTY_QUERY * essai)
{
STORAGE_PROPERTY_QUERY * machin;
STORAGE_PROPERTY_QUERY * chose;

chose = essai;                ok here,adress of the structure
machin = * essai->PropertyId; ????? failed
return 0;
}
Perhaps somebody here know how to get the adress of essai.PropertyId ?

Fa is a musical note to play with CL

#### qWord

• Member
• Posts: 1475
• The base type of a type is the type itself
##### Re: are you sure that 4+4+1=9 ?
« Reply #2 on: September 07, 2012, 12:58:44 AM »
to know where are added the three bytes for alignment ,I try to get the adress of each member of the structure.
The bytes are appended. To get a reference:  &essai->PropertyId.
MREAL macros - when you need floating point arithmetic while assembling!

#### dedndave

• Member
• Posts: 8828
• Still using Abacus 2.0
##### Re: are you sure that 4+4+1=9 ?
« Reply #3 on: September 07, 2012, 02:17:27 AM »
Code: [Select]
`STORAGE_PROPERTY_QUERY   STRUCT DWORD   PropertyId DWORD ?   QueryType DWORD ?   AdditionalParameters BYTE ?STORAGE_PROPERTY_QUERY      ENDS`
doesn't that work ???
otherwise - you might try the OPTION directive to set the structure alignment

if neither of those works....
Code: [Select]
`STORAGE_PROPERTY_QUERY   STRUCT   PropertyId DWORD ?   QueryType DWORD ?   AdditionalParameters BYTE ?  BYTE 3 dup(?)STORAGE_PROPERTY_QUERY      ENDS`
:P

#### MichaelW

• Global Moderator
• Member
• Posts: 1196
##### Re: are you sure that 4+4+1=9 ?
« Reply #4 on: September 07, 2012, 02:18:27 AM »
The attachment contains a C test and an ASM test. I think for structures that end with a 1-element array, the general expectation is that you allocate memory for the structure and use the structure definition as a template.
Well Microsoft, here’s another nice mess you’ve gotten us into.

#### TouEnMasm

• Member
• Posts: 1762
##### Re: are you sure that 4+4+1=9 ?
« Reply #5 on: September 07, 2012, 02:46:19 AM »

Ok , the & is an answer.
last question is , how i declare machin to made this work?
i have tried int dword ...
Quote
machin = (&essai - &essai->AdditionalParameters );
Fa is a musical note to play with CL

#### MichaelW

• Global Moderator
• Member
• Posts: 1196
##### Re: are you sure that 4+4+1=9 ?
« Reply #6 on: September 07, 2012, 03:23:43 AM »
This should work:
Code: [Select]
`int machin;. . .machin = ((int)&essai->PropertyId - (int)&essai->AdditionalParameters );printf("%d\n\n", machin);`
But if you want the offset of the AdditionalParameters member from the start of the structure you will need to reverse the subtraction, so the result will be 8 instead of -8.
Well Microsoft, here’s another nice mess you’ve gotten us into.

#### qWord

• Member
• Posts: 1475
• The base type of a type is the type itself
##### Re: are you sure that 4+4+1=9 ?
« Reply #7 on: September 07, 2012, 03:29:37 AM »
The rules for aligment in structures can read up here: Structure Alignment
MREAL macros - when you need floating point arithmetic while assembling!

#### japheth

• Guest
##### Re: are you sure that 4+4+1=9 ?
« Reply #8 on: September 07, 2012, 03:36:49 AM »

to get the offset of a member within a struct, the C offsetof() macro is probably the best choice:

mbrofs = offsetof( struct xxx, member );

#### TouEnMasm

• Member
• Posts: 1762
##### Re: are you sure that 4+4+1=9 ?
« Reply #9 on: September 07, 2012, 04:40:32 AM »
Thanks at all for answers.
Here a resume of my test:
Quote
DWORD TestSize(STORAGE_PROPERTY_QUERY * essai)
{
int machin;
STORAGE_PROPERTY_QUERY * chose;
chose = essai;
machin = ((int)&essai->PropertyId - (int)&essai->AdditionalParameters );
//machin = 8 or -8
machin = offsetof( STORAGE_PROPERTY_QUERY,AdditionalParameters );
// machin = 8 need  #include <stddef.h>
return 0;
}

And for masm the good method to declare an alignment for a structure
Quote
STORAGE_PROPERTY_QUERY   STRUCT DWORD
PropertyId DWORD ?
QueryType DWORD ?
AdditionalParameters BYTE 1 dup(?) ; dup (?)
STORAGE_PROPERTY_QUERY      ENDS
This give the good size and the good alignment
I have learn much today.
Fa is a musical note to play with CL

#### jj2007

• Member
• Posts: 13299
• Assembly is fun ;-)
##### Re: are you sure that 4+4+1=9 ?
« Reply #10 on: September 07, 2012, 08:38:01 AM »
AdditionalParameters BYTE 1 dup(?) ; dup (?)
and

are identical.

AdditionalParameters BYTE 4 dup(?)

achieves align DWORD, too.

#### dedndave

• Member
• Posts: 8828
• Still using Abacus 2.0
##### Re: are you sure that 4+4+1=9 ?
« Reply #11 on: September 07, 2012, 11:45:14 AM »
hiya Jochen
this one yields the correct result for SIZEOF AdditionalParameters
Code: [Select]
`STORAGE_PROPERTY_QUERY   STRUCT   PropertyId DWORD ?   QueryType DWORD ?   AdditionalParameters BYTE ?  BYTE 3 dup(?)STORAGE_PROPERTY_QUERY      ENDS`

#### TouEnMasm

• Member
• Posts: 1762
##### Re: are you sure that 4+4+1=9 ?
« Reply #12 on: September 07, 2012, 03:22:46 PM »

The interest of all this is to give at masm the correct alignment for each structure as do the c++.
here is a sample of translate,winhttp sample:
Quote
// defaut alignment for c++ is 4 ,DWORD for masm
#if defined(_WIN64)
#include <pshpack8.h>
#else
#include <pshpack4.h> //return to the defaut alignment
#endif
................ here structures ----------------------
#include <poppack.h>

can be done as this with masm
Quote
IF DEFINED(_WIN64)
;Include pshpack8.SDK
DEFALIGNMASM TEXTEQU <QWORD>
ELSE
;Include pshpack4.SDK
DEFALIGNMASM TEXTEQU <DWORD>
ENDIF
................ here structures ----------------------
ANYSTRUCTURE STRUCT DEFALIGNMASM
..........
ANYSTRUCTURE ENDS
;include <poppack.h>
DEFALIGNMASM TEXTEQU <DWORD>   ;return to defaut alignment
I have verify that this soluce work.
adding bytes to the structure isn't a good way
Fa is a musical note to play with CL

#### japheth

• Guest
##### Re: are you sure that 4+4+1=9 ?
« Reply #13 on: September 07, 2012, 05:07:34 PM »
> I have verify that this soluce work.

It works most of the time, but AFAIR the Win32 alignment is also 8 (=QWORD), not DWORD.

It just doesn't matter usually because in Win32 there are very few QWORD members found in the structures.

But for an example where alignment DWORD doesn't work in Win32, see structure PARTITION_INFORMATION in winioctl.h.

So the simplest solution may just be to add -Zp8 as cmdline parameter when you want all Windows structures to be correct.

#### TouEnMasm

• Member
• Posts: 1762
##### Re: are you sure that 4+4+1=9 ?
« Reply #14 on: September 07, 2012, 06:57:24 PM »

The Zp4 or Zp8 option are only a partial solution.
This don't avoid the "Invalid parameter" send by the function receiving a bad size of the structure.
With such an error,there is only to search , can be a long time.
Fa is a musical note to play with CL