News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Having Problem with nested structures

Started by Drakasm, November 03, 2012, 04:18:28 AM

Previous topic - Next topic

Drakasm

Dear gentlemen I have a problem with the nested structures below. :icon_eek:
I would be grateful if you can tell me what am I doing wrong.
I would also ask if it is possible some details relating to the structures. ::)
1) Local structures need to be aligned ?
2) How can i efficiently align custom structures ?
3) Structures predefined like ( rect, point, etc.. ) are aligned ?
4) The structures below are aligned ?
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
dosing STRUCT
      drugMl     dq  3 dup (?)
      drugCt     dd  4 dup (?)
      drugDs     dd  3 dup (?)
      Side       dd         ?
      Response   db  3 dup (?)
dosing ends
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Variation STRUCT
state1  dd 2 dup (?)
         state2  dd 2 dup (?)
         state3  dd 2 dup (?)
Variation ends
;<<<<<<<<<<<<<<<<<<<<<<<<<<<
conditions STRUCT
Current   dd 3 dup (?)
NewVa Variation 2 dup (<>)
conditions ends
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
classification STRUCT
folder db 200 dup (?)
align 4
         identification dd ?
         subfolder      dd ?
         internal       dd ?
         esternal       dd ?
classification ends
;<<<<<<<<<<<<<<<<<<<<<<<<<<<
patient STRUCT
  name      db 200 dup (?)
  align 4
    special     dd ?
    handicap    dd ?
    active      db ?
    present     db ?
      align 4
     datav       dd ?
     datag       dd ?
     datah       dd ?
Class          classification <>
ActConditions  conditions 2 dup (<>)
ACTdosing      dosing  4 dup (<>)
CriticalSel    db 3 dup (?)
patient ends
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.data ?

NewDepartment  patient  30 dup (<>) ;Problem Masm says structure improperly initialized
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
NewDepartment  patient <>         ;No problem program compiles

Zen

DRAKASM,
You can use the ALIGN directive in your code.
If you search the Old MASM Forum for the phrase: ALIGN directive, you will get a long list of Forum discussions on the subject.
For example: [q]forcing data/code alignment, June 2005
...Or,... Alignment, Aug 2009
This is interesting: im confused. Oct 2011
Here is a discussion of alignment timing access issues: Memory Alignment, June 2001

QuoteSyntax: ALIGN [number]
ALIGN aligns the next variable or instruction on an offset address that is a multiple of [expression]. The [number] parameter must be a power of 2 [1,2,4,8,...] less than or equal to the alignment of the current segment. If [number] is omitted, the alignment is determined by the align field of the preceding SEGMENT directive.

In your case, you can either use the ALIGN directive, or simply change the:
Response   db  3 dup (?), member of the dosing STRUCT to a multiple of 32-bits, say, a DWORD, using bit flags to indicate the action.
I think the default alignment is 32-bit (4 bytes).

Disclaimer: If I'm WRONG,...and, statistically this is a near certainty,...just see DAVE,...below,...he's our resident Assembly Programming God,...


dedndave

placing "align 4" in the middle of a structure definition is probably not a good plan
it is possible that the assembler places 0's in there instead of ?'s - causing an error
you can ensure that structure members are 4 aligned in the STRUCT line
SomeStruct STRUCT DWORD
;
;
SomeStruct ENDS


in some cases, i would be tempted to do some "manual" padding
for instance....
dosing STRUCT
      drugMl     dq  3 dup (?)
      drugCt     dd  4 dup (?)
      drugDs     dd  3 dup (?)
      Side       dd         ?
      Response   db  3 dup (?)
dosing ends

i would probably make the Response member 4 bytes in size or add a pad byte like so...
dosing STRUCT
      drugMl     dq  3 dup (?)
      drugCt     dd  4 dup (?)
      drugDs     dd  3 dup (?)
      Side       dd         ?
      Response   db  3 dup (?)
      bPad       db  ?
dosing ends


local structures do not necessarily need to be aligned
MASM's default prologue will 4-align local data items so that the stack is always 4-aligned

many of the structures you have defined have members already aligned
for example....
classification STRUCT
folder db 200 dup (?)
align 4
         identification dd ?
         subfolder      dd ?
         internal       dd ?
         esternal       dd ?
classification ends

because the 200 byte definition is evenly divisable by 4, the second member will be aligned the same as the structure

also - if you use dwords or qwords, their sizes are always evenly divisable by 4

i try to design all my custom structures so that dword members are 4-aligned
i rarely use qword members, but if i did, i'd try to get them 8-aligned   :P

i also try to define all my custom structures so that their overall size is divisable by 4
that way, when you define them in the .DATA? section, you only need align the first one

jj2007

Try this to get rid of the improperly etc error:

ActConditions  conditions <>, <>      ;2 dup (<>)
ACTdosing      dosing  <>, <>, <>, <>   ;4 dup (<>)


Re aligning:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<
patient STRUCT DWORD    ; DWORD doesn't help, no effect
...
.data?

NewDepartment1  patient  30 dup (<>)
db ?   ; force misalignment
align DWORD   ; this helps
NewDepartment2  patient  30 dup (<>)

Drakasm

Thank you gentlemen very much !!!!!!!!!!! :t
QuoteActConditions  conditions <>, <>      ;2 dup (<>)
ACTdosing      dosing  <>, <>, <>, <>   ;4 dup (<>)
:t    This one worked perfectly but why remains a mystery  :eusa_clap:
QuoteFor example: [q]forcing data/code alignment, June 2005
...Or,... Alignment, Aug 2009
This is interesting: im confused. Oct 2011
Here is a discussion of alignment timing access issues: Memory Alignment, June 2001
:t Excellent links i have to read them all !!!!!! :icon14:
Quotei would probably make the Response member 4 bytes in size or add a pad byte like so...
This is an insight  :P
Quotei try to design all my custom structures so that dword members are 4-aligned
i rarely use qword members, but if i did, i'd try to get them 8-aligned   

i also try to define all my custom structures so that their overall size is divisable by 4
that way, when you define them in the .DATA? section, you only need align the first one
This is also an insight :greenclp:
Thank you all very much !!!!!!!!!! :biggrin:

japheth


You should avoid using "name" as a field name - because NAME is a no-op directive, and Masm accepts anything behind the directive without warning.

Either avoid "name" as field name or, even better, remove the NAME directive:

    option nokeyword:<name>

Drakasm

QuoteYou should avoid using "name" as a field name - because NAME is a no-op directive, and Masm accepts anything behind the directive without warning.

Got it.  Name is history......  :icon_exclaim:
Thanks Japheth :P