What is the state of the registers after the function call?

I have limited knowledge of assembly, but I can at least read it and map to the appropriate C or C ++ code. I see that function arguments are passed either by pushing them onto the stack or using registers, and the function body uses some registers to perform its operations. But it also seems to be using the same registers that were used in the caller. Does this mean that the caller does not guarantee that the register state will be the same after the function call? What if the entire composition of a function is unknown at compile time? How does the compiler handle this?

+3


source to share


2 answers


The compiler-generated assembly code follows the calling convention . The calling convention usually indicates

  • how function arguments are passed
  • how return values ​​are passed from the called function to the caller
  • which registers must be stored in the function call and which can be changed


As long as all called functions follow the same calling convention, there is no problem using the same registers.

+3


source


As noted in the comments, the fact is that there is no standard for this. It is entirely left to the developers of the particular C ++ compiler you are using.

A more explicit question, for example, "when compiling on version N of compiler A with compiler options B calling function signature C on target CPU D using ABI E, what are the guarantees against register preservation?"

In this case, an expert (or management) on that particular toolbox can answer.



As you can probably infer that for any industrial strength project, this is the wrong question, because as your compiler evolves, the answer will change and you don't want that fact to affect the reliability of your program.

Good question because it's nice to know what the compiler is doing under the hood - it helps to learn.

But in general, the golden rule is to express clear, uncomplicated logic to the compiler in your program, and let the compiler handle the details of turning that logic into optimized machine code where modern compilers excel.

+2


source







All Articles