Question
Type Qtspim mips code into a regular notepad file. May have to download Qtspim if you do not have it. It is required in order
Type Qtspim mips code into a regular notepad file. May have to download Qtspim if you do not have it. It is required in order to run code.
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 , , and the comparison is as described on your green sheet. The bgt instruction on the green sheet, for example, says to go to the label if rs > rt, or in other words the program counter (PC) receives the memory location mapped to the label. It is very useful to begin learning how to read these summaries on the green sheet. What is a pseudoinstruction? It is an instruction that does not exist in the Instruction Set Architecture! But it is commonly used, so the writers of the assembler provide it for you, then translate that into different (usually several) machine instructions when running the code through the assembler. [An assembler is a simpler translator that turns assembly language to machine code, which is often a 1-1 translation; compilers for translating high level languages to machine code are much more complex.] Your task today is to write the program above in assembly language. Try to use the pseudoinstructions, and I also encourage you to work in small groups. Youll need to put five asciiz strings into the .data area. Dont forget to create a .text area and a marker for the main( ) function. The input and output can be accomplished with the syscall routines we already have used. You do not need to pre-initialize the variables and you may assume that the input from the user will be correct. Further, you do not have ready access to a random number generator, so please just hard code a value like 5 into a register as the value for the random number. When you have the code working, look carefully at the User Segment of the SPIM simulator. Compare the program as translated there to what you wrote, and on a separate sheet of paper answer the following questions:
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started