I have a function that takes an optional parameter. If it is omitted, I need to get a valid, default version of that parameter. Can I overwrite the parameter passed in without changing the caller's data? Or should I create a LOCAL and mov the value in and then fix it up if needed?

MyFunc proc pThing:DWORD

; pThing is optional. If it's NULL, fill it with
; the default Thing obtained from GetThing()
mov eax, pThing
test eax, eax
jnz @F
invoke GetThing ; returns default pThing in eax
mov pThing, eax

; ... continue with known-valid pThing

My question is, pThing is an *address* that was passed by *value* on the stack, right? So "mov pThing, eax" won't overwrite the caller's value? I'm pretty sure this is correct, but wanted to get it straight before I commit it to a bazillion lines of code (well, okay, more like 30. But for me, that's still well over my what-was-I-doing threshold for assembly).


Posted on 2002-04-24 15:45:55 by Chalain

If I have your question right, pThing is just a normal parameter passed on the stack which in this instance would be . I cannot see any problem with overwriting this value with whatever you wish as long as you don't try and write a larger piece of data to that address if there were other parameters above it.


Posted on 2002-04-24 16:25:55 by hutch--
Hutch is right, when the function is called, each parameter is pushed on the stack to be used by the called function. The only thing you change is a piece of the stack.

In some cases, even if you want to, you can't change the caller's data, for example:

invoke MyFunc, 100

In this case there is no caller's data, just a value that is passed to the function. The 100 only exists in the stack.

Posted on 2002-04-24 16:36:47 by Thomas
Perfect! Thank you. That was my question exactly.

I was pretty sure that's how it worked, but assembly keeps biting me just when I think I have it sussed... :eek:


Posted on 2002-04-24 16:59:46 by Chalain
if you an optional parameter is omitted, feel free to change the value. It is up to the caller to ensure they use the correct method of passing parameters, so the function that takes the optional parameter should have no fear in how it mangles that parameter.

Also, in your example, 'pThing' is not necessarily a pointer, it is only a pointer if specifically built the function to use it as a pointer.
Posted on 2002-04-25 06:50:47 by sluggy