Understanding Assembly language output of a C program

 #include <stdio.h>

 static int i = 100;

 /* Declard as extern since defined in hello.c */
 extern int global;

 int function(char *input)
 {
  printf("%sn", input);
  return global;
 }; 

    .file   "foo.c"
    .data
    .align 4
    .type   i, @object
    .size   i, 4
i:
    .long   100
    .text
.globl function
    .type   function, @function
function:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    movl    8(%ebp), %eax
    movl    %eax, (%esp)
    call    puts
    movl    global, %eax
    leave
    ret
    .size   function, .-function
    .ident  "GCC: (Debian 4.4.5-8) 4.4.5"
    .section    .note.GNU-stack,"",@progbits

What is the reason for the:

subl $24, %esp

Why is he making space on the stack for local variables by decrementing the stackpointer by 24 bytes? There are no local variables in the function. Then he does:

movl %eax, (%esp)

Why - Because the value(pointer char *input) stored at ebp+8 was moved to eax and should now be passed to puts.. but then why does he do the prior subl - To store the return value of int? But that's 4 bytes..Also i read that C did not support the calling function having to make space on the stack for the return value (renetrancy issues). What is going on here please :( ?? He could be aligning the pointer argument on a word boundary.. but then the SysV I386 ABI says that tail padding is employed..

Also,

movl %esp, %ebp

implies that esp and ebp will both point to the base of the new functions stack frame. But, assuming there was a 'call function;' consider the stack: You'd have:

pushl ptr //push char *ptr
call function 
{
pushl ebp So stack contains:
ptr
ret-value
ebp
->with-esp-pointing-to-after-ebp
(ESP always points to the top of the stack but after the last pushed   
element..?)

So he's setting ebp to esp. But in the ABI specification on pg36, SysV ABI I386 Architecture Processor Supplement 4E, he says: 0(%ebp) previous %ebp (optional) I'm guessing that reads: %ebp+0 points to previous ebp But as you can see, ebp+0 actually points to after the stored previous ebp..???

also in another program,

andl $-16, %esp
subl $32, %esp

so he's first nueking the last 4 bits of esp.. umm how much space does that give him? and then the subl..

Could someone suggest a decent tutorial or book for this.. I don't want to master assembly, just want to know enough to understand a bit of the ABI specification and alignment and GOT/PLT/Virtual-addressing and compiler/linker stuff-symbolTable/relocation etc (i'm using the Levine book but it's huge - interesting though but COFF and IBM/Sparc stuff :( which is why I started with the ABI spec. There's also Ian Wienand's web site: Computer Science from the Bottom Up.)

链接地址: http://www.djcxy.com/p/80344.html

上一篇: 只需要帮助澄清汇编代码和有关我的分析的反馈

下一篇: 了解C程序的汇编语言输出