Department of Electrical Engineering and Computer Science
EECS
61C, Summer 2004
Lab
2-1
Goals
These lab exercises are intended
to give you practice working with the malloc function and give you an introduction to MIPS.
We will be using a MIPS simulator called Spim. Bring your P&H textbook with you to
lab because it contains some documentation for Spim (starting around page A-40).
Exercise
2: Simple MIPS program.
Write a piece of
MIPS code that takes values in $s0 and $s1 and puts the value fib(i) into register
$ti (for 0 <= i <= 7). For example, if $s0 contains the value 1 and $s1 contains the
value 1, then after running your code, registers $t0 through $t7 should contain these
values:
$t0 = 2 ($s0 + $s1)
$t1 = 3 ($s1 + $t0)
$t2 = 5 ($t0 + $t1)
...
$t7 = 55 ($t5 + $t6)
You should not set the value of $s0 or $s1 in your
code. Learn how to set it manually within XSpim. Assembly programs are written in files
with a .s extension. The program must start with the label "__start:" (two underscores)
and end with the instruction "done". In order to run your program, run "spim" or "xspim" and consult P&H for commands in Spim.
Using
XSpim
XSpim is a version of Spim that has
an X-interface, as opposed to Spim's console interface. To run XSpim at home, you will need
to download support for X-interface (such as Exceed for Windows). To run XSpim in the labs,
simply run "xspim". To run your program in XSpim, use the "Load" button to load your .s file.
You should see the instructions of your .s file appear in the "Text Segments" frame. Set the
$s0 register using the "Set Value" button. Then use the "Run" or "Step" button to execute
your program. You should see the registers change in the top frame.
Exercise
2: Debugging in Spim.
The program
~cs61c/labs/lab2-1/lab2-1.s
is supposed to implement the C code below. The program just copies
the given array from source to dest but stops and does not copy when it finds a 0
(the last element). The .s file contains (at least) one bug. Load the MIPS program
into Spim or XSpim, set breakpoints to see what happens within the loop, and correct
the bug(s). Copy lab2-1.s into your home directory.
int *sourceptr;
int *destptr;
int source[7] = {3, 1, 4, 1, 5, 9, 0};
int dest[7];
sourceptr = source;
destptr = dest;
while (*sourceptr != 0) {
*destptr = *sourceptr;
sourceptr++;
destptr++;
}
}
The file lab2-1.s contains
the MIPS program below. Assume that $s0 contains the address of the source array and $s1 contains the
address of the dest array. Set a breakpoint at the address corresponding to the instruction labeled
by "loop". Run the program and look at the values of the registers and memory. Modify the code to fix
the bugs and run it.
lw $t0, $0($s0)
sw $t0, $0($s1)
addiu $s0, $s0, 1
addiu $s1, $s1, 1
bne $t0, $0, loop