tags:

views:

550

answers:

3

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.

Can you tell me where I went wrong?

+2  A: 

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 :)

tyranid
A: 

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!

Giuseppe Guerrini
+6  A: 

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

Jerry Coffin
Wow, that's cool diagram. What program did you use to draw it? I'll look it over tomorrow - as my brain is fried tonight.
drozzy
In a word, Vizio.
Jerry Coffin
I am assuming the diagrams are in the left-to-right top-to-bottom orientation. Then I am not sure how in the 3rd diagram - you have both EBP and ESP pointing to the same address initially. I mean in previous diagram they were different.
drozzy
Yes -- that's the `mov %esp, %ebp`. When you move the value that's in ESP into EBP, they end up pointing to the same place...
Jerry Coffin
Ok looks good. I think I get it, thanks!
drozzy