【发布时间】:2021-10-12 05:59:03
【问题描述】:
我需要帮助来计算整数数组中所有元素的总和。 我似乎无法循环工作。 如果您不提出基于“请勿更改或使用此行下方的任何内容”下方的更改的解决方案,我将不胜感激。
预期结果:
11个整数的和是217当前和不正确的结果:
11个整数之和为11代码sn-p:
##############################################################################
# DESCRIPTION: Calculates the total sum of all elements in an integer array
#
# INPUT: $a0 - The address of the first number in the array
# $a1 - Number of integers stored in the array
#
# OUTPUT: $v0 - The sum of all numbers in the integer array
##############################################################################
.text
j main
int_array_sum:
## Clues:
## - Do not forget to clear registers that do not have an edge content
## - An integer corresponds to 4 bytes of memory. Each integer is thus 32 bits
## - If you want to multiply the contents of a register with an even two-power,
## a common trick (hint) is to shift bits a few steps left.
## Example (verify with pen and paper. sll = Shift Left Logical):
## sll $t1, $t0, 2 # $t1 = $t0 * 4
## sll $t0, $t1, 4 # $t0 = $t1 * 16
#pseudocode:
# If $t0 (i) is = $a1 skip to end_int # Done if i == N
# Multiply $t0 by 4 and store it in $t1 # Indexadr = i * 4
# Add $t1 with $a0 and save in $t2 # Address = $a0 + Indexadr
# Retrieve the number from memory that $t2 points to and store in $t3 # n = A [i]
# Add $t3 to $v0 (results register) # Sum = Sum + n
# Add 1 to register $t0 # i = i + 1
# Skip to for_every_int
#### WRITE YOUR ASSEMBLY CODE HERE ####
li $v0, 0
li $t0, 0
loopA:
#If $t0 (i) is = $a1 skip to end_int
beq $t0, $a1, end_int # Done if i == N
# Multiply $t0 by 4 and store it in $t1
sll $t1, $t0, 2 # Indexadr = i * 4
#Add $t1 with $a0 and save in $t2
add $t2, $t1, $a0 # Address = $a0 + Indexadr
#Retrieve the number from memory that $t2 points to and store in $t3
lb $t3, 0($t2) # Load the number from array # n = A [i]
#Add $t3 to $v0 (results register)
addu $v0, $v0, $t3 # Sum = Sum + n
#Add 1 to register $t0
add $t0, $t0, 1 # i = i + 1
#Repeat
j loopA
end_int:
jr $ra # Return to calling code
##############################################################################
##############################################################################
##
## *** Don't change or use ANYTHING below this line.
##
##############################################################################
##############################################################################
### Data that is being used as main program
.data
INT_COUNT:
.word 11
INT_ARRAY:
.word 1, 3, 6, 9, 2, 4, 6, 8, 10, 55, 113
INT_1_str:
.asciiz "Sum of the "
INT_2_str:
.asciiz " integer is "
.text
.globl main
##############################################################################
#
# MAIN: Calls subroutine and prints the results
#
##############################################################################
main:
##---
### int_array_sum
##---
li $v0, 4
la $a0, INT_1_str
syscall # print string
lw $a0, INT_COUNT
li $v0, 1
syscall # print integer
li $v0, 4
la $a0, INT_2_str
syscall # print string
li $v0, -1
la $a0, INT_ARRAY
lw $a1, INT_COUNT
jal int_array_sum # run subroutine
# Print sum
add $a0, $v0, $zero
li $v0, 1
syscall # print integer (array sum)
###--- EXIT
li $v0, 10 # MARS / SPIM exit
syscall
#### EOF #####################################################################
【问题讨论】:
标签: assembly integer mips calc