Processor Stack, Stack Frame, and Frame Pointer
Stack Frame :
Stack is one of the segments of application memory that is used to store the local variables, function calls of the function. Whenever there is a function call in our program the memory to the local variables and other function calls or subroutines get stored in the stack frame. Each function gets its own stack frame in the stack segment of the application’s memory.
Features :
- The memory allocated for a function call in the stack lives only the time when the function is executing once the function gets completed we can’t access the variables of that function.
- Once the calling function completes its execution its stack frame is removed and the thread of execution of called function resumes from that position where it was left.
- Stack is used for storing function calls so in the case when we are using a lot of recursive calls in our program the stack memory gets exhausted by the function calls or subroutines which may result in stack overflow because the stack memory is limited.
- Each stack frame maintains the Stack Pointer (SP), and the frame pointer (FP). Stack pointer and frame pointer always point to the top of the stack. It also maintains a program counter (PC) which points to the next instruction to be executed.
- Whenever a function call is made a stack frame is created in the stack segment the arguments that were given by the calling function get some memory in the stack frame of called function, and they get pushed into the stack frame of the called function. When their execution is finished they get popped from the stack frame. And the thread of execution gets continues in the called function.
Structure of stack frame :
Stack pointer always points to the top and frame pointer stores address of whole stack frame of the subroutine. Each stack frame of a subroutine or a function contains as follows.
Stack Frame |
---|
Other function calls |
Other saved registers |
Local variables |
Saved [Frame Pointer] Of called function |
Saved [Link Register] Of called function |
Passed arguments |
Frame pointer (F.P) 1. Subroutine: The content of the PC must be Saved by the call Subroutine Instruction to make a correct return to the calling program. Figure – Process of a subroutine in a program The subroutine linkage method is a way in which computers call and return the Subroutine. The simplest way of Subroutine linkage is saving the return address in a specific location, such as a register which can be called a link register called Subroutine. 2. Subroutine Nesting : Subroutine nesting is a common Programming practice In which one Subroutine calls another Subroutine. Figure – Subroutine calling another subroutine From the above figure, assume that when Subroutine 1 calls Subroutine 2 the return address of Subroutine 2 should be saved somewhere. So if the link register stores the return address of Subroutine 1 this will be (destroyed/overwritten) by the return address of Subroutine 2. As the last Subroutine called is the first one to be returned ( Last in first out format). So stack data structure is the most efficient way to store the return addresses of the Subroutines. Figure – Return address of subroutine is stored in stack memory 3. Stack memory: Stack is a basic data structure that can be implemented anywhere in the memory. It can be used to store variables that may be required afterward in the program Execution. In a stack, the first data put will be the last to get out of a stack. So the last data added will be the first one to come out of the stack (last in first out). Figure – Stack memory having data A, B & C So from the diagram above first A is added then B & C. While removing first C is Removed then B & A. \Register While the subroutine is active, the frame pointer, points at the top of the stack. (Remember, our stacks grow downward, so in the picture But the stack (and the stack pointer) may be involved in arithmetic expression evaluation. This often involves pushing and popping values onto and off of the stack. If To make things easy for compilers (and for human assembly language programmers) it is convenient to have a frame pointer that does not change its value while a subroutine is active. The variables will always be the same distance from the unchanging frame pointer. In the subroutine prolog, the caller's frame pointer is pushed onto the stack along with the stack pointer and any Tirthankar Pal
|
No comments:
Post a Comment