flat assembler
Message board for the users of flat assembler.

Index > Main > struct members with predefined offsets?

Author
Thread Post new topic Reply to topic
Big Red



Joined: 25 Feb 2005
Posts: 43
Big Red 10 Jan 2006, 14:54
Hi,

I've been trying to get this to work for quite some time now, using labels, virtual at, and lots of random stuff. I read the documentation, but it didn't really provide many clues. I'm also pretty bad with macros to start with.

Anyhow, I need to define a struct structure where the members can be manually assigned a certain offset from the beginning of the structure, rather than have the data definitions determine at what offsets the labels/members will be. For example, instead of having...

struct SomeStruct
Var1 dd ?
Var2 dd ?
dd 30 dup ? ;(I'm assuming this is valid as example)
Var3 dd ?
ends

... to have Var3 start at offset +38, you could have something like...

struct SomeStruct
Var1 dd ?
Var2 dd ?
Var3 dd ? AT (SomeStruct+3Cool
ends

... and this would automatically put padding between the last var and the defined one, or just make the Var3 a label offset-ed from the struct start that would behave like a struct member. I know this syntax doesn't work, but you get the idea. You see, the problem is that I only know some of the struct members at certain locations, and I often "discover" more members and update the source often. But if I have to fill in the gaps with padding bytes, if forces me to calculate how many everytime I update a struct.

Also, I depend on the struct [ebx+SomeStruct.VarX] indexing, so I would rather stick with struct rather than struc or virtual at if possible.

Thanks
Post 10 Jan 2006, 14:54
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 10 Jan 2006, 15:26
For special purposes like this, the fasm's actual STRUC directive is better (because more flexible). With STRUC you've got many options how to do it, like:
Code:
struc SomeStruct
{
 .Var1 dd ?
 .Var2 dd ?
 dd 30 dup ?
 .Var3 dd ?
}    

or
Code:
struc SomeStruct
{ . dd 33 dup ?
  label .Var1 at .+0
  label .Var2 at .+4
  label .Var3 at .+128 }    
Post 10 Jan 2006, 15:26
View user's profile Send private message Visit poster's website Reply with quote
Big Red



Joined: 25 Feb 2005
Posts: 43
Big Red 10 Jan 2006, 16:26
Quote:
struc SomeStruct
{ . dd 33 dup ?
label .Var1 at .+0
label .Var2 at .+4
label .Var3 at .+128 }


That is very appealing indeed, but is there any way I can use struc to address register-based indexes in the fashion [ebx+SomeStruct.VarX] without having to define a actual structure in memory (as the struct definition does)? If there was a way to do this, I would "convert" to struc pretty quickly.

Thank you very much

EDIT: For example, if I define a struc structure as above like this (omitting the helper macros I would obviously have to implement to make it practical)...

struc SomeStruct
{ . dd 33 dup ?
label .Var1 at .+0
SomeStruc.Var1 equ 0
label .Var2 at .+4
SomeStruc.Var2 equ 4
label .Var3 at .+128
SomeStruc.Var3 equ 128
}

... then when I try to assemble an instruction such as mov eax,[ebx+SomeStruct.VarX], it will use the offset value from equ definition. Would this work, or would it cause compilation problems in some circumstances? Is there a better way?
Post 10 Jan 2006, 16:26
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 10 Jan 2006, 16:43
No, no, the STRUC needs to have an instance declared first, before you can use it. You can do it even this way:
Code:
struc SomeStruct
{
 .Var1 dd ?
 .Var2 dd ?
 dd 30 dup ?
 .Var3 dd ?
}

virtual at ebx
 SomeAtEbx SomeStruct ; define instance at EBX address
end virtual    

and then:
Code:
mov eax,[SomeAtEbx.Var2] ; assembles to mov eax,[ebx+4]    


If you prefer to have the offsets only, you can do it like:
Code:
virtual at 0
 SomeStruct SomeStruct ; define instance with the same name
end virtual    

and then:
Code:
mov eax,[ebx+SomeStruct.Var2]    

(this is what "struct" macro does internally). Choose the one you like more.
Post 10 Jan 2006, 16:43
View user's profile Send private message Visit poster's website Reply with quote
Big Red



Joined: 25 Feb 2005
Posts: 43
Big Red 10 Jan 2006, 16:58
Quote:
virtual at 0
SomeStruct SomeStruct ; define instance with the same name
end virtual [...] (this is what "struct" macro does internally). Choose the one you like more.


Ah, thank you, that is exactly what I needed and needed to hear.

Quote:
No, no, the STRUC needs to have an instance declared first, before you can use it. You can do it even this way.


... sounds like you have to say that often. Maybe something to put in the documentation? ;)

Thank you for the quick reply. Problem solved.
Post 10 Jan 2006, 16:58
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.