CS 61C (Fall 2007)

Homework Assignment 4

Three exercises, submit together as "hw4".  Due 2:45pm before lecture 9/26/2007.

Goals

This homework is intended to provide you with practice writing and reading MIPS assembly language, a skill which will help you immensely in project 2

Background reading

Submission instructions

Submit your solution by creating a directory named hw4 that contains files named hw4q1.txt, hw4q2.s, and hw4q3.s. (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 hw4.

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

Problem 1 (1 point)

P&H Exercise 2.30 (page 150), slightly amended.

The following code fragment, online in ~cs61c/files/hw/4/hw4q1.txt, 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:

Add your comments to the file hw4q1.txt, and submit this modified file.

Problem 2 (2 points)

P&H Exercise 2.15 (page 149), slightly amended.

Implement the following C code directly in MIPS, assuming that set_array is the first function called:

	int i;
	
	void set_array (int num) {
		int array[10];
		for (i=0; i<10; i++){
			array[i] = compare (num, i);
		}
	}
	
	int compare (int a, int b) {
		if (sub (a, b) >= 0)
			return 1;
		else
			return 0;
	}
	
	int sub (int a, int b) {
		return a-b;
	}

Retain the structure of the existing code; i.e. don't optimize any function calls, and don't worry about the fact that array is inaccessible outside of set_array (which makes the whole all the code compute an unused result).

Your solution should adhere to MIPS conventions for subroutine linkage and register use. Be sure to handle the stack pointer appropriately. (Don't worry about $fp, the frame pointer.) The array declared in set_array is allocated on the stack, and i corresponds to $s0. Draw the status of the stack before calling set_array and during each function call. Indicate the names of registers and variables stored on the stack and mark the location of $sp.

Save your answer in a file named hw4q2.s.

Problem 3 (1 point)

Write the following VectorSum function as a MIPS procedure call:

	struct vector {
	    int x;
	    int y;
	};
	
	void VectorSum (struct vector* vp, struct vector** vectors, int len) {
	       ...
	}

The function VectorSum takes an array of pointers to struct vector values (vector is an array of pointers) and calculates the sum of all the vectors. The parameter len is the length of the array. The sum of two vectors (x1,y1) and (x2,y2) is simply (x1+x2, y1+y2). The resulting vector should be stored in the struct vector pointed to by vp.

You should assume that the integer x is stored at a lower memory address than y. Therefore, if the address of a struct vector is 0x50000, then the member x is located at 0x50000, and the member y is located at 0x50004.

Your solution should adhere to MIPS conventions for subroutine linkage and register use. Save your answer in a file named hw4q3.s.