flat assembler
Message board for the users of flat assembler.

Index > Windows > How to define a local structure more than one time?

Author
Thread Post new topic Reply to topic
extra_12345



Joined: 21 Apr 2020
Posts: 36
extra_12345 31 May 2020, 07:08
Hello everyone!
I'm wondering how can I define a local structure more than one copy in the procedure?
For example I want to define Point structure as local more than one copy:

Local point Point

That only declares one copy,what if I want to declare 25 copies(declare it 25 times)?

Local point Point 25

How can I make this work?does fasm has any directives for this purpose?
Post 31 May 2020, 07:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 31 May 2020, 07:36
It depends upon how you define your Point structure.

You could do it like this:
Code:
struc Point replications {
  dd replications dup (?,?)
}    
Post 31 May 2020, 07:36
View user's profile Send private message Visit poster's website Reply with quote
extra_12345



Joined: 21 Apr 2020
Posts: 36
extra_12345 31 May 2020, 07:54
revolution wrote:
It depends upon how you define your Point structure.

You could do it like this:
Code:
struc Point replications {
  dd replications dup (?,?)
}    

For example point contains x and y arguments,so I've define my structure like this:

struc Point replications {
dd replications dup (?,?)
x dd ?
y dd ?
}

Then in my procedure:

Local replications 25

Right?
Can you please be a little bit more specific?thank you
Post 31 May 2020, 07:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 31 May 2020, 08:07
To define labels within that you might want to do this:
Code:
struc Point replications {
  x dd ?
  y dd ?
  dd replications-1 dup (?,?)
}    
Then
Code:
Local my_points Point 25    
Post 31 May 2020, 08:07
View user's profile Send private message Visit poster's website Reply with quote
extra_12345



Joined: 21 Apr 2020
Posts: 36
extra_12345 31 May 2020, 08:45
so let me get this straight,i have this structure:

Code:
struct EnochFA

alpha dd ?
x dd ?
y dd ?
bright dd ?
lpstr dd ?
timer dd ?
dum1 dd ?
dum2 dd ?

ends    


to be able to define it more than once i have to modify it such as:

Code:
struct EnochFA

alpha dd ?
x dd ?
y dd ?
bright dd ?
lpstr dd ?
timer dd ?
dum1 dd ?
dum2 dd ?

dd EnochFA dup (?,?) 

ends    


and in my procedure:

Code:
local enochl EnochFA 25    


but at runtime " invalid argument" pops up right on the above local definition,i think i did something wrong?!
Post 31 May 2020, 08:45
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 31 May 2020, 09:33
When you use struct that is a macro, not the fasm native struc.

If you are using the normal fasm package struct then it doesn't support repetitions like that.
Post 31 May 2020, 09:33
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 31 May 2020, 09:46
It is one of the questions that had been answered in the old FAQ:
Quote:
How to define an array of structures?

Since structure macroinstruction needs a label for each its instance, the structure cannot be simply repeated, since in each repeat structure needs an unique label, otherwise you would get the 'symbol already defined' error. One way to overcome this is to actually create the unique label for each copy of structure, like:
Code:
   StrucArray:
    rept 100
     {
       local s
       s MYSTRUC
     }    

The other solution can be used when array doesn't have to be initialized and your data structure has the fixed size - like the ones defined with the struct macro (the struct defines the constant holding the size of structure with the name created by attaching sizeof. before the name of structure). Then you can easily calculate how much space the array needs and just reserve this amount of bytes:
Code:
   StrucArray rb 100*sizeof.MYSTRUC    


I considered this FAQ deprecated, in favor of more recently updated FAQs on this board, but if such questions keep showing up perhaps we should copy these old answers to the forum-hosted FAQs somewhere.
Post 31 May 2020, 09:46
View user's profile Send private message Visit poster's website Reply with quote
extra_12345



Joined: 21 Apr 2020
Posts: 36
extra_12345 31 May 2020, 10:22
Tomasz Grysztar wrote:
It is one of the questions that had been answered in the old FAQ:
Quote:
How to define an array of structures?

Since structure macroinstruction needs a label for each its instance, the structure cannot be simply repeated, since in each repeat structure needs an unique label, otherwise you would get the 'symbol already defined' error. One way to overcome this is to actually create the unique label for each copy of structure, like:
Code:
   StrucArray:
    rept 100
     {
       local s
       s MYSTRUC
     }    

The other solution can be used when array doesn't have to be initialized and your data structure has the fixed size - like the ones defined with the struct macro (the struct defines the constant holding the size of structure with the name created by attaching sizeof. before the name of structure). Then you can easily calculate how much space the array needs and just reserve this amount of bytes:
Code:
   StrucArray rb 100*sizeof.MYSTRUC    


I considered this FAQ deprecated, in favor of more recently updated FAQs on this board, but if such questions keep showing up perhaps we should copy these old answers to the forum-hosted FAQs somewhere.

That worked flawlessly,thank you Tomasz.
Post 31 May 2020, 10:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 31 May 2020, 10:24
Tomasz Grysztar wrote:
It is one of the questions that had been answered in the old FAQ:
Quote:
How to define an array of structures?

Since structure macroinstruction needs a label for each its instance, the structure cannot be simply repeated, since in each repeat structure needs an unique label, otherwise you would get the 'symbol already defined' error. One way to overcome this is to actually create the unique label for each copy of structure, like:
Code:
   StrucArray:
    rept 100
     {
       local s
       s MYSTRUC
     }    

The other solution can be used when array doesn't have to be initialized and your data structure has the fixed size - like the ones defined with the struct macro (the struct defines the constant holding the size of structure with the name created by attaching sizeof. before the name of structure). Then you can easily calculate how much space the array needs and just reserve this amount of bytes:
Code:
   StrucArray rb 100*sizeof.MYSTRUC    


I considered this FAQ deprecated, in favor of more recently updated FAQs on this board, but if such questions keep showing up perhaps we should copy these old answers to the forum-hosted FAQs somewhere.
Neither of those gives access to the structure labels.

But like this we can make the label StrucArray more useful.
Code:
   StrucArray MYSTRUC
    rept 100-1
     {
       local ..s
       ..s MYSTRUC
     }    
Then
Code:
mov eax,[StrucArray.x] ; access the x value within MYSTRUC    
Post 31 May 2020, 10:24
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 31 May 2020, 10:36
revolution wrote:
Neither of those gives access to the structure labels.
They don't need to. The assumption is that you access an array through (movable) pointers in registers, as this the usual pattern:
Code:
        mov     eax,[ebx+MYSTRUC.x]    
Post 31 May 2020, 10:36
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 31 May 2020, 11:05
Tomasz Grysztar wrote:
revolution wrote:
Neither of those gives access to the structure labels.
They don't need to. The assumption is that you access an array through (movable) pointers in registers, as this the usual pattern:
Code:
        mov     eax,[ebx+MYSTRUC.x]    
I don't see the harm in making it more flexible though. Both methods can be accommodated.
Post 31 May 2020, 11:05
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1794
Roman 31 May 2020, 11:07
Quote:
StrucArray MYSTRUC
rept 100-1
{
local ..s
..s MYSTRUC
}

How get from 10 StrucArray mov eax,[StrucArray.x+sizeof.MYSTRUC*10] ? Or exist another way ?
mov eax,[StrucArray.x] get first from StrucArray
Post 31 May 2020, 11:07
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 31 May 2020, 11:40
It would be best if we can pick up the size from the defined label, not the structure itself.
Code:
mov eax,[StrucArray.x + sizeof.StrucArray * 10]    
Then if there is a change to a new structure we only need to change it in one area.
Code:
   StrucArray MYSTRUC2 ; new name
    rept 100-1
     {
       local ..s
       ..s MYSTRUC2 ; new name
     }

;...

mov eax,[StrucArray.x + sizeof.StrucArray * 10] ; no change    
Post 31 May 2020, 11:40
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 31 May 2020, 12:01
revolution wrote:
It would be best if we can pick up the size from the defined label, not the structure itself.
Code:
mov eax,[StrucArray.x + sizeof.StrucArray * 10]    
This works out of the box with fasmg's implementation:
Code:
include 'macro/struct.inc'
include 'cpu/p6.inc'

struct MYSTRUC2
        x dd ?
ends

StrucArray MYSTRUC2
rept 100-1
       MYSTRUC2 
end rept

mov eax,[StrucArray.x + sizeof StrucArray * 10]    
On the other hand, if one made a specialized macro for defining arrays of structures, one might perhaps prefer to make the main label have "sizeof" with the actual total size of the array?
Code:
struc starr? name*, count:1
        label . : sizeof name * count
        .item name
        rept count-1
                name
        end rept
end struc
StrucArray starr MYSTRUC2,100

mov ecx,sizeof StrucArray
mov eax,[StrucArray.item.x + sizeof StrucArray.item * 10]    

I would say that seeing all the differing preferences for code/syntax patterns (discussed on the forums from the beginning of their history) was probably one of the main motivators for me to design fasmg with its frenzied levels of customizability.
Post 31 May 2020, 12:01
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4043
Location: vpcmpistri
bitRAKE 31 May 2020, 18:53
Reading through your latest struct implementation, I couldn't think of a situation where it didn't work. Just amazing, and a great "go-to" for what is possible.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 31 May 2020, 18:53
View user's profile Send private message Visit poster's website 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.