Instructions: CSCE 212: Exam 1 Fall 2009
Instructions: CSCE 212: Exam 1 Fall 2009
Fall 2009
Instructions
This is a CLOSED BOOK and CLOSED NOTES quiz. However, you may use
calculators, scratch paper, and the green MIPS reference card from your textbook. Ask
the instructor if you have any questions. Good luck!
1. (20 points) Write a snippet of MIPS code that reverses the bit sequence stored in
register $s0. You may use the following algorithm (substitute symbolic registers with
actual registers):
1)
2)
3)
4)
5)
6)
7)
8)
li $t0,0
li $t1,0
loop: srlv $t2,$s0,$t0
sll $t2,$t2,31
srlv $t2,$t2,$t0
or $t1,$t1,$t2
addi $t0,$t0,1
blt $t0,32,loop
2. (10 points) Assume theres a pseudo-instruction named die, which causes the
machine to lock up by creating an infinite loop. Show how this instruction could be
translated (using assembly code).
loop: beq $0,$0,loop
3. (20 points) Write a MIPS subroutine called div8 that returns 1 in register $v0 if the
argument in register $a0 is evenly divisible by 8 (and 0 otherwise). Hint: the check
can be performed using a shift instruction and a beqz or bnez instruction.
The subroutine must not change any of the callers registers (except for $v0),
meaning that it must use the stack to save the original values of any registers that it
uses.
div8:
skip:
addi $sp,$sp,-4
sw $s0,0($sp)
li $v0,1
sll $s0,$a0,29
beqz $s0,skip
li $v0,0
lw $s0,0($sp)
addi $sp,$sp,4
jr $ra