Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 05 Mar 2004 19:53:42 -0500
From:      Chungwei Hsiung <skuma17@yahoo.com>
To:        Greg 'groggy' Lehey <grog@FreeBSD.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Strange instructions in compiler output
Message-ID:  <40492116.80607@yahoo.com>
In-Reply-To: <20040306001150.GQ67801@wantadilla.lemis.com>
References:  <4048CA38.6040203@yahoo.com> <20040305233209.GO67801@wantadilla.lemis.com> <4049108F.5080703@yahoo.com> <20040306001150.GQ67801@wantadilla.lemis.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Greg 'groggy' Lehey wrote:

>On Friday,  5 March 2004 at 18:43:11 -0500, Chungwei Hsiung wrote:
>  
>
>>Greg 'groggy' Lehey wrote:
>>
>>    
>>
>>>On Friday,  5 March 2004 at 13:43:04 -0500, Chungwei Hsiung wrote:
>>>
>>>
>>>      
>>>
>>>>Hello..
>>>>I am super new to this list, and I have a simple question that I don't
>>>>know why it does that. I have a simple test program. I compile it, and
>>>>gdb to disassemble main. I got the following..
>>>>
>>>>0x8048201 <main+9>:     mov    $0x0,%eax
>>>>0x8048206 <main+14>:    sub    %eax,%esp
>>>>...
>>>>
>>>>I don't know if at line 5, we move zero to %eax. why do we need to sub
>>>>        
>>>>
>>>>>eax, %esp? why do we need to substract 0 from the stack pointer??
>>>>>          
>>>>>
>>>>Any help is really appreciated.
>>>>        
>>>>
>>>This is probably because you didn't optimize the output.  You'd be
>>>surprised how many redundant instructions the compiler puts in under
>>>these circumstances.  Try optimizing and see what the code looks like.
>>>
>>>If this *was* done with optimization, let's see the source code.
>>>      
>>>
>>Hello.. thank you very much for the reply
>>I actually don't know how to use the optimization. 
>>    
>>
>
>Use the gcc command line options.  See below.
>
>  
>
>>I just compile it with gcc 3.2.2, and use gdb to disassemble main to
>>get this assembly. Is it possible I can get the non-redundent output?
>>here is the code I compile..
>>
>>...
>>    
>>
>
>The best way to look at the assembly output is to generate it directly
>from the compiler.  I get:
>
>$ cc -O -pipe -mcpu=pentiumpro -S exec.c
>$ cat exec.s
>.LC0:
>        .string "/bin/sh"
>...
>main:
>        pushl   %ebp
>        movl    %esp, %ebp
>        subl    $24, %esp
>        andl    $-16, %esp
>        movl    $.LC0, -8(%ebp)
>        leal    -8(%ebp), %edx
>        movl    $0, 4(%edx)
>        movl    -8(%ebp), %eax
>        movl    %eax, (%esp)
>        movl    %edx, 4(%esp)
>        movl    $0, 8(%esp)
>        call    execve
>        movl    $0, %eax
>        movl    %ebp, %esp
>        popl    %ebp
>        ret
>
>This doesn't look that much like your code.  Without the -O (optimize)
>flag  I get:
>
>$ cc  -pipe -mcpu=pentiumpro -S exec.c
>$ cat exec.s
>...
>main:
>        pushl   %ebp
>        movl    %esp, %ebp
>        subl    $24, %esp
>        andl    $-16, %esp
>        movl    $0, %eax
>        subl    %eax, %esp
>        movl    $.LC0, -8(%ebp)
>
>So yes, it looks as if you're not optimizing.
>
>Greg
>--
>Note: I discard all HTML mail unseen.
>Finger grog@FreeBSD.org for PGP public key.
>See complete headers for address and phone numbers.
>  
>
hello greg..
yes.. it does the difference..
thanks a lot for your help..

Chungwei



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?40492116.80607>