Reputation: 29
I'm new to assembly lang and I'm a bit confused on some things. For an assignment, I'm given a C program and asked to asked to put breakpoints at two points and count the stack frames and their locations. This is the code :
int swap_n_add(int *xp, int *yp) {
int x = *xp;
int y = *yp;
*xp = y;
*yp = x;
int sum = x+y;
return sum;
}
int main(int argc, char **argv) {
int a1 = 534;
int a2 = 1057;
int sum = swap_n_add(&a1, &a2);
int diff = a1-a2;
return sum * diff;
}
So I'm asked to use gdb to examine the stack frames and list their location At the first breakpoint, there's two stack frames .From doing info frame 0: I get the following:
Stack frame at 0x7fffffffe240:
rip = 0x4004f9 in swap_n_add (swap-n-add.c:7); saved rip 0x40055d
called by frame at 0x7fffffffe270
source language c.
Arglist at 0x7fffffffe230, args: xp=0x7fffffffe254, yp=0x7fffffffe250
Locals at 0x7fffffffe230, Previous frame's sp is 0x7fffffffe240
Saved registers:
rbp at 0x7fffffffe230, rip at 0x7fffffffe238
My question is, is rbp is the location or rip?
Another question is, "What are the values stored in rbp and rsp? What does it mean for the stack frame?"
I'm not sure how to answer this because from my understanding, rbp is a stack pointer and changes every time there's a new operation. I might be wrong, but I just don't know how to answer that.
Sorry for the long question, I'm just trying to get my fundamentals right
Upvotes: 1
Views: 3277
Reputation: 185
When you call a function with the call instruction the rip pointer is pushed on the stack. Nothing else happens.
The callee function if it is setting up a full stack frame pushes the value of the rbp (the base value of the last frame) the rbp is set to the rsp which becomes the new frame's base value. The rsp changes any time something is pushes or popped from the stack.
So this is basically the layout.
previous frame base
<Any automatics variables>
<Any passed variables to the current function passed the first 6th>
return address
new frame base
<Any automatics>
rsp
When the function returns the stack pointer has to be pointing to the address on the stack - 8 from the return address or in otherwords the ret call is just pop of the stack pushing the value into the instruction pointer.
Upvotes: 4