After a function A calls a function B, can the code in B trash all the registers (aside from those that hold the stack pointers and B’s parameters) without affecting variables local to A? Accordingly, after function B returns to function A, does function A pop all its locals back off the stack (reasoning that the register states might have changed while function B was executed)?
What about global variables? Does function B need to worry at all about any register operations affecting the state of global variables?
(The main reason I ask this, is that I feel like experimenting with injecting machine code at runtime as function B by using mprotect to make an array executable, and then casting the array pointer to function pointer and calling it. With the above questions I hope to figure out what the extent of B’s playground is.)
This is
calling convention, which is architecture, operating system, and compiler dependent.Edit 0:
One more link for you:
application binary interface. Drill down for your particular hardware/OS/compiler combination. You’ll find what registers are used for parameters/return values, which are reserved for specific things, and which are free for any given function to clobber.