Convert the C function below to MIPS assembly language Make

Convert the C function below to MIPS assembly language. Make sure that your assembly language code could be called from a standard C program (that is to say, make sure you follow the MIPS calling conventions).

int sum (int n) {

if (n == 0)

return 0;

else return n + sum(n-1); }

Solution

The MIPS code is as follows:

sum:

addi $sp, $sp, -8 # Set up the stack

sw $ra, 4($sp) # Save return address

addi $t0, $a0, 0 # Initialize the sum

li $v0, 0 # Initialize return value

beq $t0, 0, return # If argument is 0 then return

subi $t1, $t0, 1 # Compute n-1

sw $t0, 8($sp) # Save caller saved regs

addi $a0, $t1, 0 # Move n-1 into argument register

jal sum # Call sum

lw $t0, 8($sp) # Restore caller saved reg

add $v0, $t0, $v0 # Add return value to $t0

lw $ra, 4($sp) # Get the return address

return:

jr $ra # Return

Convert the C function below to MIPS assembly language. Make sure that your assembly language code could be called from a standard C program (that is to say, ma

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site