Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Many high level computer languages have a switch or case statement which compare

ID: 3904658 • Letter: M

Question

Many high level computer languages have a switch or case statement which compares the value of an expression against one or more labels and executes the statements that follow a matching label (or those following the default label if no label matches). A break statement exits from the enclosing switch statement. For example, in Java: switch (count) I case 1 result-"one" break; case 2: result - "two"; break; default: result"many"; break; Describe the machine code that would be generated by a compiler for a switch statement. If you wish you can use fragments of Brookshear assembly language in your answer. Your answer need not use the exact assembly language mnemonics and instruction formats, but it should be clear what the code is doing [17 marks]

Explanation / Answer

Please find the code in mips along with comments.

.globl main
.data

.text

main:
li $v0, 5 #promp user to enter any number
syscall
move $t0, $v0 #move data to t0

beqz $t0, case0 #if t0=0 go to case0
beq $t0, 1, case1 #if t0=1 go to case1
beq $t0, 2, case2 #if t0=2 go to case2
beq $t0, 3, case3 #if t0=3 go to case3
j exit #default exit

case0: #istruzioni
j exit #exit will be using as break

case1: #istruzioni
j exit
case2: #istruzioni
j exit
case3: #istruzioni
j exit

exit:
li $v0, 10
syscall

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote