Friday, 20 March 2015

Function-call stack frame

Function-call stack frame:

The arguments are stored on the stack when the compiler generates the code for a function and then only it will call the function. The code is generated to move the stack pointer down even farther to provide storage for the function’s local variables.

But during the assembly-language CALL, the CPU pushes the address in the program code where the function call came from, so the assembly language RETURN can use that address to return to the calling point. This address is of course sacred, because without it your program will get completely lost. Here’s what the stack frame looks like after the CALL and the allocation of local variable storage in the function.

Let us see the pictorial representation of the storage


The code generated for the rest of the function expects the memory to be laid out exactly this way, so that it can carefully pick from the function arguments and local variables without touching the return address. I shall call this block of memory, which is everything used by a function in the process of the function call, the function frame.

You might think it reasonable to try to return values on the stack.

The compiler could simply push it, and the function could return an offset to indicate how far down in the stack the return value begins.

Limitations of the Register storage classes:
It is not possible to get the address of the variables that are reserved in the register storage using “&”
As we know the CPU registers are limited numbers. So we can not store more variables as register variables.

It is not the good practice that register variables as a global ones. Because the expectation of the global variable is throughout the program

1 comment:

ideas said...

Awesome explanations on various cpp things