Friday, September 5, 2025
HomeData Modelling & AIHow is return address specified in Stack?

How is return address specified in Stack?

Stack:

A stack is a linear data structure in which elements are accessed with one end called the “top” of the stack. It follows the LIFO (Last In First Out) approach.

Program Counter:

The program counter is a special purpose register in the CPU which stores the memory location of the next instruction.

Note: The article uses the abbreviated form “PC” in place of Program Counter.

Stack Pointer:

A stack pointer is a special register in the CPU which is used to store the address of the top element of the stack.

When and how the return address is specified in the Stack?

  • The return address is specified in the stack when a program contains a function call or subroutine.
  • When the function is called then, after its complete execution it has to return back to the original program i.e., the next instruction after the function call in the program.
  • When the function call is to be executed then, the program counter PC holds the address of the next instruction after the function call in the program.
  • For executing the function, the control has to go to the function definition and after executing the function it has to return to the next instruction after the function call in the program.
  • So, when the function is to be executed then, the PC value i.e., the next instruction address is stored onto the stack (SP incremented) and the PC is updated by the address where the function is stored.
  • After the complete execution of the function, the PC gets updated by the return address present in the stack (value at the top of the stack).

Example:

Assume I3 is the function call instruction and CPU is currently executing I3 and PC = 304

  • The PC value 304 is stored onto the stack and stack pointer SP is incremented by one. 
  • The PC value is updated by 400 i.e., the address where the function is stored. 
  • After complete execution of the function PC value is again updated by stack value (top of stack) and stack pointer SP is decremented by 1.

Follow the below image for a better idea

Example showing storing of return address

Example showing storing of return address

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS