Write the MIPS for the function with the following signature

Write the MIPS for the function with the following signature: int getsum(int B[], int start, int end). This function will calculate the sum of all the values within array B between start and end indexes. The size of the array B is K, the base address of B in $a0. The return value will be stored in $v0. Using basic MIPs operations only (add, sub, addi, lw, beq....)

Solution

Solution ::

main: // main body
// initialize the values to the 3 registers
addi $a0,$zero,10
jal sum // calling method

// Print out summation upto the number 10
li $v0,1 // print the integer

add $a1,$v0,$zero // load the return value into the argument
syscall

li $v0,10 // Exit here
syscall

sum: // sum body

addi $sp,$sp,-8 // allocate the space on the stack   
sw $ra,0($sp) // store the return address (ra)
sw $a0,4($sp) // store the arguments

slti $t0,$a0,1 // check if the n > 0   
beq $t0,$0,recurse // it is n > 0 case
add $v0,$0,$0 // start the return value to 0   
addi $sp,$sp,8 // pop the 2 items off the stack   
jr $ra // return to the caller

recurse:   
addi $a0,$a0,-1 // calculate the n-1
jal sum // recursively the call sum(n-1)

lw $ra,0($sp) // restore the saved return address
lw $a0,4($sp) // restore the saved argument
addi $sp,$sp,8 // pop the 2 items off the stack   

add $v0,$a0,$v0 // calculate the n + sum(n-1)
jr $ra // return to the caller

                                       /// *** Thank You *** ///

 Write the MIPS for the function with the following signature: int getsum(int B[], int start, int end). This function will calculate the sum of all the values w

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site