Function Calls and the Stack

I'm reading this book and it says (according to this calling convention) it does this:

Note:

The function looks like this:
function( int a, int b)
The function call looks like this:
function(2, 1)


push arguments for function a and b backwards onto the stack
function is called, and ret is "placed" onto the stack
Prolog is executed
EBP is pushed onto stack
Variables local to the function are pushed onto the stack

This is how it tells me the stack looks at this point:

Low Memory
b
a
Ret
EBP
array
High memory

My questions are (since stack grows down memory addresses):

Why are a and b at the top when they are the first things pushed onto the stack? Why is array at such a high address? What does it mean "push backwards onto the stack"?

The assembly language looks like this according to the book:

sub 0x8, esp
sub 0x8, esp
push 0x2
push 0x1

Then inside the disassembled function it says:

push ebp

This is where I get really confused because 3 push commands were executed and how the hell is that gonna put ebp before a and b?


No wonder you are confused, throw that book away. That said, maybe the book isn't wrong, but you misunderstand it.

You didn't say what array was. Assuming that's a local in the called function, the stack may look like the illustration, albeit with low and high memory mixed up.

High Memory
b
a
Ret
EBP (saved EBP of caller)
array (local variables of called function)
Low memory
链接地址: http://www.djcxy.com/p/80378.html

上一篇: 如何通过call和ret在该程序中更改堆栈指针

下一篇: 函数调用和堆栈