Требуется помощь цикла архитектуры MIPS - PullRequest
1 голос
/ 14 июня 2019

Завершение программы на языке ассемблера SPIM loop2.s. Программа рассчитает сумму элементов в «числах» чье значение меньше или равно 1000.

Я пытался запрограммировать код, однако вывод идет, в то время как мне нужно, чтобы он был 11

Название программы: loop2.s

  • рассчитает сумму всех элементов в массиве «числа» значение которого меньше или равно 1000.
  • "числа" - это массив с 5 целочисленными элементами.
  • «count» содержит количество элементов в «numbers».

  • Выходной формат должен быть "сумма = 11"

t0 - указывает на элементы массива по очереди t1 - содержит количество элементов

t2 - содержит сумму

t3 - каждое слово из массива "цифр" по очереди

#################################################
#                                               #
#               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 Ответ

0 голосов
/ 14 июня 2019

Вам нужно добавить код, который проверяет, является ли значение <= 1000 (или> 1000), чтобы либо добавлять (либо не добавлять) число к $ 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:
...