Stack Frame in Computer Organization

15 Jun, 2021
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. 
                                                                 A typical stack frame of a subroutine.
Implementation in C++ :Let us consider an example program for better understanding the stack frame.
C++
#include <iostream>using namespace std;  //product methodint findProduct(int a, int b){    int product = a * b;    return product;}  //findSum methodint findSum(int a, int b){    int sum = a + b;    return sum;}  //main functionint main(){    int a = 6;    int b = 6;          //findProduct method called    int product = findProduct(a, b);          //findSum method called    int sum = findSum(a, b);    cout << "Product is :" << product << endl;    cout << "Sum is :" << sum << endl;      return 0;}
Output :
Product is :36
Sum is :12
Explanation using Stack :
Step-1 :When main() calls findProduct function a new stack frame is created as follows.
findProduct()
 a = 6  b=6
product = a* b
                                                                                  Stack 
Step-2 :After function findProduct finishes its instruction and returns to main() and the stack frame for findProduct gets popped from stack. After that main() calls findSum() function.
  main() 
    a = 6      b=6   product = 36   sum = call to findSum()
                                                                                    Stack
Step-3 :After function findSum finishes its instruction and returns to main() the findSum() gets popped from the stack as follows.
                                                                                      Stack
Step-4 :Now main() function executes the remaining statements and gets popped from the stack and the stack gets empty..
Picked
Computer Organization & Architecture
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Direct Access Media (DMA) Controller in Computer Architecture
Architecture of 8085 microprocessor
Pin diagram of 8086 microprocessor
Control Characters
I2C Communication Protocol
Pin diagram of 8085 microprocessor
Difference between SRAM and DRAM
Computer Architecture | Flynn's taxonomy
Computer Organization | Different Instruction Cycles
Difference between RISC and CISC processor | Set 2