All pastes #991047 Raw Edit

Miscellany

public text v1 · immutable
#991047 ·published 2008-04-20 18:01 UTC
rendered paste body
This is the driver 


#include <stdio.h>

int main(void);
int Factorial(int);

int main(void)
{
   int i;
   int number;
   int answer;

   int fib_subscript;
   printf("Please enter a number: ");
   scanf("%d",&number);
   answer = Factorial(number);
   printf("The factorial of %d is %d.\n", number, answer);
}
/* end driver code */


This is what the asm code does 


/* begin assignment specification code lab6asm.s */
/* Convert the procedure exactly as given using a local variable
   where needed. */
int Factorial(int n)
{
	if(n == 0 || n == 1)
		return 1;
	else
		return n * Factorial(n-1);
}

--- This is the asm code for the C code above 

        .globl  Factorial
        .type   Factorial,@function
Factorial:
        /* put code here */

        /* prolog */
        pushl   %ebp
        pushl   %ebx
        movl    %esp, %ebp
        subl    $4, %esp

        /* get 'number' (the argument) from stack */
        movl    12(%ebp), %ecx
        
        /* ecx is the argument now */
        cmp     $0, %ecx
        je      ret_one
        cmp     $1, %ecx
        je      ret_one

        /* save the variable locally so that after the function call we 
         * can perform our math */
        movl    %ecx, -4(%ebp)

        /* decrement then call */
        decl    %ecx
        pushl   %ecx
        call Factorial

        movl    -4(%ebp), %ecx
        /* the return was placed in %eax so we just multiply by our local */
        mull    %ecx
        jmp     return
        
ret_one:
        movl    $1, %eax

return:
        /* epilog */
        movl %ebp, %esp
        popl %ebx
        popl %ebp
        ret