flat assembler
Message board for the users of flat assembler.

 Index > Main > Purpose of ebp/rbp
Author
Fred

Joined: 22 Oct 2010
Posts: 39
Fred
I still don't get this entirely. :(
I have seen the classic
Code:
```push ebp
mov ebp, esp
sub esp, 4 (or some other number)    ```

, but I'm not sure what the point is. I haven't made any big or advanced asm programs myself, so all I've been doing with ebp is using it as a general purpose register... can someone explain what ebp/rbp usually is used for?
06 Oct 2011, 18:12
AsmGuru62

Joined: 28 Jan 2004
Posts: 1457
AsmGuru62
It used for local variables.
'4' in this case is a size of a local variable(s) - which can be accessed with [ebp+ofs] - in our case 'ofs' = 0.

To access locals ESP also can be used, but then any PUSH/POP will be affecting the 'ofs'. In that case EBP is not used and procedure looks like this:
Code:
```PROC1:
sub esp, 20h  ; <-- allocate room for locals
...
mov [esp+8h], ecx  ; <-- store ECX into local variable
...
;
; Return from procedure
;
ret
```
06 Oct 2011, 18:53
idle

Joined: 06 Jan 2011
Posts: 408
Location: Ukraine
idle
Code:
```proc One p1,p2,p3
[stack] =                                  return point | p1 | p2 | p3

push    ebp
[stack] =                            ebp | return point | p1 | p2 | p3

mov     ebp,esp
ebp = stack

stdcall Two,p1,p2,p3
[stack] = ebp+08 | ebp+12 | ebp+16 | ebp | return point | p1 | p2 | p3
= p1     | p2     | p3     | ebp | return point | p1 | p2 | p3
```

equals
Code:
```One:
[stack] =                                  return point | p1 | p2 | p3
push    dword[esp+3*4]
[stack] =                             p3 | return point | p1 | p2 | p3
push    dword[esp+3*4]
[stack] =                        p2 | p3 | return point | p1 | p2 | p3
push    dword[esp+3*4]
[stack] =                   p1 | p2 | p3 | return point | p1 | p2 | p3
```

rather the style a one writes in
you will understand with time
06 Oct 2011, 19:37
idle

Joined: 06 Jan 2011
Posts: 408
Location: Ukraine
idle
AsmGuru62 wrote:

It used for local variables.
'4' in this case is a size of a local variable(s) - which can be accessed with [ebp+ofs]....
[/code]

[ebp-4]
06 Oct 2011, 19:41
dancho

Joined: 06 Mar 2011
Posts: 74
dancho
btw, if you asking yourself why is there sub at the start of the procedure,well
the stack stores various data from high memory , and esp register serves as indirect memory operand of the top stack,

so when you add data the stack grows down from high to low memory,
and when you remove data from the stack it shrinks from low to high,

and thats why we have add instruction at the procedure end,
program need to restore ( balance ) stack pointer for the same data we take it at the start...

and ( as your example shows ) the ebp register ( base pointer ) is usually used as pointer to the stack memory area...
06 Oct 2011, 20:00
Goplat

Joined: 15 Sep 2006
Posts: 181
Goplat
Fred wrote:
I still don't get this entirely.
I have seen the classic
Code:
```push ebp
mov ebp, esp
sub esp, 4 (or some other number)    ```

, but I'm not sure what the point is. I haven't made any big or advanced asm programs myself, so all I've been doing with ebp is using it as a general purpose register... can someone explain what ebp/rbp usually is used for?

If every procedure starts with "push ebp/mov ebp,esp" and ends with "pop ebp", then a debugger can follow the chain of pushed EBPs to give you a stack trace.

Apart from debugging, addressing local variables relative to EBP takes one fewer byte in the instruction encoding than relative to ESP, so if you have more than four such references it'll make the code smaller (unless you could do better by using EBP for other purposes).

In 16-bit code you couldn't do memory access relative to SP at all, so using BP was a necessity.
06 Oct 2011, 21:22
AsmGuru62

Joined: 28 Jan 2004
Posts: 1457
AsmGuru62

In my code the lines reversed, like so:
Code:
```PUSH EBP
SUB  ESP, <local size>
MOV  EBP,ESP
```

That is why I used +4 and not -4.
It was done because I generate locals as structures:
Code:
```virtual at 0
loc32:
.VAR1  INT32   ?
.VAR2  INT32   ?
.VAR3  INT32   ?
end virtual

...

MOV EAX, [EBP + loc32.VAR2]
```
06 Oct 2011, 21:35
Fred

Joined: 22 Oct 2010
Posts: 39
Fred
AsmGuru62 wrote:
It used for local variables.
'4' in this case is a size of a local variable(s) - which can be accessed with [ebp+ofs] - in our case 'ofs' = 0.

To access locals ESP also can be used, but then any PUSH/POP will be affecting the 'ofs'.

Hmm, yeah, changes to esp would require a different offset. I haven't ran into this myself, but I guess changes to esp might happen.

Goplat wrote:
If every procedure starts with "push ebp/mov ebp,esp" and ends with "pop ebp", then a debugger can follow the chain of pushed EBPs to give you a stack trace.

Ah, ok.

Quote:
Apart from debugging, addressing local variables relative to EBP takes one fewer byte in the instruction encoding than relative to ESP, so if you have more than four such references it'll make the code smaller (unless you could do better by using EBP for other purposes).

In 16-bit code you couldn't do memory access relative to SP at all, so using BP was a necessity.

Aha, interesting. =)

So... ebp is (mostly) used as an offset to local vars in function calls since esp might change? And making stack traces possible.
07 Oct 2011, 13:36
typedef

Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef
I've also seen codes where [esp-x] was just filled with a number like so

Code:
```push ebp
mov ebp,esp
mov dword[esp-4],100h
...
```
07 Oct 2011, 17:37
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum