News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

alignment declared in structure don't work

Started by ToutEnMasm, February 08, 2008, 02:41:13 PM

Previous topic - Next topic

ToutEnMasm

Hello,
I try to align a structure as follow

Quote
   .486
   .model flat, stdcall
   option casemap :none   ; case sensitive
                ;............. include,...
LKEYDATA STRUCT 16
               ;another maner to write a record
                RepeatCount              DD (1111111111111111b    SHL 0)
                ScanCode                   DD (11111111b       SHL 16)   
                ExtendedKey              DD (1b         SHL 24)
                Reserved                    DD (1111b         SHL 25)
                ContextCode              DD (1b         SHL 29)
                PreviousKeyState        DD (1b         SHL 30)
                TransitionState           DD (1b         SHL 31)
LKEYDATA ENDS
.data
onebyte db 0
;---- here align 16 work
clavier LKEYDATA <>
.code
         lea eax,infoclav      ;41c008
         xor edx,edx
         mov ecx,16
         div ecx
         .if edx != 0
               invoke MessageBox,NULL,SADR("LKEYDATA bad Alignement"),SADR("infoclav"),MB_OK
         .endif

invoke exitprocess,NULL


Any idea ?



MichaelW

AFAIK the only valid values for the alignment are 1, 2, and 4, the actual alignment is the minimum of the field size and the specified alignment, and this applies only to the alignment between the fields in the structure.  To control the alignment of the structure in memory you must align the data definition. You can also control the alignment between the fields in the structure by placing alignment directives in the structure declaration. So this structure declaration:

    LKEYDATA STRUCT
      ;another maner to write a record
      RepeatCount       DD (1111111111111111b SHL 0)
      align 16
      ScanCode          DD (11111111b SHL 16)
      align 16
      ExtendedKey       DD (1b SHL 24)
      align 16
      Reserved          DD (1111b SHL 25)
      align 16
      ContextCode       DD (1b SHL 29)
      align 16
      PreviousKeyState  DD (1b SHL 30)
      align 16
      TransitionState   DD (1b SHL 31)
    LKEYDATA ENDS


Combined with this data definition:

      align 16
      clavier LKEYDATA <>


Should ensure that all of the fields will be aligned on a 16-byte boundary.
eschew obfuscation

ToutEnMasm


I have written a macro for testing the alignment.
Something put me on trouble.
Using ml /Zp4 must align the structures,don't work also
Quote
.data
onebyte db 0
clavier LKEYDATA <>          ;isn't align to 4 by ml /Zp4




Quote
   TESTALIGN MACRO argument
   Local findesalign
   lea eax,argument      
   xor edx,edx
   mov ecx,16
   div ecx
   .if edx == 0
      invoke MessageBox,NULL,SADR("16"),SADR("Alignment 16"),MB_OK
      jmp findesalign      
   .endif
   lea eax,argument      
   xor edx,edx
   mov ecx,8
   div ecx   
   .if edx == 0
      invoke MessageBox,NULL,SADR("QWORD"),SADR("Alignment 8"),MB_OK
      jmp findesalign      
   .endif
   lea eax,argument      
   xor edx,edx
   mov ecx,4
   div ecx   
   .if edx == 0
      invoke MessageBox,NULL,SADR("DWORD"),SADR("Alignment 4"),MB_OK
      jmp findesalign      
   .endif
   lea eax,argument      
   xor edx,edx
   mov ecx,2
   div ecx   
   .if edx == 0
      invoke MessageBox,NULL,SADR("WORD"),SADR("Alignment 2"),MB_OK
      jmp findesalign      
   .endif
   lea eax,argument      
   xor edx,edx
   mov ecx,1
   div ecx   
   .if edx == 0
      invoke MessageBox,NULL,SADR("BYTE"),SADR("Alignment 1"),MB_OK
      jmp findesalign      
   .endif               
   findesalign:
   ENDM