我試圖理解我從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]大小?
謝謝,它幫了很多。我試圖插入不同的輸入,當字符串長度爲112字節('perl -e'print「A」x112')而不是128字節時,我得到分段錯誤,爲什麼? –
@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