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



"Zheng Da" <spamtrap@xxxxxxxxxx> 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

This question comes up approximately once a week.

There have been many mumbled attempts at an explanation for this, usually
involving vague words about stack alignment. In my 15 years here, no one
has ever produced a completely satisfactory explanation of this odd
behavior on gcc's part.
--
- Tim Roberts, timr@xxxxxxxxx
Providenza & Boekelheide, Inc.

.