函数从堆栈返回的地址是什么?

时间:2013-11-15 12:17:02

标签: c++ gdb registry stack stackframe

我有以下代码

int isBST(struct node* node) 
{ 
  return(isBSTUtil(node, INT_MIN, INT_MAX)); 
} 

int isBSTUtil(struct node* node, int min, int max) 
{
  if (node==NULL) 
     return 1;      

  if (node->data <= min || node->data > max) 
     return 0; 

  return
    isBSTUtil(node->left, min, node->data) &&  // Allow only distinct values
    isBSTUtil(node->right, node->data, max);  // Allow only distinct values
}

当我在GDB中调试代码时,我看到第二个参数是由地址ebp + 0xc(0xbffff188 + 0xc)设置的,第三个参数设置为ebp + 0x10,第一个参数不清楚where,in理论上,我们知道函数的返回地址位于EBP + 4,第一个参数位于EBP +8和....我从那里得到了什么?

1 个答案:

答案 0 :(得分:0)

理论上,我们不知道论点或论点的位置 返回地址位于。在特定的架构上,我们 可以(通常)弄清楚特定编译器的作用 检查一些生成的汇编程序。 第一个的事情 检查是可预测的功能。在Intel 32位处理器上, 帧指针将在EBP中,并且将在之后设置 一定数量的push来保存寄存器。 (特别是, 在为本地设置EBP之前必须有push EBP 框架。)英特尔的典型可能是:

function:
        PUSH EBP
        MOV  EBP, ESP
        SUB  ESP, n         ;   allocate space for local variables

除此之外:英特尔堆栈逐渐减少,编译器也在增长 因为英特尔几乎普遍推动从右到右的论点 离开,所以在你的情况下,max将拥有最高的地址, min将在它下面,node低于它。所以你的 框架的图像将是:

[EBP - ...]     local variables
[EBP + 0]       the pushed old EBP
[EBP + 4]       the return address
[EBP + 8]       the first argument (node)
[EBP + 12]      the second argument (min)
[EBP + 16]      the third argument (max)

(假设参数都是32位值。) 当然,编译器之前可能会推送额外的寄存器 推动EBP,导致抵消相应更高。 这只是一种可能的布局。