Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61021

Drawing a stack frame for x86 assembly

So, I am kind of confused about drawing a stack frame for my assembly code. I have a feeling I started out wrong.

Here is what I got so far, but as you can see I am confused at step 5, because I think my initial layout is wrong.

enter image description here enter image description here

Can you tell me where I went wrong?

Upvotes: 14

Views: 15365

Answers (4)

mods
mods

Reputation: 13

The diagram shows parameters below the return address, which is actually wrong.

Assuming that the stack grows towards lower addresses, if there is need to put parameters on the stack, they reside at higher addresses compared to the return address.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490018

I think I'd start with a diagram that showed some (semi-)arbitrary amount of empty space at the "top" of the stack, and probably display EBP and ESP off to the left, with arrows to show where they're pointing to. I've used solid arrows for "points to" and dashed for data movement (in retrospect, it might be better to reverse that).

alt text

Upvotes: 29

Giuseppe Guerrini
Giuseppe Guerrini

Reputation: 4426

You are right with your diagram. The compiler uses some optimizing tricks: the first call is "quite normal", indeed the "f" parameter is placed on top of the stack. The second call is postponed after the local context cleanup (instruction "leave"), and the "h" function's parameter ir "recycled" to contain "2". Than the second "call" to "f" becomes a simple "jmp", since it's the very last line in the calling function "h" (the context of "h" has been already thrown away by "leave").

Bye!

Upvotes: 0

tyranid
tyranid

Reputation: 13318

The distance between the current ebp (once it is capture from esp) and y is indeed 8 bytes in this case as you have the return eip and the value of the previous ebp on the stack. Your diagram is correct from what I can tell though the left hand addresses are more confusing :)

Upvotes: 2

Related Questions