Question

In: Computer Science

A switch/case statement allows multiway branching based on the value of an integer variable. In the...

A switch/case statement allows multiway branching based on the value of an integer variable.
In the following example, the switch variables can assume one of the three values in [0, 2] and a different action is specified for each case.

switch (s) {
   case 0: a=a+1; break;
   case 1: a=a-1; break;
   case 2: b=2*b; break;
}

Show how such a statement can be compiled into MiniMIPS assembly instructions.

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for question . Be safe .

Note : check attached image for register value after program ends . code compiled and tested in MARS mips .

Program :

#--------------------------------------------------------
        #program to implement switch case
        #value of a variable in $t0
        #value of b variable in $t1
#--------------------------------------------------------

.data

.text
   main:
       #value in $t0 using addi
       addi $t0,$0,2 #$t0(a)=2+0
       # in $t1 using addi
       addi $t1,$0,3 #$t1(b)=3+0
       #beq is a branch instruction
       #beq <operand 1> <operand 2>,label
       beq $t0,0,case0 #if $t0(a)==0 go to case0 label
       beq $t1,0,case1 #if $t0(a)==1 go to case1 label
       beq $t2,0,case2 #if $t0(a)==2 go to case2 label
       j exit #if a not equal to 0,1,2 go to exit label (default break of switch)
      
   case0:#case0 label
       addi $t0,$t0,1 #$t0=$t0+1 = a=a+1
       j exit #break jump to exit label
   case1: #case1 label
       addi $t0,$t0,-1 #$t0=$t0-1 #a=a-1
       j exit #jump to exit label
   case2: #case 2
       mul $t1,$t1,2 #b=b*2 #t1=$t1*2
       j exit #jump to exit label
      
   exit: #exit label
       li $v0,10#syscall 10 to exit
       syscall
      
      
  
  
  
Output : when a=2 using switch b=3 so after switch case b=6

Output : a=0 and b=1 than using switch a=a+1=1

Please up vote ,comment if any query .


Related Solutions

A Switch/case statement allows multiway branching based on the value of an integer variable. In the...
A Switch/case statement allows multiway branching based on the value of an integer variable. In the following example, the switch variable s is specified to be assumed to be one of three values ​​of [0, 2], and a different action for each case becomes: case 0: a = a - 1; break; case 1: a = a + 2; break; case 2: b = 3 * b; break; Shows how such statements can be compiled into MiniMIPS assembly instructions.
Problem set Rewrite the following if statement as an equivalent switch statement. The variable digit is...
Problem set Rewrite the following if statement as an equivalent switch statement. The variable digit is of type int. if (digit == 0) value = 3; else if (digit == 1) value = 3; else if (digit == 2) value = 6; else if (digit == 3)     value = 9; The decision table below shows fines imposed for speeding violations. Write a code segment that assigns the correct fine to type double variable fine based on the value of...
A) Is it important to have a break after every case in the switch statement?) b)...
A) Is it important to have a break after every case in the switch statement?) b) Explain lain your answer by writing an example of a switch statement and showing the effect of including and excluding the break.                                                                                                                                                               10 points A) String Name = KB.NextLine();       Write a java statement to convert Name to upper case. B) char Letter =KB.next().charAt(0);           Write a java statement to convert Letter to lower case.                                                                                                                                                        5 points c) Write a while loop which...
3. Write a program using switch-case statements that accepts an integer number between 0 and 100...
3. Write a program using switch-case statements that accepts an integer number between 0 and 100 and, based on its value, reports its equivalent letter grade (A, B, C, D, or F).
8. write a program using switch-case statements that ask a user to enter a temperature value...
8. write a program using switch-case statements that ask a user to enter a temperature value in degrees Fahrenheit (In your program, use an integer to record the user’s entry. If the temperature falls between 0 and 96 make it print “Temperature below normal”. If the temperature is 97, 98, make it “Temperature normal”. If the temperature is between 100 and 150, print “You have a fever”. If the temperature is outside the ranges given above, display “Are you human”....
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i //...
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i // declare while loop condition where i is less then 25 // inside of brackets calculate the sum of i (addition) // increment i // outside the loop print the sum of values ============================================= 2) Create a sentinel value example if I press number 0 it will display the sum of data // create a scanner // prompt the user to to enter the numbers...
Output with Vars.java A variable like userNum can store a value like an integer. Extend the...
Output with Vars.java A variable like userNum can store a value like an integer. Extend the given program as indicated. Output the user's input. (2 pts) Output the input squared and cubed. Hint: Compute squared as userNum * userNum. (2 pts) Get a second user input into userNum2, and output the sum and product. (1 pt) Note: This zyLab outputs a newline after each user-input prompt. For convenience in the examples below, the user's input value is shown on the...
Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
Give a research-based statement of and rationale for the method you believe allows the best understanding...
Give a research-based statement of and rationale for the method you believe allows the best understanding of human experience
Question: Write a complete C++ program that runs multiple tests and calculations using switch statement. Based...
Question: Write a complete C++ program that runs multiple tests and calculations using switch statement. Based on your input and the selection from the keyboard, the program does the followings. If the selection is 1, the program should test the input if it’s positive, negative or zero. For example, the output should be “The selection is 1 to test the input value if it’s positive, negative or equal to zero. The input value 7 is positive” If the selection is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT