Question

Write a MIPS program to demonstrate the operation of the following switch statement. switch (S) {...

Write a MIPS program to demonstrate the operation of the following switch statement.

switch (S) {
   case  5: A = A + 1; break;
   case 25: A = A - 1; break;
   default: A = A * 2; break;
}

Implement and simulate your solution. Allocate .data locations for the variables S and A, and load these values into registers before performing the switch statement on the registers. Run your program several times with different values for these variables, to ensure that your program works as expected. Bonus marks if your program prompts the user for the values of S and A, and displays results of the computation to the user.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

.data
   inputS: .asciiz "Enter value of S: "
   inputA: .asciiz "Enter value of A: "
   outMsg: .asciiz "A = "
   S: .word 0
   A: .word 0
.text
   #print string
   li $v0, 4
   la $a0, inputS
   syscall
  
   #read int and save to S
   li $v0, 5
   syscall

   sw $v0, S
  
   #print string
   li $v0, 4
   la $a0, inputA
   syscall
  
   #read int and save to A
   li $v0, 5
   syscall

   sw $v0, A
  
   lw $t1, S
   lw $t2, A
  
switch:
   beq $t1, 5, case5
   beq $t1, 25, case25
default:
   mul $t2, $t2, 2
   b end_switch
  
case5:
   add $t2, $t2, 1
   b end_switch
case25:
   sub $t2, $t2, 1
   b end_switch

end_switch:
   sw $t2, A  
   #display value of A
   li $v0, 4
   la $a0, outMsg
   syscall
  
   move $a0, $t2
   li $v0, 1
   syscall
  
   #exit
   li $v0, 10
   syscall


output
---
Enter value of S: 20
Enter value of A: 3
A = 6
-- program is finished running --

Enter value of S: 5
Enter value of A: 6
A = 7
-- program is finished running --

Enter value of S: 25
Enter value of A: 9
A = 8
-- program is finished running --

Add a comment
Know the answer?
Add Answer to:
Write a MIPS program to demonstrate the operation of the following switch statement. switch (S) {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Find the errors in following program. //This program uses a switch statement to assign a //...

    Find the errors in following program. //This program uses a switch statement to assign a // letter grade (A, B, C, D, or F) to numeric test score. #include <iostream> uisng namespace std; int main() { int testScore; cout<<"Enter your test score and I will tell you\n"; cout<<"the letter grade you earned: "; cin>>testScore; switch (testScore) { case (testScore < 60) cout<<"Your grade is F.\n"; break; case (testScore <70) cout<<"Your grade is D.\n" break; case (testScore <80) cout<<"Your grade is...

  • Write a C program that does the following: • Displays a menu (similar to what you...

    Write a C program that does the following: • Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square, Diamond (with selected height and selected symbol), or Quits if user entered Q. Apart from these 2 other shapes, add a new shape of your choice for any related character. • Program then prompts the user to...

  • Pick a switch statement from the reading. Put it into a main() Java program and make...

    Pick a switch statement from the reading. Put it into a main() Java program and make it work. Attach the .java file and copyAndPaste the output into the WriteSubmissionBox The switch Statement S1.51 Multiway if-else statements can become unwieldy when you must choose from among many possible courses of action. If the choice is based on the value of an integer or character expres- sion, the switch statement can make your code easier to read. The switch statement begins with...

  • Write a c++ expression representing the following algebraic expression. Assume that all variables in your program...

    Write a c++ expression representing the following algebraic expression. Assume that all variables in your program are of the type double and that your program has already included the <cmath> header file. 3x + 1/y - 10 + Squareroot g Your answer: (b) Rewrite the same expression assuming that variables x and y in your program are of type int your answer: Convert the following switch statement into an equivalent if-else if statement switch (ch) {case 'A'; cout << "...

  • Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table...

    Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table shown below. The program will then display the cost per pound for that vegetable, again using the table provided. Your program must use a switch statement to process the input value and display the desired output. Be sure to provide a default case in your switch statement which handles unrecognized input. Use named constants for all constant strings (e.g. prompts and vegetable names) and...

  • please use c programme Write a program which simulates a calculator using the following specifications. (50...

    please use c programme Write a program which simulates a calculator using the following specifications. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • User input must have the following format - value1 operator value2 • Compute value1 operator value2 • Use switch statement • Operators must be ’+’ ,’-’, ’*’, ’/’ and ’%’ • Division by 0 in C leads to an error. Terminate the program before divison occurs in such cases. •...

  • Methods and File Output and Exceptions Outcome: Student will demonstrate the ability to write, use and...

    Methods and File Output and Exceptions Outcome: Student will demonstrate the ability to write, use and call methods Student will demonstrate the ability to pass values to and from methods Student will demonstrate the ability to catch exceptions Student will demonstrate the ability to create a text file Student will demonstrate the ability to validate input data Program Specifications: You to write a menu driven program. The program will use a switch statement. The cases will be as follows: Get...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class...

    // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class (SwitchDoLab) //--> {    //   Declare the main method    //-->    {        //   Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3.        //-->        //-->        //-->        //   Create an integer variable named choice to store user's option.        //-->        //   Create a Scanner object        //   Create...

  • Please design, write the code and create the requisite submission files in C++ for the following...

    Please design, write the code and create the requisite submission files in C++ for the following problem: A menu-driven program that offers the user 5 options as follows: Menu: 1. Display array 2. Add up all elements in the array and display that number. 3. Add 1 to each element in the array. 4. Swap the values in row one with the values in row three. 5. Exit In main(), declare and initialize a 2-dimensional array that contains 5 x...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT