SOLUTION TO MIDTERM #2:
-----------------------

1.		Value	Array element for column-major order
	A	0x42		(0,0)
		0x53		(1,0)
		0x44		(2,0)
		0x58		(3,0)
		0x44		(0,1)
		0x45		(1,1)
		0x46		(2,1)
		0x45		(3,1)
		0x41		(0,2)
		0x45		(1,2)
		0x45		(2,2)
		0x4d		(3,2)


		0	1	2
	-------------------------
	0 |   (0,0)   (0,1)   (0,2)
	  |
	1 |   (1,0)   (1,1)   (1,2)
	  |
	2 |   (2,0)   (2,1)   (2,2)
	  |
	3 |   (3,0)   (3,1)   (3,2)
	-------------------------

   (a) A[0,2] = 0x41
   (b) A[1,1] = 0x45
   (c) A[2,2] = 0x45
   (d) A[3,0] = 0x58
   (e) A[3,2] = 0x4d

----------------------------------------------------------------------------

2. Recursive implementation of

	factorial(0) = 1
   	factorial(x) = x * factorial (x-1)


		move	$19, $4	# temp. storage for number x contained in $4
		move	$2, 1	# function returns value in $2
		jal	factorial
		.
		.
		.
	end:	done


		# procedure factorial
   factorial:	sw	$31, 0($sp)	# store return addr on stack
		addi	$sp, $sp, -4	# decrement stack pointer

		move	$20, $19	# save copy of $19 in $20
	if:	add	$19, $19, -1	# decrement $19
		blez	$19, endif	# if $19<=0, we are done
		jal	factorial	# else, recursively call factorial

	endif:	mul	$2, $2, $20	# multiply current number with
					# existing product
		sub	$20, $20, 1
		add	$sp, $sp, 4	# pop stack
		lw	$31, 0($sp)	# load return addr
		jr	$31		# return from procedure

----------------------------------------------------------------------------

3. (a) 	MAL:	lw	$15, var	# addr of var is 0x00205044

	TAL:	lui	$15, 0x0020
		lw	$15, 0x5044($15)


   (b)	MAL:	sb	$18, var	# the addr of var is 0x00338040

	TAL:	lui	$1, 0x0033	# use register $1 since we do not want
					# to overwrite value in $18
		ori	$1, $1, 0x8040
		sb	$18, 0($1)

	Note that you should use a different register (not $18 itself)
	to do the lui and ori operations above, in order to avoid overwriting
	the value in $18.


   (c)	First way:
		sub	$1, $18, $3
		bltz	$1, br_label

	Second way:
		sub	$1, $3, $18
		bgtz	$1, br_label


   (d) First error: The addi instructions must be replaced with "add".
		(There is no addi instruction in MAL--it is only in TAL).

       Second error: The sw instruction in the end for popping the return
		address should be the lw load instruction

       This code actually contains a third (more serious) error. The jump
	  "j proc" instruction should be replaced with the "jal proc"
	  instruction, since "j" does NOT automatically save the return
	  address in register $31.

----------------------------------------------------------------------------

4.	Memory address			  M/C Code

	   0x00c01114   		0x 15740001
   	   0x00c01118   		0x 8fac0004
   	   0x00c0111c   		0x 08300445

----------------------------------------------------------------------------


5. (a)	$3:	8
	$4:	8
	$5:	0x0000 000f
	$6:	0x0000 000c
	$7:	0x0000 0080

   (b) The displacement field X in lw $4, X($5) is 16-bits long.
       Thus, the largest value of X can be 2^15 (32K) and the smallest
       value is -2^15 (-32K).