Function arguments push order

后端 未结 2 1717
失恋的感觉
失恋的感觉 2021-02-06 00:32

Why are function arguments pushed on the stack in right to left order?

相关标签:
2条回答
  • 2021-02-06 01:15

    To enable the existence of functions with a variable number of arguments, like printf. The function can extract the first one or two arguments and then use their values to deduce the total number of arguments on the stack.

    0 讨论(0)
  • 2021-02-06 01:22

    The only reason is for variadic functions: the first arguments popped from the stack are the "known" ones for the function, and it can determine from them how many other arguments it should read from the stack.

    Notice that, for this to work fine, in such calling conventions the stack cleanup is left to the caller, that knows how many arguments it pushed on the stack. This is slightly less efficient than callee-cleanup, because the cleanup code has to be written after each function call, while in calling conventions that do not allow variadic functions it can be embedded at the end of each function.

    Other than this, there's no particular reason, in facts there are several calling conventions (e.g. Pascal, Borland Fastcall) that do not admit variadic functions and push parameters left to right.

    0 讨论(0)
提交回复
热议问题