简体   繁体   中英

GDB disassemble for a simple program

Here is a simple program in C for which I used gdb to disassemble it to understand what is happening.

#include <stdio.h>
#include <string.h>
int main(){
    printf("%d", sizeof(foo("HELLOWORLD")));
}

int foo(char* c)
{
   printf("%s\n",c);
}

And below is the corresponding assembly code for disassemble main

0x08048414 <+0>:    push   %ebp
   0x08048415 <+1>: mov    %esp,%ebp
   0x08048417 <+3>: and    $0xfffffff0,%esp
   0x0804841a <+6>: sub    $0x10,%esp
   0x0804841d <+9>: mov    $0x8048520,%eax
   0x08048422 <+14>:    movl   $0x4,0x4(%esp)
   0x0804842a <+22>:    mov    %eax,(%esp)
   0x0804842d <+25>:    call   0x8048320 <printf@plt>
   0x08048432 <+30>:    leave  
   0x08048433 <+31>:    ret   

And below is disassemble foo

0x08048434 <+0>:    push   %ebp
   0x08048435 <+1>: mov    %esp,%ebp
   0x08048437 <+3>: sub    $0x18,%esp
   0x0804843a <+6>: mov    0x8(%ebp),%eax
   0x0804843d <+9>: mov    %eax,(%esp)
   0x08048440 <+12>:    call   0x8048330 <puts@plt>
   0x08048445 <+17>:    leave  
   0x08048446 <+18>:    ret  

I m confused about these instructions:

  1. 0x08048417 <+3> and $0xfffffff0,%esp Why stack pointer needs to be aligned when it is not modified before?

  2. 0x0804841a <+6>:sub $0x10,%esp what exactly is this instruction doing particular to the program?

  3. 0x0804841d <+9>:mov $0x8048520,%eax what is this instruction doing particular to the program?

  4. mov %eax,(%esp) What does parenthesis around %esp mean?

Would be helpful if someone explained this.

  1. belongs to the (function-)prologue, it is aligning the SP to a 16-byte boundary, by bitmasking the SP.

  2. memory for the stack-frame is created, as your pointer needs to be passed to the function. The address will be passed from the stack to the function. Yet it seems that the expression is evluated at compile-time, so no need for the actual call.

  3. 0x8048520 is probably the adress of your string "%d". It is being put into eax, from there on it is put on the stack using the stackpointer.

There is plenty of material around, like this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM