Decoding equivalent assembly code of C code...

Posted by puffadder on Stack Overflow See other posts from Stack Overflow or by puffadder
Published on 2010-06-07T10:08:38Z Indexed on 2010/06/07 10:12 UTC
Read the original article Hit count: 316

Filed under:
|
|

Hi All,

Wanting to see the output of the compiler (in assembly) for some C code, I wrote a simple program in C and generated its assembly file using gcc.

The code is this:

#include <stdio.h>  

int main()  
{  
    int i = 0;

    if ( i == 0 )
    {
        printf("testing\n");
    }

    return 0;  
}  

The generated assembly for it is here (only the main function):

_main:  
pushl   %ebpz  
movl    %esp, %ebp  
subl    $24, %esp  
andl    $-16, %esp  
movl    $0, %eax  
addl    $15, %eax  
addl    $15, %eax  
shrl    $4, %eax  
sall    $4, %eax  
movl    %eax, -8(%ebp)  
movl    -8(%ebp), %eax  
call    __alloca  
call    ___main  
movl    $0, -4(%ebp)  
cmpl    $0, -4(%ebp)  
jne L2  
movl    $LC0, (%esp)  
call    _printf  
L2:  
movl    $0, %eax  
leave  
ret  

I am at an absolute loss to correlate the C code and assembly code. All that the code has to do is store 0 in a register and compare it with a constant 0 and take suitable action. But what is going on in the assembly ?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c

    Related posts about gcc