Function Prologue and Epilogue in C

Question4: What do Prologue and Epilogue Terms Stand For in Context with Functions in a C Program

Answer: Every functions has three components, function prologue, function body and function epilogue. Function prologue is part of function does all work needed to start up the function. It begins by reserving space for local variables and other values on the runtime stack. Function body is where all the required work is accomplished and function epilogue cleans up the stack frame just before function is done and returns to the caller. For example, let’s consider a simple C function written below and its assembly code following that,

void pro_epi(long x, long y)
{
    long z;
 
    z = x + y;
}

x86-64 implementation of the above C function follows

1. pro_epi:
2.         .cfi_startproc           # pro_epi() begins here
3.        pushq   %rbp             # callee saved
4.        movq    %rsp, %rbp       # stack-pointer copied to '%rbp'
5.        movq    %rdi, -24(%rbp)  # copying x onto stack
6.        movq    %rsi, -32(%rbp)  # copying y onto stack
7.        movq    -32(%rbp), %rax
8.        movq    -24(%rbp), %rdx
9.        addq    %rdx, %rax       # adding x and y
10.       movq    %rax, -8(%rbp)
11.       popq    %rbp             # restoring the callee save
12.       ret
13.       .cfi_endproc             # pro_epi() ends here

Notice in the assembly code produced for the given C function “pro_epi()”, function prologue comprises of instruction nos. 3, 4, 5 and 6 while epilogue component executes instruction 11. Between prologue and epilogue is where instuctions within the function executed.

advertisement
advertisement

Notice that register ‘%rbp’ is exclusive to callee save meaning that contents of this must be saved onto stack before using it for temporary holding of values. This must be restored before stack deallocates. There are several callee save registers in x86-64 bit implementation and these are, in sequence, %rbp, %bx, %r10, %r13, %r14 and %r15.

Notice also a very interesting feature of x86-64 bit implementation is that it allows upto max of 6 arguments to be passed to procedures via registers. Therefore when arguments are less in no., as in the example C function above, stack frame isn’t needed at all. Further, any program can access upto 128 bytes beyond but towards lower address to the current value of stack-pointer (%rsp) with relative to stack-pointer.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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.