Homework activities

By Monday evening February 23 at 11:59pm, submit solutions to the three assignments described below.


Homework 5, exercise 1

Implement the following C code directly in MIPS and verify that it works in MARS.

	
        
	void set_array (int array[], int num) {
		int i;
		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;
	}

        int main() {
		int i;
		int Carray[10];
		for (i=0; i<10; i++) Carray[i] = i;
		set_array(Carray, 5);
	}

Retain the structure of the existing code; i.e. don't optimize any function calls. You will need a few instructions at the top that call main and terminate after the return. These you can find in the lab examples.

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 main is allocated on the stack, and i in set_array corresponds to $s0.

Submit your solution to Expertiza as "C-to-MIPS translation".


Homework 5, exercise 2

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 a one-dimensional array of pointers to struct vector values and calculates the sum of all the vectors. The parameter len is the number of elements in 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. Submit your solution to Expertiza as "vector sum".


Tower of Hanoi (homework 5, exercise 3)

The Tower of Hanoi problem is well known. It's described here. It has an elegant recursive solution, which you'll implement for this homework exercise.

Translate the following C program (in ~cs61cl/code/hanoi.c) to MIPS assembly language in the file hanoi.s. The program assumes there are pegs labeled A, B, and C. It prints moves of the form x y, where x and y are pegs. For example, the move AB means move the top disk from the peg labeled A to the peg labeled B.

Adhere to previously covered conventions for managing registers and the stack. Translate each call to putchar into a call to syscall; see the MARS online help for further information. Note that syscall, like any other procedure, may trash some registers.

void main ( ) {
	Hanoi (5, 'A', 'B', 'C');
}

void Hanoi (int num, char from, char to, char other) {
	if (num == 0) {
		return;
	}
	Hanoi (num-1, from, other, to);
	putchar (from);
	putchar (to);
	putchar ('\n');
	Hanoi (num-1, other, to, from);
}

Submit your solution to Expertiza as "tower of hanoi".