Search code examples
c++assemblyfunction-calls

Does the assembly function call cause all registers to be pushed onto the stack?


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?


Solution

  • 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.