Question: Using branching statements for loops and conditionals! Consider the C program below: #include //#include // has rand(). #include // On older compilers, use using namespace

Using branching statements for loops and conditionals!

Consider the C program below:

#include

//#include // has rand().

#include // On older compilers, use

using namespace std;

int main()

{

int magic_number; // magic number

int guess; // user's guess

cout << "I will create a magic number between 0 and 9 inclusive and ask you to guess it." << endl;

magic_number = rand()%10; // get a random number between 0 and 9

cout << "Enter your guess: ";

cin >> guess;

while (guess != magic_number) // as long as guess is incorrect

{

if(guess > magic_number)

{

cout << "Too big! Guess again..." << endl;

}

else // guess is less than magic

{

cout << "Too small! Guess again..." << endl;

}

cin >> guess;

}

cout << "You are RIGHT!" << endl;;

return 0;

}

Note that this program has both a loop and a conditional statement. To implement these in MIPS we need to use branching statements. The two statements in the MIPS Instruction Set Architecture are beq and bne (branch on equal and branch on not equal, respectively). However, you may have noticed that there are statements for other branches that we have already used (bge) and a further number are listed on your books green card in a special area near the front right bottom corner, which is labeled pseudoinstruction set. You may want to look at that now. All of the branch instructions compare two values stored in registers. Use the s0-s7 registers for declared variables in C/C++, and t0-t9 for temporary values. The format for the instructions is , ,

1. What branching instructions did you use to implement your loop and if-else blocks? What combinations of instruction(s) did the simulator use in place of each?

2. What does the instruction nop stand for (that is, what does the acronym mean)? Did any of the branch instructions in the simulator have a nop instruction following the branch?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!