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 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.

Solutions

Expert Solution

Switch statement

The switch statement is one of the statements we had in computer programming. It is a multiway branch statement. Using the switch statement we can choose one of the code blocks to be executed. As in, using a variable, we test the value of the variable, against list of values. Each value of the variable is called a case.


Given problem:
We have a variable ‘s’ with the possible values from the set [0,2]
We also have cases given:
1.   case 0: a=a-1;
break;
2.   case 1: a=a+2;
break;
3.   case 2: b=3*b;
break;


The computer programming code/pseudocode can be as follows:
switch (s)
{
case 0: a=a-1;
break;
case 1: a=a+2;
break;
case 2: b=3*b;
break;
default: a = 0; //assume for example
break; //suppose
}


Coming to the MINIMIPS assembly instructions:


Using the mapping as follows:
a: s0, b: s1, s: s2

Code in MIPS


lw $s0,a # load a from memory--assume
lw $s1,b # load b from memory--assume

bne s2, 0, L1 # case s!=0
sub s0, s0, 1 # case s=0 so a=a-1
b Exit # end of case so Exit


L1:
bne s2, 0, L2 # case s != 1
addi s0, s0, 2 # case s=1 so a=a+2
b Exit # end of case so Exit


L2:
bne s2, 0, Exit # case s != 2
muli s1, s1, 3 # case s=2 so b=b*3
Exit:


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 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.
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT