flat assembler
Message board for the users of flat assembler.

Index > Main > struc offsets

Author
Thread Post new topic Reply to topic
lazer1



Joined: 24 Jan 2006
Posts: 185
lazer1
if I have a struc eg

Code:
struc xyz x,y,z
   {
    .x db x+0
    .y dw y+0
    .z dd z+0
   }
    


what's the best way to obtain the offset of say .z? Surprised

here I think it will be 3,

at the moment the only way I can see is to
define a dummy variable, eg if ebx contains a pointer
to a struc xyz and I want to read field .z to eax I do:

Code:
t   xyz 

offset_xyz_z  equ  (t.z-t) ; Sad 

    mov eax,[ebx+offset_xyz_z]
    
    


here I needed to set up a dummy variable t
in order to define offset_xyz_z

what I'm looking for is a way to do this
without requiring a dummy variable, Cool

and also the more general question of how
you access fields of a pointer to a struc,

I need this eg for dynamically allocated strucs Mad

or eg with query code where I'm querying just 1 field
of a struc,
Post 17 Mar 2006, 14:31
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
For the structures with fixed size fields (and I guess you don't many any other), you can do it with the quite old trick:
Code:
virtual at 0
 xyz xyz ; defines offsets like xyx.x, xyz.z etc.
end virtual    

and then do things like:
Code:
mov eax,[ebx+xyz.z]    


However I often recommended the other method, like declaring the dedicated virtual instance of structure for your appliance:
Code:
virtual at ebx
 myxyz xyz
end virtual

mov eax,[myxyz.x] ; generates the mov eax,[ebx+0] instruction    
Post 17 Mar 2006, 15:39
View user's profile Send private message Visit poster's website Reply with quote
lazer1



Joined: 24 Jan 2006
Posts: 185
lazer1
thanks for the explanation! Razz

both ideas will be useful for me,

sometimes I need just the offsets where the first suggestion
will be useful and sometimes I need to use
struc fields where the second suggestion is what I need,

I think I will use the second suggestion like this:

Code:
virtual at ebx
    ebx_xyz xyz
end virtual
    


that way whenever I want ebx to be a pointer to a struc
ebx I can use it as ebx_xyz,

I'll name it like this so I dont forget its via ebx,

question on this: will ebx_xyz then be the same as ebx?

eg is the following code:

Code:
   mov eax,ebx_xyz
    


the same as:

Code:
   mov eax,ebx
    


? Rolling Eyes
Post 17 Mar 2006, 18:19
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
No, not really.
Funny that this question was already asked today: http://board.flatassembler.net/topic.php?t=4959
Post 17 Mar 2006, 18:26
View user's profile Send private message Visit poster's website Reply with quote
Patrick_



Joined: 11 Mar 2006
Posts: 53
Location: 127.0.0.1
Patrick_
I have a (somewhat) similar question, so I thought I would ask it here.

I have a structure declared like so:

Code:
struc dirent {
   .d_ino: rd 1
   ...
}    


However, I'm not sure how I would put the address of the structure (so I can, say, pass it as a system call argument) into a register. How would I do this?
Post 18 Mar 2006, 01:41
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
Patrick_ wrote:
I have a (somewhat) similar question, so I thought I would ask it here.

I have a structure declared like so:

Code:
struc dirent {
   .d_ino: rd 1
   ...
}    


However, I'm not sure how I would put the address of the structure (so I can, say, pass it as a system call argument) into a register. How would I do this?


address of the first variable would be the same as the address of the structure Smile

there is probably some trick but i would just build it manually:

Code:
dirent:
    .d_ino rd 1
size.dirent = $-dirent
    


you could pass 'dirent' or 'dirent.d_ino' (no brackets) as the address

actually come to think of it, you can do the same with 'struc'

Code:
struc abc {
    .wutang dd ?
}

hi abc
.....

mov eax, hi
    


that should also be fine, since 'abc' wont actually be anywhere in your code unless you declare a structure like 'hi' as it

_________________
redghost.ca
Post 18 Mar 2006, 01:52
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Patrick_



Joined: 11 Mar 2006
Posts: 53
Location: 127.0.0.1
Patrick_
Thank you! This works. Smile
Post 19 Mar 2006, 14:55
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.

Powered by rwasa.