Re: Why gcc translate a c program into assemble as follow
- From: "randyhyde@xxxxxxxxxxxxx" <spamtrap@xxxxxxxxxx>
- Date: Wed, 26 Oct 2005 20:16:45 +0000 (UTC)
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
.
- References:
- Why gcc translate a c program into assemble as follow
- From: Zheng Da
- Why gcc translate a c program into assemble as follow
- Prev by Date: Re: improve strlen
- Next by Date: Combining two MMX registers into one SSE register?
- Previous by thread: Why gcc translate a c program into assemble as follow
- Next by thread: Re: Why gcc translate a c program into assemble as follow
- Index(es):
Relevant Pages
|