The following code must be converted to MIPS using a jump address table. Asume t
ID: 3721524 • Letter: T
Question
The following code must be converted to MIPS using a jump address table. Asume the 6 variables are contained in registers $s0 to $s5 and that register $t2 contains 4. Variable K needs to be asked to the user:
--------------------------------------------------------------------------------------------------------------------------------------------------------
CODE: switch (k) {
case 0: f = i + j; break ; /* k = 0*/
case 1: f = g + h; break ; /* k = 1*/
case 2: f = g - h; break ; /* k = 2*/
case 3: f = i - j; break ; /* k = 3*/
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
Solution:
The MIPS implementation of the statements are given below:
code:
Slt $t3,$s5,$zero # checking the condition if k<0
bne $t3,$zero,Exit # jump to exit if k<0
slt $t3,$s5,$t2 # check if k<4
beq $t3,$zero,Exit # kump to exit if k>=4
add $t1,$s5,$s5 # $t1 =2*k
add $t1,$t1,$t1 #$t1 =4*k
add $t1,$t1,$t4 #$t1= this is the address of the JumpTable[k]
lw $t0,0($t1) #$to=JumpTable[k]
jr $t0 #jump based on register $t0
L0: add $s0,$s3,$s4
j Exit
L1: add $s0,$s1,$s2
j Exit
L2: sub $s0,$s1,$s2
j Exit
L3: sub $s0,$s3,$s4
Exit:?
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.