2013-04-13 47 views
2

我试图理解我从gdb反汇编得到的c程序的汇编代码,请你帮我一下。了解gdb生成的程序集

我的C代码:

#include <unistd.h> 



int main(int argc, char *argv[]) 

{ 

char buff[100]; 

/*if no argument…*/ 

if(argc <2) 

{ 

    printf("Syntax: %s <input string>\n", argv[0]); 

    exit (0); 

    } 

    strcpy(buff, argv[1]); 

    return 0; 

} 

和我的主要功能组件的代码是:

为函数main的汇编代码转储:

0x08048424 <+0>: push %ebp 
    0x08048425 <+1>: mov %esp,%ebp 
    0x08048427 <+3>: and $0xfffffff0,%esp 
    0x0804842a <+6>: add $0xffffff80,%esp 
    0x0804842d <+9>: cmpl $0x1,0x8(%ebp) 
    0x08048431 <+13>: jg  0x8048454 <main+48> 
    0x08048433 <+15>: mov 0xc(%ebp),%eax 
    0x08048436 <+18>: mov (%eax),%eax 
    0x08048438 <+20>: mov %eax,0x4(%esp) 
    0x0804843c <+24>: movl $0x8048544,(%esp) 
    0x08048443 <+31>: call 0x8048344 <[email protected]> 
    0x08048448 <+36>: movl $0x0,(%esp) 
    0x0804844f <+43>: call 0x8048354 <[email protected]> 
    0x08048454 <+48>: mov 0xc(%ebp),%eax 
    0x08048457 <+51>: add $0x4,%eax 
    0x0804845a <+54>: mov (%eax),%eax 
    0x0804845c <+56>: mov %eax,0x4(%esp) 
    0x08048460 <+60>: lea 0x1c(%esp),%eax 

哪里是部分的分配buff [100]大​​小?

回答

4

以下代码

int main(int argc, char *argv[]) 
{ 

char buff[100]; 

请求创建一个炭的[100]缓冲堆栈上。这里是实际发生的事情:

;// 1. pushing the base pointer register on the stack 
0x08048424 <+0>: push %ebp 

;// 2. Creating the stack frame. Copying the stack pointer register to the base pointer 
;// register creates a stack frame: an area on the stack where a subroutine 
;// can store local data. 
0x08048425 <+1>: mov %esp,%ebp 

;// 3. Making sure that the stack is aligned to 16 bytes. 
0x08048427 <+3>: and $0xfffffff0,%esp 

;// 4. Making room for 128 bytes (100 as requested would throw off the alignment). 
;// 128 is compatible with your requests and is optimized. 
0x0804842a <+6>: add $0xffffff80,%esp 

所以,这里是你的缓冲区被创建,在一个16字节对齐的堆栈。你要求100,编译器给你至少100,同时优化速度。

+0

谢谢,它帮了很多。我试图插入不同的输入,当字符串长度为112字节('perl -e'print“A”x112')而不是128字节时,我得到分段错误,为什么? –

+0

@ZainabJH如果你声明一个'char buff [100];'并且你试图在其中写入一个长度为112个字符的字符串,你将会溢出你的缓冲区并擦除之后的任何内容。由于你的缓冲区在堆栈中......两个事实:1.如果你的缓冲区是独立的,编译器只需要分配112个字节(7 * 16)。所以还有其他的东西(也许)。 2.堆栈在您的架构上向下(参见:[这篇文章在SO](http://stackoverflow.com/questions/4560720/why-stack-address-goes-in-decreasing-memory-address))所以你可能会覆盖重要的东西(如'ebp',返回地址)... – Jean

4

这里:

add $0xffffff80,%esp 

这substracts 128个字节(添加-128)到ESP(堆栈指针寄存器)。

3

我想是这一个add $0xffffff80,%esp。移动堆栈指针以在该函数内部提供空间。