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.
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).
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...
consider execution of the following switch statement: int Enter = 10; cin >> Enter; switch (Enter)...
consider execution of the following switch statement: int Enter = 10; cin >> Enter; switch (Enter) { case 1: Enter = -4; case 2: Enter = -6; case 4: break; case 6: Enter = -8; break; default: Enter = -1; } What would the value of Enter be after execution of this code if the value read for Enter were 4? -4,-6,-8 or none
Financial Statement Case 4-1 This case, based on the balance sheet of Target Corporation, will familiarize...
Financial Statement Case 4-1 This case, based on the balance sheet of Target Corporation, will familiarize you with some of the assets and liabilities of that company. Use the Target Corporation balance sheet to answer the following questions. Requirements 1.Which balance sheet format does Target use? 2.Name the company’s largest current asset and largest current liability at January 30, 2016. 3.Compute Target’s current ratios at January 30, 2016, and January 31, 2015. Did the current ratio improve, worsen, or hold...
Use a switch statement to write a function that returns TRUE if a character is a...
Use a switch statement to write a function that returns TRUE if a character is a consonant and returns FALSE otherwise.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT