News:

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

Nested arrays of structures

Started by Yuri, April 11, 2010, 06:34:22 AM

Previous topic - Next topic

Yuri

It seems impossible to declare a structure that contains an array of other structures (unless I am missing some trick).

ONE STRUCT
a DD
b DD
ENDS

TWO STRUCT
c DD
d ONE 3 DUP
ENDS


DATA SECTION

example TWO


CODE SECTION

Start:
xor eax,eax
ret


Quote
GoAsm.Exe Version 0.56.8 - Copyright Jeremy Gordon 2001/9 - JG@JGnet.co.uk

Error!
Line 14 of assembler source file (struct.asm):-
Must use <> or {} brackets when initialising structure or union
  In the struct at Line 6 of assembler source file (struct.asm)

I tried to initialize it in two ways, but none of them worked (and the error message was the same as above):

example TWO <0,<0,0>,<0,0>,<0,0>>

example TWO <0,<<0,0>,<0,0>,<0,0>>>


Even if they had worked, it would be no solution to my real situation where the array should contain 256 structures.

The help file has no example of this type of structure, so it's probably just not implemented. In my opinion, it would be a useful feature. But as Jeremy seems to have left forever, I am getting desperate with all these struct/union problems.  :'(

donkey

Hi Yuri,

Weird one, can't even get MASM to do it...

ONE STRUCT
a DD ?
b DD ?
ONE ENDS

TWO STRUCT
c DD ?
d ONE 256 DUP (<>)
TWO ENDS

.data?
testtwo TWO <>


Any attempt to initialize data in the structure gets me a "error A2177: nested structure improperly initialized". This seems to be the same behavior as in GoAsm except that in GoAsm you can't even name the nested structure member...

ONE STRUCT
a DD
b DD
ENDS

TWO STRUCT
c DD
ONE 256 DUP <>
ENDS

DATA SECTION
testtwo TWO <>


"SIZEOF testtwo" returns 2052 in both GoAsm and MASM.

To get the nested structure named you could do this:

ONE STRUCT
a DD
b DD
ENDS

TWO STRUCT
c DD
d ONE <>
ONE 255 DUP <>
ENDS


Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Yuri

Yes, this way it works! :bg Thanks, Edgar! :clap:

z941998

Wouldn't this work also?

ONE STRUCT
   a DD ?
   b DD ?
ONE ENDS

TWO STRUCT
   c DD ?
   d 256 DUP ONE
TWO ENDS

Yuri

I would be much surprised if it worked. But it doesn't, at least in GoAsm.
Quote
Error!
Line 14 of assembler source file (struct.asm):-
Invalid data/structure/union declaration in implementation of structure:-
d 256 DUP ONE
  In the struct at Line 6 of assembler source file (struct.asm)

jj2007

This works in Masm.
include \masm32\include\masm32rt.inc

ONE STRUCT
   za DD ?
   zb DD ?
ONE ENDS

TWO STRUCT
   zc DD ?
   zd ONE 20 dup(<?>)
TWO ENDS

.data
MyTwo TWO <>

element = SIZEOF ONE

.code
AppName db "Masm32:", 0

start: mov MyTwo.zc, 123
mov MyTwo.zd[19*element].za, 123
print str$(MyTwo.zd[19*element].za)
inkey " OK"
exit
end start

donkey

Hi jj2007,

Yes, it works in GoAsm as well, the problem was in initializing it not accessing it. ie:

ONE STRUCT
a DD
b DD
ENDS

TWO STRUCT
c DD
d ONE <>
ONE 255 DUP <>
ENDS

DATA SECTION
testtwo TWO <>

CODE SECTION
mov D[testtwo.d+(SIZEOF ONE*19)+ONE.b],1
; or more clearly...
mov D[testtwo.d.b+(SIZEOF ONE*19)],1
; or even...
mov D[(SIZEOF ONE*19)+testtwo.d.b],1


Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Yuri

With structures like ACCEL (it's 6 bytes in size), you just can't make up an array because GoAsm aligns each structure on a dword boundary, so that it actually consumes 8 bytes. Due to this, only the first accelerator will work. Finally I had to use plain DW instead.

#dynamiclinkfile msvcrt.dll

ACCEL STRUCT
    fVirt DW  ; fVirt is DB in the headers, but it needs
    key DW    ; an alignment byte after it.
    cmd DW
ENDS


DATA SECTION

Accels  ACCEL <1,2,3>,<4,5,6>,<7,8,9>
cAccels DD sizeof Accels / sizeof ACCEL


CODE SECTION

Start:
    invoke printf, "Array size: %d, structures in array: %d", \
                sizeof Accels, [cAccels]
    add esp,0Ch
    ret


printf's output:
Quote
Array size: 22, structures in array: 4