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.
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.
- Practice BCA MCQs
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Check C Books