1. Translate the following code into MIPS code. Test (int i, int j) { int k; k =
ID: 670995 • Letter: 1
Question
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.
2. Translate the following C code into MIPS code.
int test (int n)
{
if (n < 2 ) return (10);
else
{
k = 20 + test (n-1);
k=k + n;
return (k);
}
}
Assume variable k is assigned to register $s1. Note: your code should be complete.
3. Translate the following code into MIPS code.
int sum (int n)
{
if (n < 2 ) return (1);
else return (n + sum(n-1) + sum(n-2));
}
4. Translate the following code into MIPS code.
int csf (int n)
{
if (n <1) return (1);
else
{
if( n is odd) return ( n + csf(n-1))
else return ( n * csf(n-1))
}
}
Explanation / Answer
In the above case, Test is a non-leaf subroutine since it calls Sub.
The function Sub does not call any other function. However, it will need storage for the local variables. Therefore, Sub is a leaf function with data.
Sub:
addi $sp, $sp, -8
addi $t1,$t1
lw $ra, 4($sp)
return:
jr $ra
Test:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.