参数如何加载到 $esp 中?
假设我有两个参数 arg1 和 arg2,以及一个我希望调用的函数。如果我将 arg1 加载到 $esp + 4 并将 arg2 加载到 $esp 中,我将执行以下哪一项操作:
func(arg1, arg2)
或
func(arg2, arg1)
我正在使用 IA32。
Let's say I have two arguments, arg1 and arg2, and a function that I wish to call. If I load arg1 into $esp + 4 and arg2 into $esp, which of the following will I be doing:
func(arg1, arg2)
or
func(arg2, arg1)
I am using IA32.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
天哪,我可以用我最喜欢的方式回答!
这取决于您!
这取决于您使用的调用机制。看看 x86 调用约定,你会感到惊讶。
但是,请注意,在最常见的默认值 cdecl 中,函数参数按从右到左的顺序压入堆栈。事实上,它们也在 Windows 使用的 stdcall 中从右向左推送。
现在来确定
esp+x
在函数排序中采用哪种方式。堆栈从地址空间的高端向下增长,从右到左的顺序意味着最右边的对象首先进入堆栈,因此最右边的参数具有更高的内存地址。因此,当您添加到 esp 或任何跟踪堆栈中最低(就内存地址而言)参数的寄存器时,您将从右向左移动参数。我想补充一点,
ebp
往往是基指针,并且esp
被移动以允许按照 函数序言。Oh man I get to answer in my favourite way!
It Depends!
It depends on the calling mechanism you're using. Take a look at x86 calling conventions and be amazed.
However, note that in the most common default, cdecl, function parameters are pushed on the stack in a right-to-left order. In fact, they are also pushed right-to-left in stdcall, which Windows uses.
Now to work out which way
esp+x
goes in the function ordering. The stack grows from the high end of the address space down, and right to left ordering implies the rightmost object goes onto the stack first, so rightmost arguments have a higher memory addresses. Thus, as you add toesp
or whatever register tracks the lowest (in terms of memory address) argument in the stack you are moving through the arguments right to left.I want to add that
ebp
tends to be the base pointer andesp
is moved to allow for local variable storage as per this description of the function prologue.f(arg2, arg1)
堆栈向下增长,参数的布局有点像数组,特别是第一个参数(您的示例称为 arg2)位于同一位置,无论调用中有多少个参数。
否则,当函数采用可变数量的参数时,要使函数正常工作将非常困难。
理论上,平台可以采取不同的方式,但任何主要实例都不是这样。
f(arg2, arg1)
The stack grows down and the arguments are are laid out somewhat like an array, and in particular such that the first argument (what your example calls arg2) is at the same place regardless of how many arguments are in the call.
Otherwise, it would be very difficult to make functions work when they take a variable number of parameters.
A platform could, in theory, do it differently, but that's not the case with any major instance.