What Happens During Function Epilogue Phase?

Question: What Happens During Function Epilogue Phase?

Answer: As we know that called function has three components viz., function prologue, function body and lastly function epilogue. Function prologue begins with creating space for local variables, saved registers and for parameters being passed to a procedure. Function body is where useful work is performed. When function completes execution and finishes, function epilogue begins by restoring values of saved registers, previous frame pointer and restores the stack pointer before function returns to where it’s called from.

As we have become familiar that ‘x86-64 bit’ architecture doesn’t have frame pointer and all locations on the stack frame are accessed relative to stack pointer. Moreover, it allows max 6 parameters being passed to a procedure to be stored via registers and therefore 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.

Therefore, in an instance where stack frame was created, only useful work that is done during the epilogue phase is to restore ‘callee save registers’ if, any, modified, restoring the stack pointer and popping off of the return address. Recall that in x86-64 bit implementation, a ‘callee save’ register is one whose contents must be copied onto stack before the same is used to hold temporaries and must be restored when function is done and about to return. On the other hand, when there’s no stack frame created, epilogue pops off return address which was pushed onto stack by calling function and where called function would return after having finished. Control is then transferred to calling program.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

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.