完成SPIM汇编语言程序loop2.s。 程序将计算“数字”中元素的总和 值小于或等于1000。
我尝试对代码进行编程,但是输出即将到来,而我需要将其设置为11
程序名称:loop2.s
“计数”保存“数字”中元素的数量。
输出格式必须为 “总和= 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
答案 0 :(得分:0)
您需要添加代码以检查值是否为<= 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: