Summer 2010 CS61C Homework 3 : MIPS & C

Due Tuesday, July 6th, 2010 @ 11:59:59pm
NOTE: This originally had an incorrect date of the 7th


This assignment will give you practice of C and MIPS. By the end of the homework assignment, you should be able to translate C to MIPS code and back with ease.

Background Reading

Submitting your Solutions

Submit your solution by creating a directory named hw3 that contains files named hw3.txt. (Note that capitalization matters in file names; the submission program will not accept your submission if your file names differ at all from those specified.) From within that directory, type "submit hw3".

NOTE: This is not a partnership assignment! Hand in your own work.

Problem 1

The following logical operations are not included in the MIPS instruction set. Answer the subsequent questions for both operations a and b .
 andn $t1, $t2, $t3 
// bit-wise AND of $t2, !$t3
 xnor $t1, $t2, $t3 
// bit-wise exclusive-NOR of $t2, $t3 (ie. complement of exclusive-OR)

, what is the resulting value in

Implement the operations using a minimal set of MIPS instructions.

Problem 2

Consider the following C statements.
A = ++B & 4*C[0] 
A = A ? A^B : C[2] 
while (A < 10){
  C[A] = B + A++;
A += (C[4] = (C[5] + B)/2) 

Assume that the C variables are mapped to the following registers:
 A => $s0, B => $s1, C => $s2
Write a sequence of MIPS instructions for each C statement.
Hint: If you are not sure what the code does, compile and run it!

Problem 3

Do question 2.18.5 (both part a and b) from the P&H book

Consider the following MIPS assembly code fragments:

		addi  $t1, $0, 100
LOOP:	lw    $s1, 0($s0)
		add   $s2, $s2, $s1
		addi  $s0, $s0, 4
		subi  $tl, $tl, 1
		bne   $t1, $0, LOOP
		addi  $t1, $s0, 400
LOOP:	lw    $s1, 0($s0)
		add   $s2, $s2, $s1
		lw    $s1, 4($s0)
		add   $s2, $s2, $s1
		addi  $s0, $s0, 8
		bne   $t1, $s0, LOOP

Translate the loops above into C. Assume that the C-level integer
is held in register
holds the C-level integer called
, and
holds the base address of the integer

Problem 4

Assume that there is no multiplication in the MIPS instruction set. Let's implement a function
 void square(int* x) 
, which squares the value stored in
. We won't worry about overflow in this exercise.

First, write the C function. Make sure it does not use the multiplication operation (
), and it is correct for all possible input values. Hint: You will need to use loops.

Write the corresponding MIPS instructions. Assume that
is mapped to the argument register
. Feel free to use the temporary registers
$t0 - $t7