tags:

views:

51

answers:

2

Hello. I would like to know if after calling functions the data I have in the stack is persistent. Like, I would like to know if (assuming cdecl convention) can I do this (independently of what is done in the function X and independently of optimizations):

push 1
push 2
push 3
call X
call X
call X
add 12 esp

?

Also, let's say that before the calls I save the address of where the pushed values are in a global variable. Can I, inside X, alter the values it contain by acessing the global variable? Like, for some reason I want that in X I'm able to alter the values in stack so that the second and third call to X receive different values.

+1  A: 

In cdecl, the caller clears the stack after the callee returned. Return values are placed in EAX, so I think that it's safe to assume that the caller can clear the stack the way you describe - otherwise, how would cdecl work in practice?

And yes, X can alter the contents of the stack if it wants to. In assembly, everything is open, really, there are very few restrictions.

Eli Bendersky
A: 

Yes, you can do that with i386 cdecl (with x64 these args would be in registers.) But why? Wouldn't it be simpler (and faster) to do a single call and loop within the function? You'd save on branching.

Nikolai N Fetissov