26 Write a MIPS assembly program that computes the sum of in
26) Write a MIPS assembly program that computes the sum of integers N to 1000, where N is input by the user. The program must check to ensure that N lies between 0 through 1000. Run your program using MIPSym2.0 and make sure that the program compiles and functions correctly
Solution
Pseodocode :-
cout<<”Enter N value:-“;
cin>>v0;
if(v0>=0)
{
t0=0;
while(v0>0 &&v0<=1000)
{
t0=t0+v0;
to=t0-1;
}
cout<<t0;
}
else
exit 0;
Program:-
.data
prompt: .asciiz “\ Enter N value:-“
result: .asciiz “Sum of the integers from 1 to N is:-“
global main
.text
main:
li $v0,4 #system call code for Print String
la $a0,prompt #load address of prompt into $a0
syscall #print the prompt message
li $v0,5 #system call code for Read Integer
syscall #reads the value of N into $n
blez $v0,end #branch to end if $n<=0
li $t0,0 #clear register $t0 to zero
loop:
add $t0,$t0,$v0 #sum of integers in register $t0
addi $v0,$v0,-1 #summing integers in reverse order
bnez $v0,loop #branch to loop if $v0 is !=zero
li $v0,4 #system call code for Print String
la $a0,result #load address of message into $a0
syscall #print the string
li $v0,1 #system call code for Print Integer
move $a0,$t0 #move value to be printed to $a0
syscall #print sum of integers
b main #branch to main
li $v0,10 #terminate program run and
syscall #return control to system

