I just want to know if there is some way to pass a pointer to a structure to a proc and then inside the proc reference the members of the structure by a dot ".", or do i have to set the members at "low level"?. I mean this:

--- BEGIN CODE ---

.DATA?

MyStrc STRUCT
Var1 dword ?
Var2 dword ?
MyStrc ENDS

.CODE
start:

invoke Process_Struct, addr MyStrc

Process_Struct proc StrcPtr:DWORD

mov StrcPtr.Var1, 3ah <---- This is illegar in masm
mov .Var1, 3ah <---- This too

ret

Process_Struct endp

end start

--- END CODE ---

I have read in masm help the struct topic and it says that it can be a pointer to a struct but i cant figure it out... can somebody help???
Posted on 2002-12-12 11:24:43 by IrOn
Iron first thing you must remember the x86 do not allow indirect addressing from memory , only from registers...
so in Process_Struct you need to put the structure pointer into a register.

after that you can access the structure members by one of two ways:

mov eax,StrcPtr
assume eax:ptr MyStrc
then you can do :

mov .Var1, 3ah
once you're finished with eax you should
assume eax:nothing

the other way which I prefer
mov eax,StrcPtr
mov (MyStrc ptr).Var1, 3ah
Posted on 2002-12-12 11:40:18 by MArtial_Code
Thank you very much!! now i can finish my funtion!!!:alright:
Posted on 2002-12-12 12:25:24 by IrOn
Oops.. I think MArtial Code made a mistake.

should be


lea eax,StrcPtr
assume eax:ptr MyStrc
mov [eax].Var1, 3ah
....
assume eax:nothing


or



lea eax,StrcPtr
mov (MyStrc ptr[eax]).Var1, 3ah
Posted on 2002-12-12 20:13:49 by roticv
IrOn,



mov StrcPtr.Var1, 3ah <---- This is illegar in masm
mov [StcPtr].Var1, 3ah <---- This too


The problem here is that you are not specifying the size of an immediate operand and the assembler cannot work it out from the memory operand which the structure member is.


mov DWORD PTR StrcPtr.Var1, 3ah

Specify the data size and it builds OK.

Regards,

hutch@movsd.com
Posted on 2002-12-12 21:39:57 by hutch--
Since Var1 is a first member in your struct you can use the following:


Process_Struct proc StrcPtr:DWORD
mov eax,StrcPtr
mov [eax], 3Ah //var1 = 3Ah
mov [eax+4], 3Bh //var2 = 3Bh
ret
Process_Struct endp
Posted on 2002-12-13 03:27:46 by Alexey
Alexey,

While your code will work, you really shouldn't do things like that. The point of having STRUCTs is to make things easier. If the layout of the STRUCT changes, you'ld need to find and change ALL the "hard coded" references to it. This could be a considerable effort if the STRUCT is used in many places, and/or if it contains several members. When you use the names, the assembler will make any adjustments for you.

:)
Posted on 2002-12-13 06:01:14 by S/390
I was responding to the fact that he has a local pointer to the structure.

Roticv--your way won't work because lea eax,StrcPtr loads the address of StrctPtr into eax when infact we want the value which it contains i.e. the pointer to the structure (StrcPtr is only a local dword)

Hutch: Your method won't work either...for one thing StrcPtr is not a structure but a structure pointer(at best he'll get crazy results)
Also masm takes the structure member size from the structure definition

His problem was/is one of indirect memory addressing from memory variable...

Now that I've taken a closer look there're quite a few things wrong with the snippet which iron posted...
Assuming he meant Mystrc to be a global structure variable and not a structure definition...
he could have done away with the explicit pointer and simply used mov MyStrc.Var1,3AH

:tongue:
Posted on 2002-12-13 08:24:52 by MArtial_Code
oh my mistake... did not realise that the Strptr is a pointer to a structure :grin:
Posted on 2002-12-13 09:17:51 by roticv
My problem is/was this:

To set a scrollbar we need to fill the some structure defined as SCROLLINFO. This structure would be global in the code. I just wanted to make a piece of code (function) to recive the parameters then it would take care of "filling" and then calling the api and the other code in the main program could acces the members of the structure cos it need it and also another function in the program could acces the structure param. I wanted that this code could be inserted in another program without making any change to the function, so the user would have only to create a global structure and pass a pointer and other things for setting a scrollbar. Thats the reason why i dont use directly the structure and pass a pointer, but its all ok, now i create a SCROLLINFO local variable in the function and then call to GetScrollInfo, so i can retrive the info and then set the scrollbar using SetScrollInfo. The unique disadvantage of this is that main program (caller) cant acces to scroll info structure directly.

Although i have solved my problem i still wonder why masm dont have an option for doing a thing like this.
Posted on 2002-12-13 12:54:15 by IrOn
Hutch,

I never use DWORD PTR when i do such things... for one reason.

The assembler already knows the data width of the structure member!

mov .MyStruct.Var3, 21h

Will work by virture fo the fact that structures are defined by the amound of space each field will occupy.. it cant offset from EAX to Var3 without knowing the number of bytes Var1 and Var2 used. To do this it looks at the data types of Var1 and Var2. Hence, it knows what Var3 is as well cause it has a 'menu' to work with.

mov , 21h

However, will not for obvious reasons. (no "memu") ;)

I added to the FAQ section some stuff i had on my old web pages that discusses structures with a fairly good degree of detail...

If there is a person still reading this thread and confused, check it out..
Stuctures 101

:alright:
NaN
Posted on 2002-12-13 19:14:37 by NaN