C to MIPS Conversion Practice Set 2
Full MIPS solutions are provided below so you can blank out fields as needed.
Assumptions:
- Calling convention: arguments in $a0, $a1, $a2 and the return value should be placed in $v0
- 32-bit MIPS
1) Function f1
C code:
/* p: $a0, t: $a1, v: $a2 */
int f1(short *p, unsigned int t, short v)
{
int x = v + *p;
t = t - 2;
*p = x >> t;
return x;
}
MIPS solution:
f1:
l__ $t0, 0($a0)
add ___, $a2, $t0
addi $t1, $a1, -2
____v $t2, ___, $t1
___ $t2, _____
jr $ra
2) Function f2
C code:
/* arr: $a0, k: $a1, j: $a2*/
void f2(int *arr, int k, int j)
{
if (k < j) {
arr[0] = arr[k];
}
else {
arr[1]++;
}
}
MIPS solution:
f2:
____ $t0, $a1, $a2
b____ $t0, $zero, L1
sll $t1, $a1, ____
add $t1, $a0, $t1
lw $t2, ____
sw $t2, ____
b L2
L1:
____ $t3, ____
addi $t3, $t3, 1
sw ____, ____
L2:
jr $ra
f1 Solutions
f1:
lh $t0, 0($a0)
add $v0, $a2, $t0
addi $t1, $a1, -2
srav $t2, $v0, $t1
sh $t2, 0($a0)
jr $ra
f2 Solutions
f2:
slt $t0, $a1, $a2
beq $t0, $zero, L1
sll $t1, $a1, 2
add $t1, $a0, $t1
lw $t2, 0($t1)
sw $t2, 0($a0)
b L2
L1:
lw $t3, 4($a0)
addi $t3, $t3, 1
sw $t3, 4($a0)
L2:
jr $ra