What is a Stack Frame, and How does it Work in the Context of a Function Call?

Question: How Stack Frame is Organised and How Protocols for Calling and Returning from Functions are Interpreted

Answer: Unlike IA32 ISA, ‘x86-64 bit’ architecture implements stack differently. Because of max 6 registers allowed for parameters being passed to a procedure to be strored via registers, some procedures do not need stack at all. At times when arguments being passed to a procedure are more than 6 and/or called routine has its own local arguments, need for stack frame arises. At this point, x86-64 allows any program to access up to 128 bytes of memory (towards lower memory addresses with current value of stack pointer) in stack segment with locations accessible with relative to stack pointer ‘%rsp’. This area of memory is called ‘red zone’ and this is managed by virtual memory management system.

Now, what will happen if local arguments within called function are big sized arrays or structures? How will then demand of increased required amount of memory got to be fulfilled? In such a case, stack frame is created with required amount of memory which is pre-calculated and known at compile time with stack pointer fixed and refferring to top location in the stack frame. All locations in stack frame are accessed with relative to stack pointer. Surprisingly, x86-64 bit doesn’t have the concept for frame pointer.

Recall that by doubling the register set, programs need not be so dependent on the stack for storing and retrieving procedure information. This can greatly reduce the overhead for procedure calls and returns.

Here are some of the highlights of how procedures are implemented with x86-64:

1. Arguments (up to the first six) are passed to procedures via registers, rather than on the stack. This eliminates the overhead of storing and retrieving values on the stack.

advertisement
advertisement

2. The call instruction stores a 64-bit return pointer on the stack.

3. Many functions do not require a stack frame. Only functions that cannot keep all local variables in registers need to allocate space on the stack.

4. Functions can access storage on the stack up to 128 bytes beyond (i.e., at a lower address than) the current value of the stack pointer. This allows some functions to store information on the stack without incrementing or decrementing the stack pointer.

5. There is no frame pointer. Instead, references to stack locations are made relative to the stack pointer. Typical functions allocate their total stack storage needs at the beginning of the call and keep the stack pointer at a fixed position.

6. As with IA32, some registers are designated as callee-save registers. These must be saved and restored.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.