Why are function parameters pushed earlier on call stack than the return address?

后端 未结 2 1214
猫巷女王i
猫巷女王i 2021-02-02 01:32

From http://en.wikipedia.org/wiki/Stack_pointer#Structure

\"enter

I am wondering w

2条回答
  •  醉话见心
    2021-02-02 01:55

    The return address is usually pushed via the call machine command, [which in the native language's instruction set] while the parameters and variables are pushed with several machine commands - which the compiler creates.

    Thus, the return address is the last thing pushed by the caller, and before anything [local variables] pushed by the callee.

    The parameters are all pushed before the return address, because the jump to the actual function and the insertion of the return address to the stack is done in the same machine command.

    Also, another reason is - the caller is the one allocating space on stack for the parameters - It [the caller] should also be the one who cleans it up.

提交回复
热议问题