究竟是什么基指针和堆栈指针?做他们点什么呢? [英] What is exactly the base pointer and stack pointer? To what do they point?

查看:388
本文介绍了究竟是什么基指针和堆栈指针?做他们点什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这个例子的维基百科的到来,其中DrawSquare()调用的DrawLine()

Using this example coming from wikipedia, in which DrawSquare() calls DrawLine(),

替换文本

(注意,此图在底部的顶部高地址和低地址。)

(Note that this diagram has high addresses at the bottom and low addresses at the top.)

谁能解释一下我是什么 EBP 尤其在这种情况下?

Could anyone explain me what ebp and esp are in this context?

这是我所看到的,我想说的堆栈指针指向总是堆栈的顶部,和底座指向当前函数的开始?还是什么?

From what I see, I'd say the stack pointer points always to the top of the stack, and the base pointer to the beginning of the the current function? Or what?

编辑:我在Windows开发背景下的意思是这

edit: I mean this in the context of windows programs

EDIT2:?以及如何做 EIP 工作,太

edit2: And how does eip work, too?

EDIT3:我从MSVC ++以下code:

edit3: I have the following code from MSVC++:

var_C= dword ptr -0Ch
var_8= dword ptr -8
var_4= dword ptr -4
hInstance= dword ptr  8
hPrevInstance= dword ptr  0Ch
lpCmdLine= dword ptr  10h
nShowCmd= dword ptr  14h

所有这些似乎是双字,从而以4个字节。所以,我可以看到有从的hInstance 4个字节VAR_4的差距。他们是什么?我想这是返回地址,可以在维基百科的图片中可以看出?

All of them seem to be dwords, thus taking 4 bytes each. So I can see there is a gap from hInstance to var_4 of 4 bytes. What are they? I assume it is the return address, as can be seen in wikipedia's picture?

(编者注:去掉了从迈克尔的回答,并没有在这个问题属于很长的报价,但后续问题被编辑在):

(editor's note: removed a long quote from Michael's answer, which doesn't belong in the question, but a followup question was edited in):

这是因为该函数调用的流程是:

This is because the flow of the function call is:

* Push parameters (hInstance, etc.)
* Call function, which pushes return address
* Push ebp
* Allocate space for locals

我的提问(最后,我希望!)现在的问题是,究竟是什么我从弹出我想起来打电话序言的结尾函数的参数的瞬间会发生什么?我想知道是怎么EBP,尤其在那些时刻演变(我已经知道如何序言的作品,我只是想知道后,我被推入堆栈,序言之前的参数发生了什么)。

推荐答案

ESP是你说的是,堆栈的顶部。

esp is as you say it is, the top of the stack.

EBP是在函数的开始通常被设置为电除尘器。局部变量减去一个常量从EBP的偏移访问。所有的调用约定的x86 EBP定义为是在函数调用pserved $ P $。 EBP本身实际上指向previous框架的基本指针,使堆走在一个调试器和查看其他帧的局部变量工作。

ebp is usually set to esp at the start of the function. Local variables are accessed by subtracting a constant offset from ebp. All x86 calling conventions define ebp as being preserved across function calls. ebp itself actually points to the previous frame's base pointer, which enables stack walking in a debugger and viewing other frames local variables to work.

大多数功能prologs看起来是这样的:

Most function prologs look something like:

push ebp      ; Preserve current frame pointer
mov ebp, esp  ; Create new frame pointer pointing to current stack top
sub esp, 20   ; allocate 20 bytes worth of locals on stack.

再后来你可能有code类的功能(presuming局部变量是4字节)

Then later in the function you may have code like (presuming both local variables are 4 bytes)

mov [ebp-4], eax    ; Store eax in first local
mov ebx, [ebp - 8]  ; Load ebx from second local

FPO或帧指针省略的优化,你可以启用实际上消除了这一点,并使用EBP作为另一个寄存器和访问当地人直接关闭的ESP,但是这使得调试有点难度,因为调试器不再能够直接访问堆栈帧较早的函数调用。

FPO or frame pointer omission optimization which you can enable will actually eliminate this and use ebp as another register and access locals directly off of esp, but this makes debugging a bit more difficult since the debugger can no longer directly access the stack frames of earlier function calls.

编辑:

有关更新后的问题,在栈中缺少两个项目分别是:

For your updated question, the missing two entries in the stack are:

var_C = dword ptr -0Ch
var_8 = dword ptr -8
var_4 = dword ptr -4
*savedFramePointer = dword ptr 0*
*return address = dword ptr 4*
hInstance = dword ptr  8h
PrevInstance = dword ptr  0C
hlpCmdLine = dword ptr  10h
nShowCmd = dword ptr  14h

这是因为该函数调用的流程是:

This is because the flow of the function call is:


  • 按参数(实例句柄等)

  • 通话功能,这推寄信人地址

  • 推EBP

  • 分配空间当地人

这篇关于究竟是什么基指针和堆栈指针?做他们点什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆