flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Help initializing array of struct

Author
Thread Post new topic Reply to topic
alorent



Joined: 05 Dec 2005
Posts: 201
alorent
Hellp guys,

I have the following struct (taken from another post):

Code:
struc StructureName x,y,life,color,direc 
{ 
.x dw x 
.y dw y 
.life dw life 
.color dw color 
.direc dw direc 
}     


And I would like to initialize several (about 50) sequences of that structure. Example:

Code:
ArrayStructure  StructureName 0,0,0,1,-4
                     StructureName 0,0,0,2,-4   ; <-- ERROR!!!
                     StructureName 0,0,0,3,-4
                     StructureName 0,0,0,4,-4
                     StructureName 0,0,0,5,-4
                     StructureName 0,0,0,6,-4
                     StructureName 0,0,0,7,-4
                     StructureName 0,0,0,8,-4
                     StructureName 0,0,0,9,-4
                     StructureName 0,0,0,1,-4
                     StructureName 0,0,0,2,-4
                     StructureName 0,0,0,3,-4
                     StructureName 0,0,0,4,-4
                     StructureName 0,0,0,5,-4
    


I get FASM error as it seems that I have to name each line with a variable name. Is it possible to define the above withouth giving multiple variable names for each "StructureName" initialization?

Thanks!
Post 21 May 2010, 11:51
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
Add this somewhere before "ArrayStructure":
Code:
macro StructureName x,y,life,color,direc
{
 dw x
 dw y
 dw life
 dw color
 dw direc
}    
Post 21 May 2010, 12:01
View user's profile Send private message Visit poster's website Reply with quote
alorent



Joined: 05 Dec 2005
Posts: 201
alorent
Thanks revolution! It works as you mention.

But, is there any "cleaner" way to do it? I mean, with your solution, if I modify the structure I have to make sure that I also update the macro to avoid inconsistences, which produces a much difficult code to maintain.

Thanks!
Post 21 May 2010, 12:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
Try this:
Code:
macro StructureName [args]
{
common
local ..x
..x StructureName args
}    
Post 21 May 2010, 12:22
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
I think by including win32 struct macro we can get all the
functionality we need, correct me if i am wrong...
Code:
include 'macro\struct.inc'

struct StructureName
  x     dw ?
  y     dw ?
  life  dw ?
  color dw ?
  direc dw ?
ends

ArrayStructure:
  StructureName 0,0,0,1,-4
  StructureName 0,0,0,2,-4
  StructureName 0,0,0,3,-4
  StructureName 0,0,0,4,-4
  StructureName 0,0,0,5,-4
  StructureName 0,0,0,6,-4
  StructureName 0,0,0,7,-4
  StructureName 0,0,0,8,-4
  StructureName 0,0,0,9,-4
  StructureName 0,0,0,1,-4
  ...

; TESTING IT...

mov ebx,ArrayStructure
mov ax,[StructureName.x+ebx] ; first instance.x

add ebx,sizeof.StructureName
mov dx,[StructureName.y+ebx] ; second instance.y
    
Post 21 May 2010, 13:04
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
bitshifter,

You're right. Even more, struct macro defines its argument as both macro and struc so you can use ArrayStructure StructureName too.
Post 21 May 2010, 15:50
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
And you dont even need to initialize the values Razz
Code:
ArrayStructure:
  repeat 256
    StructureName
  end repeat    
Post 21 May 2010, 19:41
View user's profile Send private message Reply with quote
alorent



Joined: 05 Dec 2005
Posts: 201
alorent
Thanks a lot bitshifter! Exactly what I was looking for Very Happy
Post 24 May 2010, 10:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.