This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

extra space on stack allocated for recursive functions


I am using gcc version 3.2.2 on Linux O/S.  I noticed
that when I call a recursive function there is extra
space allocated on the stack.  Can you tell me what
the intended use is for the extra space.

the following "C" factorial code:

int factorial (int n)
{
  int r = 0;
  if (n <= 0)
  {
    r = 1;
  }
  else
  {
    r = n * factorial(n-1);
  }
  return r;
}

int main(int argc, char** argv)
{
  int n = 3;
  int f;
  f = factorial(n);
  return 0;
}

Generates the following assembly language code:

	.file	"factorial.c"
	.text
	.p2align 2,,3
.globl factorial
	.type	factorial,@function
factorial:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ebx
	movl	8(%ebp), %ebx
	testl	%ebx, %ebx
	movl	$1, %eax
	jle	.L3
	subl	$12, %esp
	leal	-1(%ebx), %eax
	pushl	%eax
	call	factorial
	imull	%ebx, %eax
	addl	$16, %esp
.L3:
	movl	-4(%ebp), %ebx
	leave
	ret
.Lfe1:
	.size	factorial,.Lfe1-factorial
	.p2align 2,,3
.globl main
	.type	main,@function
main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	andl	$-16, %esp
	subl	$12, %esp
	pushl	$3
	call	factorial
	xorl	%eax, %eax
	leave
	ret
.Lfe2:
	.size	main,.Lfe2-main
	.ident	"GCC: (GNU) 3.2.2 20030222 (Red Hat Linux
3.2.2-5)"



__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]