flat assembler
Message board for the users of flat assembler.

Index > Windows > Pointers on Structs

Author
Thread Post new topic Reply to topic
denial



Joined: 12 Sep 2004
Posts: 98
denial
Hello,

is it possible to access a FASM struct through a pointer?
Like in this Pseudo-C code:

Code:
struct PERSON
{
   char name[100];
   int age;
};

...
PERSON myself;
PERSON* spointer;

spointer=&myself;
...
spointer->age=1000;
    


FASM compiled successful when getting the pointer to a structure through LEA. But now, I don't know how to access the structures elements through the pointer like in the line above spointer->age=1000;

Thank you
Post 08 Mar 2005, 13:21
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Well, if you have some variable in static memory, you can use it this way:
Code:
struct PERSON
   .name rb 100
   .age  dd ?
ends

SomeVar  PERSON

    mov   [SomeVar.age], 1000

; if you have pointer to the variable of the type PERSON, let say obtained 
; on dynamic memory allocation or some calculations and this pointer is 
; stored in register (edx for example):

    mov  [edx+PERSON.age], 1000   ; will do the job

; if the pointer is stored in some variable, you need to load it into some 
; register (C will do the same, only hidding it from you)

    mov   eax, [ptrMyself]   
; now eax points to some structure of PERSON type.

    mov   [eax+PERSON.age], 1000  ; so use it...
    


I hope this helps you.

Regards.
Post 08 Mar 2005, 13:55
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial
Thank you, it really helped me. Smile
Post 08 Mar 2005, 18:46
View user's profile Send private message Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial
Sorry for double-posting, however I've got yet another problem. Your method works well with DWORDS, but with strings, I got a problem because I don't know how to pass the address to the function wsprintf

Code:
mov ebx,[_SomeVar] ;my var
mov ebx,[ebx+PERSON.name]
cinvoke wsprintf,ebx,'%s',"Somebody "
    


This doesn't work. I tried it with LEA. Then it compiled but nothing happened.
Can you help me out please?
Post 08 Mar 2005, 19:47
View user's profile Send private message Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial
Can nobody help? :O(
Post 10 Mar 2005, 11:56
View user's profile Send private message Reply with quote
rea



Joined: 14 Nov 2004
Posts: 92
rea
Supose taht you have something like....
Code:
struc x
.resd e1
.resd e2
.resd e3
endstruc
    


that define a model for access memory.

that model say that at displacement 0 you will "interprett" like e1, displacement at 4 from the start of the memory you will interpretit like e2, in fact this displacements are defined as follows....
Code:
x.e1 = 0
x.e2 = 4
x.e3 = 8
    

Now supose that you whant to access at memory m with this modelation of memory, m is located at 0x100, this model wil let you access like this:
Code:
m+x.e1 = 0x100+0x0  = 0x100
m+x.e2 = 0x100+0x4  = 0x104
m+x.e3 = 0x100+0x8  = 0x108
    

each location in memory have something.

Before I explain the next, for what lea and mov act in the same way with any memory location that I have put in data, code or bss see that Im not including the ones at heap and stack, because the first group have fixed locations and can be calculated by the assembler, and because is a inmediate number, will be loaded at the respective displacement taking the respective segment (in plain memory they ahve the same value), that is you can use mov eax, memory and because memory is ixed (or a single number.. the calculation of such number is left to the assembler) and is for that lea work exactly the same way with this memory locations.

But you can not move some like mov eax, memory+4, in this case is where you use lea Wink. Now pheraphs you are questionating for what lea uses this sintaxis: lea eax, [memory...], because you are saying: load effective address in eax of this content in memory, that is lea eax, [memory], because [] indicate the content of such memory.

The second group the stack and the heap can not be calculated by the assembler.

The allocation functions return a sngle number that can be moved with mov.

The stack is a combination of a register and a displacement, this is way you can not move the memory location directly, and you se lea for calculate it...



Anyway you can have a humber of a memory location in a register, memory or direct(only posible when the assembler where able to calculate it)



OK, lets continue.

Now m is at 0x100
you can save a memory location of a string like this...

Code:
mov ebx, numberOfMemory
mov [m+x.e3], ebx
    

Where numberOfmemory can be obtained from lea, from a direct number(provided by the assembler) or from a allocation function.

or pass this value previously saved to the function that require the value saved there (that represent a memory location)

mov eax, [m+x.e3]
cinvk wsprintf, eax, "%s", "aName"

and is what you do... the point is that this function si writing the output string at the location pointed by eax.. you havent tried to print such location?? some ike:
invk MessageBoxA, NULL, eax, eax, MB_OK ?????


That should work Wink.. sure if you previously save the memory location in m+x.e3 Wink.
Post 10 Mar 2005, 17:06
View user's profile Send private message Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial
Oh god I'm stupid... ok now it works. I made some string-operations and I made it the right way. But when I tested with MsgBox, I passed the wrong address so the string didn't appear. Ah! Silly Newbie-Mistake ;O)

I'll get better, I promiss. Thank you very much for your detailed help. :O)))
Post 10 Mar 2005, 18:44
View user's profile Send private message Reply with quote
rea



Joined: 14 Nov 2004
Posts: 92
rea
Some times hapend Wink, you know.

Keep in touch Wink.
Post 11 Mar 2005, 14:59
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.