Goals
These exercises are intended to give you more practice with function calls and bit manipulation in MIPS.
Setup
Copy the directory ~cs61c/labs/04 to an appropriate directory under your home directory.
Exercises
Exercise 1: A short MIPS program
Write a piece of MIPS code from scratch that, given values in $s0 and $s1, accomplishes the following:
$t0 = $s0 $t1 = $s1 $t2 = $t0 + $t1 $t3 = $t1 + $t2 ... $t7 = $t5 + $t6
In other words, for each register from $t2 to $t7, store the sum of the previous two $t# register values. The $s0 and $s1 registers contain the initial values. Set the values of $s0 and $s1 manually with MARS instead of in your code. Finally, have your code print out the final value of $t7 as an integer (Hint: syscall).
Save your code in a file called lab4_ex1.s. Don't forget the "main:" label and to end your program with a "syscall 10"!
Checkoff
- Show lab4_ex1.s to your TA, who will give you numbers to enter into $s0 and $s1. Your code should print $t7 and exit normally.
Exercise 2
Write two versions of a function named first1pos
(starting from first1pos.s) that, given a value in $a0,
returns in $v0 the position of the leftmost bit that is set to 1 in the word in $a0.
If $a0 contains 0, store -1 in $v0.
You are allowed to modify $a0 in the process of finding this position.
Positions range from 0 (the rightmost bit) to 31 (the sign bit).
The first version repeatedly shifts $a0 to the left, checking the sign bit at each shift. The second version starts a mask at 0x80000000 and repeatedly shifts it right to check each bit in $a0.
Checkoff
- Show your TA both versions of the function and its test run.