Why does gcc generate verbose assembly code?
        Posted  
        
            by 
                Jared Nash
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Jared Nash
        
        
        
        Published on 2012-09-08T09:34:36Z
        Indexed on 
            2012/09/08
            9:37 UTC
        
        
        Read the original article
        Hit count: 376
        
I have a question about assembly code generated by GCC (-S option). Since, I am new to assembly language and know very little about it, the question will be very primitive. Still, I hope somebody will answer:
Suppose, I have this C code:
main(){
    int x = 15; 
    int y = 6;
    int z = x - y;
    return 0;
}
If we look at the assembly code (especially the part corresponding to int z = x - y ), we see:
main:
...
subl    $16, %esp
movl    $15, -4(%ebp)
movl    $6, -8(%ebp)
movl    -8(%ebp), %eax
movl    -4(%ebp), %edx
movl    %edx, %ecx
subl    %eax, %ecx
movl    %ecx, %eax
movl    %eax, -12(%ebp)
...
Why doesn't GCC generate something like this, which is less copying things around.
main:
...
movl    $15, -4(%ebp)
movl    $6, -8(%ebp)
movl    -8(%ebp), %edx          
movl    -4(%ebp), %eax          
subl    %edx, %eax              
movl    %eax, -12(%ebp)
...
P.S.
Linux zion-5 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010 i686 GNU/Linux gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
© Stack Overflow or respective owner