Question
2. Modify sum.s to find the sum of all positive numbers in the array. You need to test the program with some negative numbers in
2. Modify sum.s to find the sum of all positive numbers in the array. You need to test the program with some negative numbers in the array.
## sum.s - print sum of array elements. ##
## Assumes the array has at least two elements (a[0]
## and a[1]). It initializes both min and max to a[0]
## and then goes through the loop count-1 times.
## This program will use pointers. ##
## t0 - points to array elements in turn
## t1 - contains count of elements
## t2 - contains sum
## t3 - each word from array in turn
##
################################################# # # # text segment # # # #################################################
.text main: la $t0,array # $t0 will point to the array lw $t1,count # exit loop when $t1 is 0 li $t2,0 # initialize t2 as 0, t2 to hold the sum loop: lw $t3,0($t0) # load next word from array add $t2,$t2,$t3 # sum=sum + array[i] add $t1,$t1,-1 # decrement counter add $t0,$t0,4 # increment pointer by word bne $t1,$zero,loop # and continue if counter>0
la $a0,ans li $v0,4 syscall # print "sum = "
move $a0,$t2 li $v0,1 syscall la $a0,endl # system call to print li $v0,4 # out a newline syscall
li $v0,10 syscall # au revoir...
################################################# # # # data segment # # # #################################################
.data
array: .word 3,4,2,6
count: .word 4
endl: .asciiz " "
ans: .asciiz "sum = "
## ## end of file sum.s
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started