flat assembler
Message board for the users of flat assembler.

Index > Main > How to pass a STRUCT to a PROC?

Author
Thread Post new topic Reply to topic
alorent



Joined: 05 Dec 2005
Posts: 221
alorent 12 Jan 2010, 09:01
Hi!!

I was wondering if it's possible to pass a STRUCT directly to a procedure? (Instead of a pointer to the struct itself)

Example:

Code:
TestStruct struct 

     x1  dd  0
     x2  dd  0

TestStruct ends

MyProc proc param1:TestStruct 

    mov    eax, param1.x1
    mov    ebx, param1.x2
    ret 

MyProc endp    


I have seen that in MASM if you define something like the above and call the function passing a struct, it pushes on the stack each element in the struct. Example:

Code:
v TestStruct <>

invoke MyProc, v     


Will generate the following:

push [v.x2]
push [v.x1]
call MyProc


Not sure if there is equivalent in FASM.

Thanks!!!
Post 12 Jan 2010, 09:01
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 12 Jan 2010, 09:20
Why to pass a whole structure if its very much faster to pass a pointer to it?
Anyway, i dont know if its possible. So lets wait the answer Smile
Post 12 Jan 2010, 09:20
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 12 Jan 2010, 12:19
Teehee,

This is the difference between parameter passing by value and by reference. In the former case callee gets private copy of parameter and changes are not reflected back to passed parameter. In the latter case (when pointer to structure is passed), callee can modify passed parameter.


alorent,

FASM's invoke is a macro, not a directive as in MASM. It's functionality can be extended by yourself. Stock macro simply pushds parameters and indirectly calls given function (i.e. first parameter of invoke is dereferenced to obtain function's address: invoke ebx, v will generate pushd v / call [ebx]). Extended include files (e.g. Win32AX.Inc) define pushd macro to augment invoke functionality, for example, with ability to use nested invokes as parameters.

Probably there is a way to implement something like byval parameter qualifier in pushd (like it's done with addr and double) to pass struct parameter by value, this will involve some kind of type information being attached to it.

Anyway, you can decompose struct into fields and pass them individually (does your function need copy of them all?).
Post 12 Jan 2010, 12:19
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 12 Jan 2010, 14:57
ohh, gotcha!
Post 12 Jan 2010, 14:57
View user's profile Send private message Reply with quote
alorent



Joined: 05 Dec 2005
Posts: 221
alorent 13 Jan 2010, 08:12
Thanks guys!

I guess that's better to pass just the pointer. I was asking to reuse some code from MASM (in external OBJs)

Thanks!!!
Post 13 Jan 2010, 08:12
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.