Q- How are variables accessed within a frame in the stack?
Ans: Well as there is a pointer known as stack pointer to manage different operations on stack, the same way there is a frame pointer to access different variables with a frame. The stack pointer points to the top of the stack while the frame pointer points to the starting of the topmost frame as shown:
Now when the new function is called, the new frame is added in the stack and the frame pointer of the previous frame is saved in this new frame and the frame pointer now contains the starting address of the new frame as shown:
NOTE: We have represented this frame pointer as part of OTHER REGISTERS.
Similarly if now we have a new function call, then new frame is added in stack and frame pointer of frame ‘abc’ is stored in this new frame and the frame pointer now contains the address of this new function.
How ever when the function returns, the frame address stored in the frame is re-loaded in the Frame Pointer.
Hence Frame Pointer always contains the starting address of the active frame and active frame always contains the frame address of the lower frame.