We know that the stack pointer points to the current topmost data item on the stack. But what is purpose of base pointer? What are the differences between stack and base pointer? (meaning of task)
Posted on 2007-02-24 07:18:05 by sawer
Base pointer is just a register, like many else. _BUT_ indirect memory references using (e)bp are shorter by 1 byte. That's why you want to use it as a pointer for, let's say, local data. That is why almost every procedure starts with something like "enter XX,0", or "push ebp; mov ebp, esp; sub ebp, XX".
Posted on 2007-02-24 07:37:34 by ti_mo_n

The basepointer is typically used in combination with the stackpointer.
The memory between them is called a 'stackframe'.
Posted on 2007-02-24 07:48:25 by Homer
I believe a base pointer (ebp) exist so that we can easily access the local variables and parameters passed to the function, else it will be very different trying to figure these values out. It's not impossible, but difficult.
Posted on 2007-02-24 08:54:20 by roticv
Thank you for all answers.
@ti_mo_n

Base pointer is just a register, like many else. _BUT_ indirect memory references using (e)bp are shorter by 1 byte. That's why you want to use it as a pointer for, let's say, local data. That is why almost every procedure starts with something like "enter XX,0", or "push ebp; mov ebp, esp; sub ebp, XX".

CAn you please explain this a bit detail?
@Homer
OS adjust SP automatically when call a procedure. Also adjust bp automatically?
@roticv

I believe a base pointer (ebp) exist so that we can easily access the local variables and parameters passed to the function, else it will be very different trying to figure these values out. It's not impossible, but difficult.

Cant SP do that? Why cant we reach local variables by SP?
Is the difference between SP and BP this?

Thanks again...
Posted on 2007-02-24 09:42:00 by sawer
As I said, using (e)bp take 1 byte less than using (e)sp. That's why it (ebp) has been adopted for this task (pointer to local variables). Lot's of compilers (and even assemblers) create stack frame by initializing ebp appropriately (either using the 'enter' instruction, or the 'push - mov - sub' combination).
Posted on 2007-02-24 11:06:51 by ti_mo_n
I use the EBP register to access parameters of a procedure because if you are inside the first procedure and you already know the offsets to your parameters from the stack pointer and then within the code fo the first procedure you call another procedure, the offsets will be changed by the number of bytes that are pushed onto the stack when the second procedure is called.
Posted on 2007-02-24 14:32:26 by XCHG
indirect memory references using (e)bp are shorter by 1 byte because
we have not use mov eax, ss: (we have not to redefine prefix segment). We just use mov eax,, SS is defualt segment in operations with ebp and esp.

Moreover, operations with ebp are shorter operations with esp

8B 45 10        mov eax,  (if modrm=101(mean that only displacement present) we have to use mod=01 that is 1-byte displacement)
8B 44 24 10    mov eax,  (if modrm=100(mean that sib is present) we have to use mod=01 and index=100)

Posted on 2007-02-24 15:08:52 by Asvald

@roticv

I believe a base pointer (ebp) exist so that we can easily access the local variables and parameters passed to the function, else it will be very different trying to figure these values out. It's not impossible, but difficult.

Cant SP do that? Why cant we reach local variables by SP?
Is the difference between SP and BP this?

Thanks again...


Like I said it is possible. Imagine if you are using the parameters to your function to call yet another function - it will be a mess as all the pushes and pop will screw up the value of esp and you must make the correct adjustment. (Yeah I know because I play with functions without stack frames and some bugs are caused by it)
Posted on 2007-02-24 22:04:06 by roticv