The Wayback Machine - https://web.archive.org/web/20241003135916/https://www.geeksforgeeks.org/stack-frame-in-computer-organization/
Open In App

Stack Frame in Computer Organization

Last Updated : 15 Jun, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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)

                                                                 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 method
int findProduct(int a, int b)
{
    int product = a * b;
    return product;
}
  
//findSum method
int findSum(int a, int b)
{
    int sum = a + b;
    return sum;
}
  
//main function
int 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

 
 
 main()
  a = 6             
  b=6
product = call to findProduct()
sum = call to findSum()

                                                                                  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.

findSum()
a = 6  
b=6
sum = a+ b
 
 

  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.

  main()
   a = 6  
   b=6   
  product = 36 
  sum = 12

                                                                                      Stack

Step-4 :
Now main() function executes the remaining statements and gets popped from the stack and the stack gets empty..



Similar Reads

Memory Stack Organization in Computer Architecture
A stack is a storage device in which the information or item stored last is retrieved first. Basically, a computer system follows a memory stack organization, and here we will look at how it works. A portion of memory is assigned to a stack operation to implement the stack in the CPU. Here the processor register is used as a Stack Pointer (SP). The
4 min read
Differences between Computer Architecture and Computer Organization
It is very important for any person who is interested or has a leaning towards computer science or anybody who has to work with computers to understand the difference between computer architecture and computer organization. What needs to be understood is that although these two concepts are interconnected they are different, one deals with the smal
5 min read
Computer Organization | Basic Computer Instructions
Computer organization refers to the way in which the components of a computer system are organized and interconnected to perform specific tasks. One of the most fundamental aspects of computer organization is the set of basic computer instructions that the system can execute. Basic Computer InstructionsBasic computer instructions are the elementary
7 min read
Performance of Computer in Computer Organization
In computer organization, performance refers to the speed and efficiency at which a computer system can execute tasks and process data. A high-performing computer system is one that can perform tasks quickly and efficiently while minimizing the amount of time and resources required to complete these tasks. The performance of a computer system depen
6 min read
Introduction of Stack based CPU Organization
The computers which use Stack-based CPU Organization are based on a data structure called a stack. The stack is a list of data words. It uses the Last In First Out (LIFO) access method which is the most popular access method in most of the CPU. A register is used to store the address of the topmost element of the stack which is known as Stack point
3 min read
Computer Organization | Amdahl's law and its proof
It is named after computer scientist Gene Amdahl( a computer architect from IBM and Amdahl corporation) and was presented at the AFIPS Spring Joint Computer Conference in 1967. It is also known as Amdahl's argument. It is a formula that gives the theoretical speedup in latency of the execution of a task at a fixed workload that can be expected of a
6 min read
Computer Organization | Micro-Operation
In computer central processing units, micro-operations (also known as micro-ops) are the functional or atomic, operations of a processor. These are low level instructions used in some designs to implement complex machine instructions. They generally perform operations on data stored in one or more registers. They transfer data between registers or
3 min read
Computer Organization | Different Instruction Cycles
Introduction : Prerequisite - Execution, Stages and Throughput Registers Involved In Each Instruction Cycle: Memory address registers(MAR) : It is connected to the address lines of the system bus. It specifies the address in memory for a read or write operation.Memory Buffer Register(MBR) : It is connected to the data lines of the system bus. It co
11 min read
Computer Organization | Asynchronous input output synchronization
Introduction : Asynchronous input/output (I/O) synchronization is a technique used in computer organization to manage the transfer of data between the central processing unit (CPU) and external devices. In asynchronous I/O synchronization, data transfer occurs at an unpredictable rate, with no fixed timing or synchronization between the CPU and ext
7 min read
MPU Communication in Computer Organization
MPU communicates with the outside world with the help of some external devices which are known as Input/Output devices. The MPU accepts the binary data from input devices such as keyboard and analog/digital converters and sends data to output devices such as printers and LEDs. For performing this task, MPU first need to identify the input/output de
4 min read
Purpose of an Interrupt in Computer Organization
Interrupt is the mechanism by which modules like I/O or memory may interrupt the normal processing by CPU. It may be either clicking a mouse, dragging a cursor, printing a document etc the case where interrupt is getting generated. Why we require Interrupt? External devices are comparatively slower than CPU. So if there is no interrupt CPU would wa
2 min read
Peripherals Devices in Computer Organization
Generally peripheral devices, however, are not essential for the computer to perform its basic tasks, they can be thought of as an enhancement to the user's experience. A peripheral device is a device that is connected to a computer system but is not part of the core computer system architecture. Generally, more people use the term peripheral more
4 min read
Data Manipulation Instructions in Computer Organization
Data Manipulation Instructions Data manipulation instructions perform operations on data and provide computational capabilities for the computer. The data manipulation instructions in a typical computer are usually divided into three basic types as follows. Arithmetic instructionsLogical and bit manipulation instructionsShift instructions Let's dis
3 min read
Control Logic Gates in Computer Organization
Control Logic Gates is the hardware component of a basic computer. It comprises some inputs and outputs. The diagram given below is related to the hardwired control organization. The diagram below consists of the instruction register which has three parts: the I bit (15), the operation code (12, 13, 14), and bits 0 through 11. The symbols as D0 to
2 min read
Synchronous Data Transfer in Computer Organization
In Synchronous Data Transfer, the sending and receiving units are enabled with the same clock signal. It is possible between two units when each of them knows the behaviour of the other. The master performs a sequence of instructions for data transfer in a predefined order. All these actions are synchronized with the common clock. The master is des
4 min read
Computer Organization and Architecture | Pipelining | Set 3 (Types and Stalling)
Please see Set 1 for Execution, Stages and Performance (Throughput) and Set 2 for Dependencies and Data Hazard. Types of pipeline Uniform delay pipeline In this type of pipeline, all the stages will take same time to complete an operation. In uniform delay pipeline, Cycle Time (Tp) = Stage Delay If buffers are included between the stages then, Cycl
3 min read
Computer Organization and Architecture | Pipelining | Set 2 (Dependencies and Data Hazard)
Please see Set 1 for Execution, Stages and Performance (Throughput) and Set 3 for Types of Pipeline and Stalling. Dependencies in a pipelined processor There are mainly three types of dependencies possible in a pipelined processor. These are : 1) Structural Dependency 2) Control Dependency 3) Data Dependency These dependencies may introduce stalls
6 min read
Computer Organization | Hardwired v/s Micro-programmed Control Unit
Introduction : In computer architecture, the control unit is responsible for directing the flow of data and instructions within the CPU. There are two main approaches to implementing a control unit: hardwired and micro-programmed. A hardwired control unit is a control unit that uses a fixed set of logic gates and circuits to execute instructions. T
5 min read
BUS Arbitration in Computer Organization
Introduction : In a computer system, multiple devices, such as the CPU, memory, and I/O controllers, are connected to a common communication pathway, known as a bus. In order to transfer data between these devices, they need to have access to the bus. Bus arbitration is the process of resolving conflicts that arise when multiple devices attempt to
7 min read
Cache Memory in Computer Organization
Cache memory is a small, high-speed storage area in a computer. The cache is a smaller and faster memory that stores copies of the data from frequently used main memory locations. There are various independent caches in a CPU, which store instructions and data. The most important use of cache memory is that it is used to reduce the average time to
9 min read
Computer Organization and Architecture | Pipelining | Set 1 (Execution, Stages and Throughput)
Pipelining is a technique used in modern processors to improve performance by executing multiple instructions simultaneously. It breaks down the execution of instructions into several stages, where each stage completes a part of the instruction. These stages can overlap, allowing the processor to work on different instructions at various stages of
9 min read
RISC and CISC in Computer Organization
RISC is the way to make hardware simpler whereas CISC is the single instruction that handles multiple work. In this article, we are going to discuss RISC and CISC in detail as well as the Difference between RISC and CISC, Let's proceed with RISC first. Reduced Instruction Set Architecture (RISC) The main idea behind this is to simplify hardware by
5 min read
Computer Organization | Instruction Formats (Zero, One, Two and Three Address Instruction)
In computer organization, instruction formats refer to the way instructions are encoded and represented in machine language. There are several types of instruction formats, including zero, one, two, and three-address instructions. Each type of instruction format has its own advantages and disadvantages in terms of code size, execution time, and fle
11 min read
Computer Organization | Booth's Algorithm
Booth algorithm gives a procedure for multiplying binary integers in signed 2’s complement representation in efficient way, i.e., less number of additions/subtractions required. It operates on the fact that strings of 0’s in the multiplier require no addition but just shifting and a string of 1’s in the multiplier from bit weight 2^k to weight 2^m
7 min read
Stack machine in Computer Organisation
Instruction formats are classified into different types depending upon the CPU organization. CPU organization is again classified into three types based on internal storage: Stack machine, Accumulator machine, General purpose organization or General register. In this article, we will learn about the stack machine in computer organizations. Stack Ma
2 min read
Bus organization of 8085 microprocessor
Introduction : The bus organization of the 8085 microprocessor is the way in which the microprocessor communicates with other devices in a computer system. The 8085 microprocessor has a 16-bit address bus, an 8-bit data bus, and various control signals that are used to manage data transfer and other operations. The address bus is used to specify th
9 min read
Introduction of Single Accumulator based CPU organization
The computers, present in the early days of computer history, had accumulator-based CPUs. In this type of CPU organization, the accumulator register is used implicitly for processing all instructions of a program and storing the results into the accumulator. The instruction format that is used by this CPU Organisation is the One address field. Due
2 min read
Introduction of General Register based CPU Organization
When we are using multiple general-purpose registers, instead of a single accumulator register, in the CPU Organization then this type of organization is known as General register-based CPU Organization. In this type of organization, the computer uses two or three address fields in their instruction format. Each address field may specify a general
3 min read
2D and 2.5D Memory organization
The internal structure of Memory either RAM or ROM is made up of memory cells that contain a memory bit. A group of 8 bits makes a byte. The memory is in the form of a multidimensional array of rows and columns. In which, each cell stores a bit and a complete row contains a word. A memory simply can be divided into this below form. 2n = N where n i
4 min read
Cache Hits in Memory Organization
The user has a memory machine. It has one layer for data storage and another layer for the cache. The user has stored an array with length N in the first layer. When the CPU needs data, it immediately checks in cache memory whether it has data or not. If data is present it results in CACHE HITS, else CACHE MISS, i.e., data is not in cache memory so
8 min read