【问题标题】:Function Calls and the Stack函数调用和堆栈
【发布时间】:2014-07-08 05:57:20
【问题描述】:

我正在阅读这本书,它说(根据这个调用约定)它是这样做的:

注意:

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

这就是它告诉我堆栈在这一点上的样子:

Low Memory
b
a
Ret
EBP
array
High memory

我的问题是(因为堆栈会向下增长内存地址):

为什么当 a 和 b 是最先被压入堆栈的东西时,它们在顶部?为什么数组在这么高的地址? “向后推入堆栈”是什么意思?

按照书上的汇编语言是这样的:

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

然后在反汇编函数里面说:

push ebp

这是我真正感到困惑的地方,因为执行了 3 个推送命令,这到底是怎么把 ebp 放在 a 和 b 之前的?

【问题讨论】:

  • Low MemoryHigh memory 在您的图中颠倒了,并且堆栈框架设置代码错误。此外,此堆栈帧结构完全依赖于编译器并受操作系统约束。例如。如果您查看现代 gcc 堆栈框架,它们在很多时候不会像这样工作。现代 Windows 64 位框架有额外的花里胡哨。看来这本书很烂。
  • 谢谢。我很感激。

标签: assembly


【解决方案1】:

难怪你糊涂了,把那本书扔了。也就是说,也许这本书没有错,但你误解了它。

你没有说array 是什么。假设这是被调用函数中的一个局部,堆栈可能看起来像插图,尽管低内存和高内存混合在一起。

High Memory
b
a
Ret
EBP (saved EBP of caller)
array (local variables of called function)
Low memory

【讨论】:

  • 这本书实际上是Shellcoder手册的第二版(相当有名)。我刚下载了第三版,翻的像你一样。
猜你喜欢
  • 2019-11-22
  • 2012-09-11
  • 2014-11-13
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多