CS 35101 – Computer Architecture – Spring 2006 - Homework # 2 Due Friday 2/17

Name ______

1. [50 points] This C version of acasestatement is called aswitchstatement.The following C++ code chooses among four alternatives depending onwhetherkhas the value 0, 1, 2, or 3.

#include <iostream>

using std::cout;

using std::cin;

int main()

{

int A = 18;

int B = 11;

int C = 0;

int k = 0;

cout < "Please enter integer value from 0 to 3 ";

cin > k;

switch (k) {

case 0: C = A + B; /* k = 0 */

cout < “Case 0: C = A + B “;

break;

case 1: C = A - B; /* k = 1 */

cout < “Case 1: C = A - B “;

break;

case 2: C = A & B; /* k = 2 */

cout < “Case 2: C = A & B “;

break;

case 3: C = A | B; /* k = 3 */

cout < “Case 3: C = A | B “;

break;

default: cout < “invalid choice “;

break;

}// end switch

}// end main

Use the template below and insert the corresponding MIPS code for the switch statement.Step through the code for all 4 valid cases. Try an invalid case also. [Hint: use theswitchvariablekto index the jump address table, and then jump viathe value loaded. Include if statements to make sure k matches one of the cases (0≤k≤3);if not, the code exits theswitchstatement.] Assume Case0, Case1 etc in the .data section are the addresses to jump to for Case 0, Case 1 etc. See example in text and online lecture notes.

switch program template

2. [20 points] While Loop

Implement the following C++ program which uses a while loop.

Use the assembler source program template . While Loop Template

#include <iostream>

using std::cout;

using std::cin;

int main()

{

int arrayA[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int arrayB[10];

int i = 0;

while ( i < 10 )

{

arrayB[i] = arrayA[i] + 48;

cout < arrayB[i] < “ “;

i = i + 1;

} // end of while loop

cout < endl;

} // end of program

3. [15 points ] Simple function ( “leaf procedure”)

Implement the following C++ program which has a simple function call

Use the assembler source program template simple function call

int main()

{

int n = 45;

int nsq = 0;

nsq = square(n);

return;

} // end main

int square(int n)

{

return n*n;

} // end square

4.[15points] Recursive function

Implement the following C++ program which has a recursive function.

Use the assembler source program template recursivefunction call

Start of C++ version

/*********************************************************

* Recursively find gcd. *

* Parameter lg must be >= than sm *

* Note how the return in the else passes the result *

* back through the sequence of recursive calls. *

********************************************************/

int gcd(int lg, int sm)

{

int remainder = lg % sm;

if (remainder == 0)

return sm;

else

{

return gcd(sm, remainder);

}

}

#include <iostream>

using std::cout;

using std::cin;

int main()

{

int a_;

int b_;

int answer;

cout < "Welcome to Dr. Durand's GCD computer \n";

cout < "Please enter two positive integers => ";

cin > a_ > b_;

if (a_ > b_)

answer = gcd(a_,b_);

else

answer = gcd(b_,a_);

cout < "The GCD of (" < a_ < "," < b_ < ") is " < answer < "\n";

}

end of C++ version