
CS 301, Fall 2004


Assignment #6: 10 Points.

Due Date: Wednesday, 11/03/04.


(2)  1.	Create an assembly language version of the integer Factorial() 
	function from HW#5, Problem 2.  Compile and test your function 
	using the main() function below.  No modifications are allowed 
	to main().

		#include <stdio.h>
		extern int Factorial(int);
		main()		// driver program for factorial
		{ int N; 
		  printf("Enter N>0: "); scanf("%d",&N);
		  printf("%d! = %d\n",N,Factorial(N));
		}

	Compile main() with the Factorial() function using the command:

		gcc main.c factorial.s

	and type ./a.out to run the executable named a.out.  Test your 
	program on values of N which give both valid and invalid results 
	for N!.  Hint: Use the C compiler to create the assembly code 
	for the factorial function.**

(3)  2.	Modify the assembly code from Problem #1 to return 0 from the 
	Factorial() function if the factorial calculation exceeds the maximum 
	size of a 32 bit integer.  Test your program with values of N which 
	give both valid and invalid results for N!.  Hint: Modify the code
	to perform 64-bit multiplication and test %edx for nonzero.**

(3)  3. Assume that %eax and %ebx contain signed data.  Determine the
        comparison and conditional jump instructions to test the following
        conditions and execute a jump to L when the condition is true:

	(a) Is %eax less than or equal to %ebx?
	(b) Is %eax greater than %ebx and %ebx greater than zero?

(2)  4. Explain how many times the "body" of each of the following loops
        is executed:

        (a)             movl $3, %ecx
                .L4     ...body...
                        decl %ecx
                        jg .L4

        (b)             movl $1, %ecx
                .L5:    cmpl $5, %ecx
                        jge .L6
                        ...body...
                        sall $1, %ecx
                        jmp .L5
                .L6


      **For full credit, turn in a well commented source program listing and
	verbatim output from your computer runs, including the commands to
	compile and test the program.



