开发者

Drawing a stack frame for x86 assembly

So, I am kind of confused about drawing a stack frame for my assem开发者_如何学Pythonbly 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.

Drawing a stack frame for x86 assembly

Drawing a stack frame for x86 assembly

Can you tell me where I went wrong?


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

Drawing a stack frame for x86 assembly


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


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜