Lab 7 - Branch Prediction

Due: Wednesday at noon

The purpose of this lab is to profile the behavior of branches for both loops and if-else statements and to see how accurate the basic branch prediction schemes will be for the code.

You may work in groups on this lab. Make sure all lab partner names are on the writeup and have one lab partner submit the writeup via Moodle. Those not submitting the writeup via Moodle should put the name of their lab partners in the Notes section of the assignment on Moodle.

Consider the following C++ main() function, which contains both a loop and an if-else statement:

int main()
{
  int balance, payment;

  cout << "Enter loan balance: ";
  cin >> balance;
  cout << endl;

  while(balance > 0)
  {
    int minp;              // Local to the while loop

    // Typecast the result to int to avoid compiler warnings.
    minp = int(balance * 0.04);

    cout << "Balance is $" << balance << ". Minimum payment is $" << minp << ".\n";
    cout << "Enter payment amount: ";
    cin >> payment;

    // Verify the payment is >= min payment and payment is <= new balance
    if(payment >= minp && payment <= balance)
    {
      balance -= payment;
    }
    else {
      cout << "Incorrect payment. Minimum is $" << minp 
           << " and maximum is $" << balance << "." << endl;
    }
    cout << endl;
  }  // end of the while loop

  cout << "Loan is paid off.\n";

  return 0;
}
The basic structure of the MIPS assembly for this loop would be the following:
       # Read the loan balance into register $s1

Loop:  beq $s1, $zero, Exit

       # Calculate the minimum payment into register $t1
       # Print current balance and minimum payment information
       # Read the user payment input into register $s2

       slt $t0, $s2, $t1   # See if payment is less than minimum
       sgt $t2, $s2, $s1   # See if payment is greater than balance
       or  $t3, $t0, $t2   

       bne $t3, $zero, Else
       sub $s1, $s1, $s2   # Apply payment to balance - the if statement
       j Loop

Else:  # Print error message
       j Loop

Exit:  # Print "Loan is paid off"

Assume that the user entered the following input while running the above code (user input is in BLUE font):

Enter loan balance: 500

Balance is $500. Minimum payment is $20.
Enter payment amount: 50

Balance is $450. Minimum payment is $18.
Enter payment amount: 5
Incorrect payment. Minimum is $18 and maximum is $450.

Balance is $450. Minimum payment is $18.
Enter payment amount: 100

Balance is $350. Minimum payment is $14.
Enter payment amount: 500
Incorrect payment. Minimum is $14 and maximum is $350.

Balance is $350. Minimum payment is $14.
Enter payment amount: 350

Loan is paid off.

Lab Writeup

  1. Profile the two branch statements in the MIPS code in terms of whether the branch was "taken" (T) or "not taken" (N) each time the branch instruction was encountered while the user was running the program. Each branch will have its own profile.
  2. For each branch, calculate the number of correct guesses and the number of incorrect guesses for the following branch prediction schemes:
    1. Assume not taken
    2. 2 bit predictor initialized to weakly predict not taken (each branch has its own predictor)
  3. Assuming the correct branch target is calculated in the ID stage, how many stalls would each of the branch prediction schemes in Question 2 cause for this code?
  4. How would your answer to the prior three questions change if you had a particularly stubborn user who kept giving a payment amount either less than the maximum or greater than the balance?
Submit your writeup via Moodle.