2
votes

Quick question about the answer to the question: Purpose of ESI & EDI registers?

srcp [srcidx++] = argv [j]; C code translates too

8B550C         mov    edx,[ebp+0C]
8B0C9A         mov    ecx,[edx+4*ebx]
894CBDAC       mov    [ebp+4*edi-54],ecx
47             inc    edi

I've taken an assembly class and know the basics, not looking for what mov means, but I'm mostly confused as the what the +0C, the +4*ebx, and the ebp+4*edi-54 statements are for. He explains it in the post but I'm still not understanding.

The ebp+12 has argv, but why is it incremented by 12?

The ebx is j, but why is that multiplied by 4 and added onto ebp?

And he says that "third instruction uses edi multiplied by 4 and adds ebp offset by 0x54 (location of srcp)"; why does it have to be multiplied by 4? Also he says add the offset; why is it -54 and not +54 in the code?

Just a little confused.

1
i think multiplication by 4 exists because the type of argv is a 4 bytes one (int for example). - seleciii44
exactly your question is explained here - seleciii44

1 Answers

5
votes

I think we have the following:

  • srcidx lives in edi register
  • argv is at offset 0xc from the start of the frame (thus [ebp+0C])
  • j is indeed in ebx - the reason we have times 4 is that we are working with an array of 4 byte integers so the offset has to be multiplied by the size of the element (4) to get the correct address.
  • srcp is a stack variable positioned at offset -54 relative to the start of the frame (ebp)

I hope that answers your questions. It might help to load the code in a debugger and run it through stepping one instruction at a time and dumping the registers and portions of memory at each step and comparing it with the state of your variables.