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...
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...
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 ( (+, -, * , /) .
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...
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