Write the MIPS assesmbly code corresponding to the following
Write the MIPS assesmbly code corresponding to the following C code segment. Assume that f is assigned to register $s0. If possible. can you leave comments so I can follow what is actually being done, thanks!
int Example (int i, int j, int k)
{
int f;
f = i + j - k;
return f;
}
Solution
To solve this problem break the code into different lines where it is actually using register and doing computation
my code will be like this
1) int example(int i, int j, int k){
2) int f;
3) f = i + j;
4) f -= k;
5) return f;
6) }
For the first line MIPS code will be something like this -
sw $fp,12($sp)
move $fp,$sp
sw $x,16($fp) // x is assigned to i
sw $y,20($fp) // y is assigned to j
sw $z,24($fp) // z is assigned to k
for 2nd line in code nothing will happen. Its just declaration.
for 3rd line
lw $3,16($fp) // load value of i
lw $2,20($fp) // load value of j
addu $s0,$3,$2 // add both the value and store in temporary register $s0
sw $s0,4($fp) // store that value into $fp + 4
for 4th line
lw $3,4($fp) // load value of f to register $3
lw $2,24($fp) // load value of k into register $2
subu $s0,$3,$2 // substract $3- $2 and assgned it to register $s0
sw $s0,4($fp) // store the value of $s0 into memory[$fp + 4]
for 5th line
lw $2,4($fp) // returning so need to load the value from memory $fp + 4 to $2.
for 6th line
move $sp,$fp
lw $fp,12($sp)
j $31
Some of the instruction in MIPS is quite confusing and sometimes ordering makes it more confusing.

