The following C program calculates the sum of 10 array elements using a recursiv
ID: 3564924 • Letter: T
Question
The following C program calculates the sum of 10 array elements using a recursive function. Convert it to MIPS assembly language (assembly code) and simulate your code in MARS(MIPS Assembler and Runtime Simulator) for testing.
C Program:
#include <stdio.h>
int sum_array_elements( int arr[], int n ) {
//base case:
if (n < 0) {
return 0;
}
//Recursion: calling itself
return arr[n] + sum_array_elements(arr, n-1);
}
int main(){
int array[] = {1,2,3,4,5,6,7,8,9,10};
int sum;
sum = sum_array_elements(array,10);
printf(" Sum of array elements is: %d ", sum);
return 0;
}
Explanation / Answer
# instead of N use 10 this is generalised program
#####################################################################
# Cross References:
# v0: N,
# t0: Sum
#####################################################################
.data
prompt: .asciiz