

CS301, Fall 2004


Assignment #5: 10 Points.

Due Date: Monday, 10/18/04.


	Read Chapter 3, sections 3.4-3.7.

(2)  1.	Suppose register %eax holds the value x and %ecx holds the value y.  
	Give the value that will be stored in %edx by the following IA32
	instructions:

	 	leal 6(%eax,%eax), %edx     ; %edx =
		leal 0xA(%eax,%ecx,4), %edx ; %edx = 

(3)  2.	Compile the following C factorial function using the gcc compiler
	to obtain the assembly code for the function in a file named 
	factorial.s.  

		unsigned int Factorial(unsigned int N)
		{ unsigned int I, Nfac=1;
		  for (I=1;I<=N;I++) Nfac *= I;
		  return Nfac;
		}

	Refer to the assembly code to answer the following questions:
	(a) Where are the variables I and Nfac stored during execution of 
	the function?  (b) Explain how to modify the assembly code to use
	the mull instruction to compute the full 64-bit product of Nfac*I. 
	(c) Explain how to tell if the product in (b) is larger than 32 bits.
	Extra Credit: Show how to modify the assembly code to return 0 from 
	the function if the factorial calculation exceeds the maximum size 
	of a 32 bit unsigned integer.**

(2)  3.	Compile the hello.c program in Figure 1.1 to obtain the assembly
	code for the program in a file named hello.s.  Draw a diagram of
	the contents of the stack immediately before the call to printf().
	The targets of any pointers on the stack should also be shown.**

(1)  4.	Specify a single IA32 instruction which produces the same result 
	in location 8(%ebp) as the following sequence of instructions:

		movl 8(%ebp), %eax	
		cltd
		divl $16		
		mull $16		
		subl  %eax, 8(%ebp)	

(2)  5.	Problem 3.32 (A,B).


      **For full credit, turn in well commented program listing and
	output from your computer run.



