CS 61CL homework assignment 4

The two exercises below, together with your peer evaluation of homework 3. should be submitted to Expertiza by 11:59 on Friday evening, February 13.

Exercise 1

The following code fragment, online in ~cs61cl/code/hw4q1.s, processes two arrays and produces an important value in register $v0. Assume that each array is indexed starting at 0, that the base addresses of the arrays are stored in $a0 and $a1 respectively, and their sizes (in words) are stored in $a2 and $a3, respectively. Add comments to the code that describe what the code is doing and what role each register is playing (a comment per line wouldn't be a bad ratio). Also describe in one sentence what this code does. Specifically, when control reaches the instruction labeled done, what will be in $v0?

	sll	$a2, $a2, 2
	sll	$a3, $a3, 2
	add	$v0, $zero, $zero
	add	$t0, $zero, $zero
outer:	add	$t4, $a0, $t0
	lw	$t4, 0($t4)
	add	$t1, $zero, $zero
inner:	add	$t3, $a1, $t1
	lw	$t3, 0($t3)
	bne	$t3, $t4, skip
	addi	$v0, $v0, 1
skip:	addi	$t1, $t1, 4
	bne	$t1, $a3, inner
	addi	$t0, $t0, 4
	bne	$t0, $a2, outer
done:

Exercise 2

Design and implement a MIPS assembly language version of the adding machine from homework assignment 1. The specifications are the same for that assignment. Input to the program will represent integer values, typed one per line without leading or trailing white space. (Don't worry about incorrectly formatted input.) Input should be handled as follows:

Your solution should be well commented—one comment per line would not be overkill—and should use white space appropriately to delimit logical sections of the program.

You may use syscall to read an entire integer from the input; unlike in homework assignment 1, you don't need to build the integer out of its constituent digits. Also, MARS seems to echo user input, so it will appear interspersed with your subtotal/total messages. Print a newline character at the end of each of your output lines to improve the readability of the output. One last thing: only the $s registers are safe from being trashed by syscall.