There are some calling conventions (e.g pascal
, stdcall
) but as far as I am concerned, C does use cdecl
(C-declared). Each of these conventions are slightly different in the way the caller loads the parameters onto the stack, respectively which (caller / callee) does the cleanup.
Talking about the cleanup, here is my question. I do not understand: are there three different things?
- stack clean
- moving the pointer back to the penultimate stack frame
- stack restoration
Or how should I see them?
Also, the target of this question is basically how could variadic function works in calling conventions like Pascal or stdcall
where the callee should clear / clean / restore (I don't know which operation) the stack - but he doesn't know how many parameters it will receive.
EDIT
Why is it so important the order in which parameters are pushed on to the stack? You still have the first parameter (stable parameter not from ellipsis) which gives you the information about -for example- the number of variable arguments. And there is also the "guardian" which can be added into ellipsis punctuator and can be used as a marker for the variable part's end independent on the calling convention. In this link why both caller and callee should restore values of those register if they both save their state before messing them up? Shouldn't only one of them (e.g caller) save them on the stack before calling the function and that's all ? Also, on the same link
"So, the stack pointer ESP might go up and down, but the EBP register remains fixed. This is convenient because it means we can always refer to the first argument as [EBP + 8] regardless of how much pushing and popping is done in the function."
The pushed variables and the local variables are consecutive in memory. Where is the advantage of referring them using the EBP ? They will never have some dynamic offset between them, even if the stack changes in size.
One of the materials I've read is this site (only the beginning) for a better understanding on what is exactly the stack frame.
Then I went on yt and found these stack overview and call stack tutorials but they somehow missed the part I needed. What does exactly happends when you call the function (I don't understand the instruction "call address" followed by the next instruction a push
value on to the stack that means the return value). Who controls what the return address will be ? The caller? the callee? When the callee returns, the program contiunes by executing an instruction which is a reading operation from a register or what ?
but he doesn't know how many parameters it will receive
It's completely fine for an ABI to define that compiler needs to pass count of variadic arguments to the function. All these calling conventions are specific to x86, right? – Rubidium