Translate the high-level code below into MIPS assembly. You can assume that $s0
ID: 3792445 • Letter: T
Question
Translate the high-level code below into MIPS assembly. You can assume that $s0 holds the value of x and $s1 holds the value of y. int x, y; x = 0 ; while (x > 4; x = x + 2;} Translate the high-level code below into MIPS assembly. Assume that $t1 holds the value of var and $t2 holds the value of i. Assume that you must store the value of i to address 0x2000A000 at the end of this code. char i ; int var; switch (var) {case 0 : i++; break; case 1: i--; break; case 2: i = 0x12345678; break; default: i = 0;}Explanation / Answer
(1)
$t0 is the constant 30
$t1 is the counter variable i.e. x
$t2 is y
li $t0, 30 # t0 is a constant 30
li $t1, 0 # t1 is our counter (i)
loop:
beq $t1, $t0, end # if t1 == 30 we are done
srl $t2 $t1 4 # shift x 4 places to right and store the result in y.
addi $t1, $t1, 2 # add 1 to t1
j loop # jump back to the top
end:
(2)
.data
array .space 80 # this array is stored at address 0x12345678
lw $t2, 0($i)
lw $t1, 0($var)
bltz $t1, default
slti $t3, $t1, $zero # i<3?
bne $t3, $zero, Exit # if i< 0 then exit
slti $t3, $t1, 3 # check if i<3
beq $t3, $zero, Exit # if i >= 3 then exit
add $t4, $t1, $t1 # $t4 = 2*i
add $t4, $t4, $t4 # $t4 = 4*i
add $t4, $t4, $t5 # $t5 = address of JumpTable
lw $t0, 0($t1) # $to = JumpTable[k]
# The branch table is initialized to hold in successive locations
the absolute addresses of the instructions at labels is0, is1, and is2.
jr $t0
is0: addi $t2, $t2, 1 # i++
j done
is1: addiu $t2, $t2, -1 # i--
j done
is2: // to load address depends on MIPS32 or MIPS64 you are using.
j done
def : xor $t2, $t2, $t2 # xor the contents of $t2 to make it zero.
j done
done:
Again to store address 0x2000A000 in i, it depends on MIPS32 or MIPS64
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.