For number 2
this depends on many things including the compiler and the platform. Basically the different ways of passing and returning arguments to functions are called calling conventions. the article Calling conventions on the x86 platform goes into some detail on sequence of operations and you can see how ugly and complicated it gets with just this small combination of platforms and compilers which is most likely why you have heard all sorts of different scenarios, The gen on function calling conventions. covers a wider set of scenarios including 64 bit
platforms but is harder to read. It gets even more complicated because gcc
may not actually push
and pop
the stack but directly manipulate the stack pointer, we can see an example of this, albeit in assembly here. It is hard to generalize about calling conventions, if the number of arguments is small enough many calling conventions can avoid using the stack
at all and will use registers
exclusively.
As to number 3
, nested functions does not change anything, it will just repeat the procedure over again for the next function call.
As to number 1
As Sean pointed out .Net
compiles to byte code with performs all it's operations on the stack. The Wikipedia page on Common Intermediate Language has a good example.
The x86-64 ABI
document is another great document if you want to understand how one specific calling convention works in detail. Figure 3.5 and 3.6
are neat since they give a nice example of a function with many parameters and how each parameter is passed using a combination of general purpose registers
, floating point registers
and the stack
. This sort of nice diagram is a rare find when looking at documents that cover calling conventions.