1. Translate the following code into MIPS code.
Test (int i, int j)
{
int k;
k = i +j +10;
k = Sub(k+1) + Sub (k)
return k;
}
Sub (int m)
{
int g;
g = m + m;
return g;
}
Assume variables k and g are assigned to registers $s0 and $s1, respectively. Note: your code should be complete.
Solution:
Given, variables k and g are assigned to registers $s0 and $s1, respectively.
Let us assume i, j and m are assigned to $a0, $a1, $a2 respectively.
Translation of C code to MIPS:
Test: addi $sp, $sp, -4 // adjust stack for 2 items
sw $ra, 0($sp) //save the return address
sw $a0, 0($sp) //save the argument i
sw $a1, 0($sp) //save the argument j
sw $a2, 0($sp) //save the argument m
addi $s0, $a0, $a1, 10 //store k equal to i + j + 10
add $t0, $s0, 1 //add K + 1 and store in temporary register
sub $t0, $t0, $s0 //subtract k+1 and store temporary register
add $s0, $s0, $t0 //addition of sub(K+1) and $s0
jr $s0 // return k
Exit
Sub: sw $a2, 0($sp) //save the argument m
sw $s1, 0($sp) //save the variable g
add $s1, $a2, $a2 //g = m + m
jr $s1 // return g
Please give thumbsup, if you like it. Thanks.
Get Answers For Free
Most questions answered within 1 hours.