代碼如下:
#include <stdio.h>
void hello()
{
int i = 0;
printf("i = %d, hello world\n", i);
}
int main()
{
hello();
return 0;
}
gdb生成調(diào)試信息,跟進(jìn)去看看。
在調(diào)用hello之前,在main函數(shù)內(nèi)查看寄存器情況時(shí),打印如下:
(gdb) info registers
eax 0xbff644a4 -1074379612
ecx 0xbff64420 -1074379744
edx 0x1 1
ebx 0xb7f2fff4 -1208811532
esp 0xbff64400 0xbff64400
ebp 0xbff64408 0xbff64408
esi 0x8048420 134513696
edi 0x8048310 134513424
eip 0x80483f7 0x80483f7 <main+17>
eflags 0x286 [ PF SF IF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
在進(jìn)入hello函數(shù)之后,查看寄存器情況,打印如下:
(gdb) info registers
eax 0xbff644a4 -1074379612
ecx 0xbff64420 -1074379744
edx 0x1 1
ebx 0xb7f2fff4 -1208811532
esp 0xbff643e0 0xbff643e0
ebp 0xbff643f8 0xbff643f8
esi 0x8048420 134513696
edi 0x8048310 134513424
eip 0x80483ca 0x80483ca <hello+6>
eflags 0x282 [ SF IF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
根據(jù)兩個(gè)棧楨中寄存器的數(shù)據(jù),看看變化前的esp - 變化后的ebp,得到以下結(jié)果:
(gdb) print 0xbff64400 - 0xbff643f8
$1 = 8
再看看 在hello之中*(ebp), *(ebp + 4)的數(shù)據(jù):
(gdb) x 0xbff643f8
0xbff643f8: 0xbff64408
(gdb) x 0xbff643f8+4
0xbff643fc: 0x080483fc
其中, 第一次打印的結(jié)果0xbff64408是main棧楨中ebp寄存器的數(shù)據(jù)。
而反匯編main函數(shù)的結(jié)果如下:
(gdb) disassemble main
Dump of assembler code for function main:
0x080483e6 <main+0>: lea 0x4(%esp),%ecx
0x080483ea <main+4>: and $0xfffffff0,%esp
0x080483ed <main+7>: pushl -0x4(%ecx)
0x080483f0 <main+10>: push %ebp
0x080483f1 <main+11>: mov %esp,%ebp
0x080483f3 <main+13>: push %ecx
0x080483f4 <main+14>: sub $0x4,%esp
0x080483f7 <main+17>: call 0x80483c4 <hello>
0x080483fc <main+22>: mov $0x0,%eax
0x08048401 <main+27>: add $0x4,%esp
0x08048404 <main+30>: pop %ecx
0x08048405 <main+31>: pop %ebp
0x08048406 <main+32>: lea -0x4(%ecx),%esp
0x08048409 <main+35>: ret
End of assembler dump.
可以看到,在調(diào)用call hello的下一句指令的地址是0x080483fc,就是上面
(gdb) x 0xbff643f8+4
0xbff643fc: 0x080483fc
的結(jié)果。
因此,可以給出函數(shù)調(diào)用前后棧楨的分布圖如下:

另外,在圖中沒有顯示出來的是hello棧楨中的局部變量從(ebp - 4)地址開始,你可以在hello棧楨中打?。?ebp-4)的數(shù)據(jù)看看。
結(jié)論如下:
1) 調(diào)用前的esp - 調(diào)用后的ebp = 8,因?yàn)樾枰4鎯蓚€(gè)寄存器的數(shù)據(jù)
2)*(ebp)存放的是上一個(gè)棧楨的ebp數(shù)據(jù),而*(ebp+4)存放的是返回上一個(gè)棧楨時(shí)需要執(zhí)行的下一條語句的地址,即函數(shù)調(diào)用返回時(shí)存入到eip寄存器的數(shù)據(jù)。
3)根據(jù)結(jié)論2),有一個(gè)問題:為什么下一條指令的地址高于所要保存的ebp寄存器的地址?因?yàn)樵赾all指令執(zhí)行的時(shí)候首先保存下一條指令的地址,再跳轉(zhuǎn)到函數(shù)的執(zhí)行地址。
4)根據(jù)結(jié)論3),將與函數(shù)調(diào)用有關(guān)的幾條匯編指令再進(jìn)行一下講解:
a)call指令:上面已經(jīng)做了解釋,重復(fù)如下:首先保存下一條指令的地址,再跳轉(zhuǎn)到函數(shù)的執(zhí)行地址
b)進(jìn)入一個(gè)函數(shù)時(shí)首先會(huì)調(diào)用的幾條語句:
push %ebp ;保存ebp寄存器
mov %esp,%ebp ;將esp寄存器保存到ebp
sub $0x18,%esp ;調(diào)整esp,用以保存返回地址和局部變量,這個(gè)調(diào)整值并不確定,根據(jù)局部變量的情況而定
這幾句指令就是用于保存上一個(gè)棧楨的ebp寄存器地址,向地址低位擴(kuò)展棧位置。
其中的pushl %ebp
相當(dāng)于:
subl $4, %esp
movl %ebp, (%esp)
c)退出一個(gè)函數(shù)時(shí),執(zhí)行的幾條指令是:
退出一個(gè)函數(shù)時(shí):
leave ; 相當(dāng)于 movl %ebp, %esp;popl %ebp(也就是將ebp保存的esp地址恢復(fù),然后恢復(fù)ebp寄存器數(shù)據(jù))
ret ; 相當(dāng)于popl %eip;jmp %eip的作用
5) 兩個(gè)名詞不能混淆了,棧(stack)指的是一個(gè)進(jìn)程中所有用于給函數(shù)調(diào)用局部變量的空間,這是對(duì)進(jìn)程全局而言的;而棧楨(stack frame)針對(duì)的是進(jìn)程內(nèi)一個(gè)單一的函數(shù)的空間,因此,棧楨是棧的子集。
注意,以上說明均在X86平臺(tái)下。