【发布时间】:2019-10-28 15:20:14
【问题描述】:
完成SPIM汇编语言程序loop2.s。 该程序将计算“数字”中元素的总和 其值小于或等于 1000。
我尝试编写代码,但是输出即将到来,而我需要它是 11
程序名称:loop2.s
- 将计算数组“numbers”中所有元素的总和 其值小于或等于 1000。
- “numbers”是一个包含 5 个整数元素的数组。
“count”保存“numbers”中元素的数量。
输出格式必须是 “总和 = 11”
t0 - 依次指向数组元素 t1 - 包含元素计数
t2 - 包含总和
t3 - 数组“numbers”中的每个单词依次
#################################################
# #
# 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
【问题讨论】:
-
“但是输出即将到来,而我需要它是 11” 请澄清 “输出即将到来” 的含义。显然,如果您想排除某些元素,则需要某种比较和条件分支。我建议您通过 MIPS 指令集参考来查看可以使用哪些指令。