69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
struct S
{
int a;
int b[20];
int c;
};
int main(int argc, char *argv[])
{
struct S s, s1;
int a;
int b;
printf("%ld\n",(long int)&s);
printf("%ld\n",(long int)&s1);
printf("%ld\n",(long int)&a);
printf("%ld\n",(long int)&b);
return 0;
}
Breakpoint 2, main () at test.s:35
35 call _foo //调用函数foo
(gdb) info reg
eax 0x1 1
ecx 0x20018020 536969248
edx 0x0 0
ebx 0x22ac2c 2272300
esp 0x22abf0 0x22abf0
ebp 0x22ac08 0x22ac08 //esp < ebp, 说明是向低地址增长的
esi 0x0 0
edi 0x20039378 537105272
eip 0x40119d 0x40119d <main+21>
eflags 0x246 [ PF ZF IF ]
cs 0x1b 27
ss 0x23 35
ds 0x23 35
es 0x23 35
fs 0x3b 59
gs 0x0 0
(gdb) s
foo () at test.s:8
8 pushl %ebp //函数foo内部
(gdb) info reg
eax 0x1 1
ecx 0x20018020 536969248
edx 0x0 0
ebx 0x22ac2c 2272300
esp 0x22abec 0x22abec
//esp 比调用函数前的值0x22abf0小了4,进一步证明栈是往低地址方向增长
ebp 0x22ac08 0x22ac08
esi 0x0 0
edi 0x20039378 537105272
eip 0x401180 0x401180 <foo>
eflags 0x246 [ PF ZF IF ]
cs 0x1b 27
ss 0x23 35
ds 0x23 35
es 0x23 35
fs 0x3b 59
gs 0x0 0
(gdb) n
11 movl %esp, %ebp
(gdb) info reg
eax 0x1 1
ecx 0x20018020 536969248
edx 0x0 0
ebx 0x22ac2c 2272300
esp 0x22abe8 0x22abe8 //执行ebp入栈后,esp进一步减小4
ebp 0x22ac08 0x22ac08
esi 0x0 0
edi 0x20039378 537105272
eip 0x401181 0x401181 <foo+1>
eflags 0x246 [ PF ZF IF ]
cs 0x1b 27
ss 0x23 35
ds 0x23 35
es 0x23 35
fs 0x3b 59
gs 0x0 0
(gdb) f
#0 foo () at test.s:11
11 movl %esp, %ebp
(gdb) bt
#0 foo () at test.s:11
#1 0x004011a2 in main () at test.s:35
(gdb)
这个可以证明。 $ cat test.c; gcc -Wall test.c -o demo; ./demo
#include <stdio.h>
int main()
{
void* sp = 0;
//取出当前的栈顶指针
asm("movl %%esp,%0;"
:"=r"(sp)
);
printf("sp:%p\n", sp);
asm("push $0"); //整数0入栈
//取出当前的栈顶指针
asm("movl %%esp,%0;"
:"=r"(sp)
);
printf("sp:%p\n", sp);
//出栈
asm("pop %0"
:"=r"(sp)
);
return 0;
}
//sp:0x22ac10
//sp:0x22ac0c
gcc 4.7.3,上面的例子可以说明GCC的栈是往低地址方向增长。注意,这个结论不能推广到其它编译器上。