When we call a function, the local variables and parameters of the function are stored in a stack. In x86 64-bit architecture or x64, the RBP register contains the base address of the stack. And, the RSP register contains the address of the top of the stack. When we call a function from another function, the steps mentioned below are executed:
1. We push the current value of the RBP register.
2. We move the value of RSP to RBP.
3. We subtract the space needed for the function’s data from the RSP register.
4. We execute the code of the function.
5. We execute the leave instruction. This copies the value of the RBP to RSP.
6. We return from the current function using the ret instruction.
How are function arguments passed in x64?
In x86 64-bit processors or x64, the first six function arguments are passed in the RDI, RSI, RCX, RDX, R8, and R9 registers. If there are more than six arguments, then the rest of the arguments are passed onto the stack.
x64 Stack Frame
Now, let’s try to understand what the x64 stack frame looks like. Let’s write a small piece of code in C.
#include <stdio.h> int func(int a) { int c = a + 1; return c; } int main() { int a = 1, d; d = func(a); return 0; }
![Share on Facebook Facebook](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/facebook.png)
![Share on Twitter twitter](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/twitter.png)
![Share on Reddit reddit](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/reddit.png)
![Pin it with Pinterest pinterest](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/pinterest.png)
![Share on Linkedin linkedin](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/linkedin.png)
![Share by email mail](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/mail.png)
0 Comments