Re: Why gcc translate a c program into assemble as follow




Zheng Da wrote:
> A C function is like below:
> void echo()
> {
> char buf[4];
> gets(buf);
> }
> I use gcc compile it with the option -O2 -S, and get the following
> assemble program:
> echo:
> pushl %ebp
> movl %esp,%ebp
> leal -4(%ebp),%eax
> subl $20,%esp ##why allocate 20 bytes on stack? The variable in C
> function is only 4 bytes
> pushl %eax
> call gets
> leave
> ret

Indeed, why even bother setting up a stack frame with EBP? Try -O3 or
the various options that tell GCC to use ESP to address the stack
frame.

BTW, the compiler may be doing this just so it can align the stack on a
16-byte boundary later on, if it needs to do so for certain (e.g., SSE)
data structures. True, this function doesn't do that, but the compiler
may not have figured this out by the time it emits the subl
instruction, and wasting 12 bytes on the stack (reclaimed on return)
probably doesn't seem to be that big of a deal to the compiler's
designers.
Cheers,
Randy Hyde

.



Relevant Pages

  • Re: Do buffers always start with the lowest memory address being the first element?
    ... > The C standard does not assume a downward-growing stack, ... > an upward-growing stack. ... C allows but does not require that the array produced ... > machine depends on both the C compiler and the machine. ...
    (comp.lang.c)
  • Re: switch statement, was compiler, status...
    ... Primary Register, Secondary Register, ... Stack, and abit of storage does it. ... This version of Small-C is copyrighted as a revision to Ron Cain's ... Croatia) is "Calculator Compiler" by Senko Rasik. ...
    (alt.lang.asm)
  • Re: subroutine stack and C machine model
    ... On a typical system the stack on entry to the function looks like this ... balance to specify the order of evaluation). ... compiler developer to find out when code can be moved around. ... I think the Standards people used compiler optimization as an excuse ...
    (comp.lang.c)
  • Re: back online again...
    ... really "acceptable") convention. ... stack machine, but the design ... target with my compiler (may need a different name for this though, ... most of this code is not likely to ever go to object files anyways... ...
    (alt.lang.asm)
  • Re: 2.6.25-git2: BUG: unable to handle kernel paging request at ffffffffffffffff
    ... since the compiler is totally free to spill and reload the local variable ... So forget about the prefetch, ... variable onto the stack, since it did that volatime memory access through ... the insane "store and immediately reload from ...
    (Linux-Kernel)