CS3421 Exam 1 Solutions

  1. 1) fetch the next instruction
    2) increment the program counter
    3) decode the instruction
    4) execute the instruction


  2. add	$s2,$a0,$zero  (there are other solutions too)
    
    addi	$t4,$zero,-1   (there are other solutions too)
    


  3. $t1 = 0xd02
    $t2 = 0xff9c0f4d
    


  4. 44


  5. One possible solution. There are many.
    	la	$t0,info		# load address of beginning of array
    loop:
    	lw	$t1,0($t0)		# load next element of info
    	blt	$t1,$zero,neg		# out of loop if negative
    	addi	$t1,$t1,4		# advance to next element
    	b       loop                    # check next element
    neg:
    	la	$t1,info		# get address of beginning of array
    	sub	$s0,$t0,$t1		# subtract address of negative element
    	sra     $s0,$s0,2		# divide by 4
    	# at this point $s0 has the value
    


  6. One possible solution. There are many.
    # Parameter values are in $a0, $a1 and $a2, return value goes into $v0.
    # Since no $s registers are used and there are no jal's in the code, no
    # registers need to be saved.
    which:
    	# assume $a0 is the largest
    	move    $t0,$a0
    	li	$v0,0
    
    	# if $a1 is larger, record it
    	bge     $a0,$a1,skip1
    	move    $t0,$a1
    	li	$v0,1
    
    skip1:
    	# if $a2 is larger yet, record it
    	bge	$t0,$a2,skip2
    	move    $t0,$a2
    	li	$v0,2
    
    skip2:
    	# return
    	jr	$ra
    


  7. When the procedure contains a jal instruction.


  8. 000000 10100 00010 00111 00000 100000
    

    000100 00100 00101 1111111111111011


  9. sll   $a2,$t1,16
    

    lw $t9,17($sp)