EBP ESP and ESI?

Posts 1–4 of 4 · Page 1 of 1
EBP ESP and ESI?
Hello assembly section.

I'm having a little trouble with these 3 registers I've mentioned. I was looking at calling conventions and it seems that this is what it looks like.

Code:
push ebp
mov ebp,esp
I have a few questions relevant to that little piece of code.

1)First off, we're pushing ebp onto the stack to save it, I think. What is the point of saving the old base pointer...? Also, does the old base pointer still point to the old parameters? If so, why would we need them and what the hell is the point of it?

2)Why are we moving esp to ebp? I understand that ebp+8 is the first parameter, ebp+0xC is the second etc etc.. But, why can't we just do esp+8 instead? What's the point of having that extra instruction?

3)I can't really find a detailed explanation on exactly how calling a function works, so if anyone could take their time to explain it that would be awesome.

Moving on to the ESI register. I have absolutely no idea what the hell we would ever use this for...
Sorry for the late reply.. holidays and stuff..
A really good read is here: x86 Disassembly/Functions and Stack Frames - Wikibooks, collection of open-content textbooks - full index here (x86 Disassembly - Wikibooks, collection of open-content textbooks)but to summarize quickly:

The entire ebp/esp thing is so that we can access parameters to a function as positive offsets from ebp and local variables in a function as negative offsets. A function can call another function which calls another function etc, and each can this way have a standardized way of getting at the variables etc they need. Also when the sub-function is done, the ebp gets restored so that the calling function doesn't have it's variables messed with.

Code:
As an example, the following C code:

void MyFunction3(int x, int y, int z)
{
  int a, int b, int c;
  ...
  return;
}

Will create the following assembly code:

push ebp
mov ebp, esp
sub esp, 12 ; sizeof(a) + sizeof(b) + sizeof(c)
;x = [ebp + 8], y = [ebp + 12], z = [ebp + 16]
;a = [ebp - 12] = [esp], b = [ebp - 8] = [esp + 4], c = [ebp - 4] = [esp + 8]
mov esp, ebp
pop ebp
ret 12 ; sizeof(x) + sizeof(y) + sizeof(z) - cleans up stack pointer
If you don't see a 'sub esp, SOMEVALUE' that usually means there are no local variables being allocated in that particular method.


ESI: "(Extended) Source Index" usually used in combination with EDI or the 'Destination Index' to copy bytes/dwords etc using movsb/repz like commands. Basically you can think of ESI/EDI like pointers in C++.
Sample I found somewhere:
Code:
    cld                  ; set direction flag forward
    mov esi, source      ; put address into the source index
    mov edi, dest        ; put address into the destination index
    mov ecx, ln          ; put the number of bytes to copy in ecx
  ; --------------------------------------------------
  ; repeat copying bytes from ESI to EDI until ecx = 0
  ; --------------------------------------------------
    rep movsb
Hope that provides some insight
As always, your posts are always useful. Thanks a lot.

I actually tried disassembling a small program and it came out to be exactly what you said. The way ESP and EBP works is clear to me now. Thank you.

Although, the ESI and EDI registers being pointers confuses me alot.
Basically whenever you want to copy data in memory from one location to another is when you use ESI/EDI with ESI pointing to your source and EDI pointing to your destination. (though you could in theory use them like any other register if you don't need them for any of those operations).

The above sample for instance could basically be copying a string of length ln, while in C++ you'd have a char* source and a char* target . Look at the strcpy or memcpy functions for reference.

Basically it takes whatever value ESI is pointing to and sticks it in the location EDI is pointing at, and then moves forward one & repeats until done. Only difference is that unlike C++ loops which usually increment in this case it counts down until ECX is 0.
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us