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

## Goals

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.

• P&H (4th): Sections 2.7, 2.8. 2.10

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 .
 a. ` andn \$t1, \$t2, \$t3 ` // bit-wise AND of \$t2, !\$t3 b. ` xnor \$t1, \$t2, \$t3 ` // bit-wise exclusive-NOR of \$t2, \$t3 (ie. complement of exclusive-OR)

1.1
`  `
If
`\$t2=0x0FAA85AE `
and
`\$t3=0xCCDF003C`
, what is the resulting value in
`\$t1`
?

1.2
`  `
Implement the operations using a minimal set of MIPS instructions.

## Problem 2

Consider the following C statements.
 a. `A = ++B & 4*C ` b. `A = A ? A^B : C ` c. ``` while (A < 10){ C[A] = B + A++; }``` d. `A += (C = (C + 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:

 a. ``` 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 ``` b. ``` 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
`i`
is held in register
`\$t1`
,
`\$s2`
holds the C-level integer called
`result`
, and
`\$s0`
holds the base address of the integer
`MemArray`
.

## 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
`x`
. We won't worry about overflow in this exercise.

4.1
`  `
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.

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