Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Variables offset from stack base pointer

Tags:

c

x86

assembly

I am trying to learn more about the stack and base pointer. The following sample assembly code is from an objdump of a binary compiled by gcc on an IA32.

08048e0b <func_3>:
8048e0b:    55                      push   %ebp
8048e0c:    89 e5                   mov    %esp,%ebp
8048e0e:    83 ec 28                sub    $0x28,%esp
8048e11:    8d 45 f0                lea    -0x10(%ebp),%eax
8048e14:    89 44 24 0c             mov    %eax,0xc(%esp)
8048e18:    8d 45 f4                lea    -0xc(%ebp),%eax
8048e1b:    89 44 24 08             mov    %eax,0x8(%esp)
8048e1f:    c7 44 24 04 65 9b 04    movl   $0x8049b65,0x4(%esp)

I know that the base pointer %ebp is used to reference the function parameters and local variables. Normally the positive offsets are parameters passed to the function and the negative offsets are local variables?

On the line 8048e18: 8d 45 f4 lea -0xc(%ebp),%eax What is -0xc(%ebp) referring to?

like image 486
no0neknow Avatar asked Jan 26 '13 08:01

no0neknow


People also ask

Are local variables allocated on the stack?

Local variables of intrinsic types such as int are created on a portion of memory known as the stack. The stack is allocated and de-allocated as methods are invoked. When you start a method, all its local variables are created on the stack.

Why base pointer value is stored in the stack?

So that the stack pointer can be incremented to accomodate the new stack frame. When it is time to return, the base pointer is popped into the stack pointer to restore the old value.

What is the difference between stack pointer and frame pointer?

The compiler passes parameters and return variables in a block of memory known as a frame. The frame is also used to allocate local variables. The stack elements are frames. A stack pointer (sp) defines the end of the current frame, while a frame pointer (fp) defines the end of the last frame.

Is RSP the stack pointer?

On x86, the stack pointer is stored in the register called "rsp" (Register: Stack Pointer). Conceptually, the stack is divided into two areas: high addresses are all in use and reserved (you can't change these values!), and lower addresses that are unused (free or scratch space).


1 Answers

The arguments to the function are based in (%ebp) + (positive value) in your case you have 1 arguments.

and (%ebp) - (positive value) are local variables and you have 2 in your case.

see the following image:

enter image description here

You may read about calling convention as well.

like image 187
0x90 Avatar answered Oct 07 '22 08:10

0x90