CS 61C Homework 2-2

Background reading

Lecture notes from 7/1 and 7/2 and P&H: Chapter 3.5, 3.6

Administrative requirements

Submit your solution online by 8pm on the 4th of July, 2004. Do this by creating a directory named hw2-2 that contains the files hw2-2.txt (put your answers to problems 1 and 2 in here), and lowerCase.s (problem 3 solution). From within that directory, type:

submit hw2-2

This is not a partnership assignment -- hand in your own work, and don't collaborate with anyone else.

Problem 1: Warm Up

Given the declaration for a struct point:
    struct point {
	union { int i; double d; } x;	
	union { int I; double d; } y;
    } p[10];

Consider the following C code:
    int i;
    for(i=0; i<10; i++) {
	p[i].x.i = 0;
	p[i].y.i = 0;
    }
Copy and paste the following piece of MIPS code into your hw2-2.txt file. Fill in the blanks in the following translation of this C code into MIPS:
        la $8, p			# $8 points to p[0]
        addi $9, $8, ____		# $9 points to p[10]
    L1: bge $8, $9, L2		# done if past end of array
        sw ___, 0($8)		# p[i].x.i = 0
        sw ___, ___($8)		# p[i].y.i = 0
        addi $8, ____, ____	# i++
        b L1
    L2:

Problem 2: Reverse Engineering

The following piece of MIPS code is the translation of a function foo written in C. You are given the function prototype of foo in C. Your task is to disassemble the MIPS code back to C. Copy the C prototype tp ypur hw2-2.txt file and fill in the rest of the code.
    1  foo:
    2      bnez $a0, L
    3      beqz $a1, exit1
    4      li $v0, 0
    5      jr $ra
    6
    7  exit1:
    8      li $v0, 1
    9      jr $ra
    10
    11 L:
    12     addiu $sp, $sp, -16
    13     sw $ra, 12($sp)
    14     sw $a0, 8($sp)
    15     sw $a1, 4($sp)
    16
    17     addiu $a0, $a0, -1
    18     jal foo
    19     sw $v0, 0($sp)
    20     lw $a0, 8($sp)
    21     lw $a1, 4($sp)
    22     addiu $a0, $a0, -1
    23     addiu $a1, $a1, -1
    24     jal foo
    25     lw $t0, 0($sp)
    26     addu $v0, $v0, $t0
    27
    28     lw $ra, 12($sp)
    29     addiu $sp, $sp, 16
    30     jr $ra

 
    int foo(int x, int y) {

        if (x==0 && y==0)

        /* Please fill in the rest of this code */
        
        return 1;
    }

Problem 3: Hands on MIPS

Write a MIPS assembly program that converts upper case characters in a string to lowercase characters. Put the string in the .data portion of your code. Save your code in lowerCase.s file.