Computer Science 361.01

Fall 2003

Homework 8: Due Thursday, November 6th

Please do individually and begin early.

I will not answer questions after Wednesday the 5th  at 3PM.

 

1. (15 points) Translate the following C++ program to Pep/7 assembly language.  Run and test with various inputs.

 

     int num, largest = 0;

     int k;

 

int max(int x, int y) {

        if (x > y)

          return x;

        else

          return y;

}

main() {

       for (k = 1;  k <= 10; k++) {

          cin >> num;

          largest = max(largest, num);

       }

       cout << largest;   

}

          

2. (15 points) Translate the C++ program found at  FastMult.cpp  to Pep/7 assembly language.  Run and test the “fast multiplication” function with various inputs.

 

   3. (20 points)  Translate the following to Pep/7 assembly language.  This means the

   Pep/7 numBits function must itself be recursive.  Test and run with various inputs.

 

int number;

 

int Abs(int x) {

      if (x < 0)

        x = -x;

 

      return x;

}

 

int numBits(int n) { // n assumed non-negative

      if (n <= 1)

            return 1;

      else

            return 1 + numBits(n/2);

}

main() {

      cin >> number;

      number = Abs(number);  // assures a non-negative

      cout << numBits(number);

}