I am interested in what happens when executing the call instruction of the assembly code Visual C++ is compiled to.
What I think happens is that the call instruction itself pushes all context onto the stack (register content, PC, ...) and updates the PC. But then again, I wonder whether all context is pushed or not. In many cases, not all registers are used within the function called. Do the compiler detect this and only push the context that is modified by the function, or is pushing all context implemented so cheaply at the hardware level that it is always done?
This all depends on the ABI (Application Binary Interface) which dictates whether the caller or the callee pushes things onto the stack. Since you didn't specify a platform (although Visual Studio sort of implies x86), the answer depends.
In general, the caller can't know what the called function will do, so the compiler can't optimize based on information inside the function.
Usually, pushing context on to the stack is not performed automatically on any platform, but several platforms have macros and optimized instructions to do it quickly. Usually the only thing that is automatically pushed and popped from the stack is the PC.
Edit: for x86, there are several ABIs. You should check out this Wiki reference to see them all (cdecl, stdcall, etc). Each has different rules about whether the caller or callee saves the context.
The instruction CALL does not pushes the registers in the stack. It pushes only the address where the function should return.
The INT instruction (that acts similar to call) pushes also the content of the FLAGS register.
If the program needs some other registers to be preserved during the function call, it needs to store these registers in the stack by explicit push instructions and then to restore them by pop instructions.
What to be stored and exactly how, when we are talking about high level languages, depends only on the compiler. The compilers are free to use any convention, but usually they stick to the standard convention in order to provide ability to link modules written in different languages.
In assembly programming the preserving strategy is entirely up to the programmer. He can use some of the standard conventions or custom conventions depending on his programming goals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With