MIPS architecture loop help required

144 views Asked by At

Complete the SPIM assembly language program loop2.s. The program will calculate the sum of the elements in "numbers" whose value is less than or equal to 1000.

I tried to program the code, however the output is coming , whereas i need it to be 11

Program Name: loop2.s

  • will calculate the sum of all elements in the array "numbers" whose value is less than or equal to 1000.
  • "numbers" is an array with 5 integer elements.
  • "count" holds the number of elements in "numbers".

  • Output format must be "sum = 11"

t0 - points to array elements in turn t1 - contains a count of elements

t2 - contains sum

t3 - each word from the array "numbers" in turn

#################################################
#                                               #
#               text segment                    #
#                                               #
#################################################

        .text
        .globl __start
__start:                # execution starts here


#   Put your answer between dashed lines.
#
#------------------Your code starts next line---------------
  la $t0, numbers
   lw $t1, count
   li $t2, 0

   process:
       lw $t3, ($t0)           # load word from the array
       add $t2, $t2, $t3       # add it to sum
       add $t0, $t0, 4           # increment the pointer / get the next element of the array
       sub $t1, $t1, 1           # decrement the counter
       beqz $t1, done           # if counter = 0, then it's done
       j process

   done:
       la $a0, ans1
       li $v0, 4  
       syscall

       move $a0, $t2
       li $v0, 1
       syscall

       la $a0, endl
       li $v0, 4
       syscall

       li $v0, 10
       syscall
#-----------------Your code ends above this line----------------

    la $a0,endl # syscall to print out
    li $v0,4    # a new line
    syscall 

    li $v0,10   # Exit
    syscall     # Bye!


#################################################
#                                               #
#               data segment                    #
#                                               #
#################################################

        .data
    numbers:    
        .word 3,2000,2,6,3000
    count:  .word 5

    ans1:   .asciiz "sum = "
    endl:   .asciiz "\n"       

##
##  end of file loop2.s
1

There are 1 answers

1
lostbard On

You need to add code that checks whether the value is <= 1000 (or > 1000) to either do (or don't) add the number to $t2.

process:
   lw $t3, ($t0)           # load word from the array

   # check if > 1000, and if it is, jump to don't_add (ie: skip the adding to sum)
   bgt $t3, 1000, dont_add


   add $t2, $t2, $t3       # add it to sum
dont_add:    
   add $t0, $t0, 4         # increment the pointer / get the next element of the array
   sub $t1, $t1, 1         # decrement the counter
   beqz $t1, done          # if counter = 0, then it's done
   j process

done: