Translate the following algorithm that finds the sum of the

Translate the following algorithm that finds the sum of the numbers from 0 to N to MIPS assembly. Assume $s0 holds N, $s1 holds sum, and that N is greater than or equal to 0.

int sum = 0

if (N==0)

return 0;

while (N != 0) {

sum = sum + N N=N-1;

}

return sum;

Solution

there are two variables - N(\'num\') and sum (\'sum\')

suppose we want to add numbers from 1 to 5

So num = 5, and we have to find the value of \'sum\'. let initialize the value of sum as 0.

according to algorithm:
N != 0,

so it will enter in else part

sum = sum + N: 0+5 = 5 so sum = 5,
N=N-1: 5-1 = 4 so N=4

keep repeating these steps till the vaule of N is 0.

5+4 =9, sum = 9, N= 3
9+3 =12, sum 12, N=2,
12+2 =14, sum = 14, N=1,
14+1 =15, sum = 15, N=0.

now the value of N=0, come out of loop and display the value of \'sum\' as 15.

Translate the following algorithm that finds the sum of the numbers from 0 to N to MIPS assembly. Assume $s0 holds N, $s1 holds sum, and that N is greater than

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site