所以赋值是在MIPS中编写一个带有数组的函数,将数组的所有内容添加到一起,然后返回总和。主要的问题是我是Assembly的新手,并不完全确定如何将数组传递给Assembly中的函数,这是我到目前为止所拥有的:
# Title: homework2.asm
# Author: Jacob Suarez
# Date: 9/28/14
# Purpose: Create a function that can find the sum of the elements in an array, and run two arrays through it
# Text Segment
.text
.globl __start
__start:
li $t0, 0 # $t0 <- sum
li $a0, 0 # $a0 <- array index
# Set arr1 as the argument for the sumArr function
la $t4, arr1
sw $t4, arrx
# Set upper bound of array index
li $t9, 4
# Call function to calculate the sum of arr1
jal sumArr
sw $v0, sum1
la $a0, sum1
# Print sum1 from $a0
li $v0, 1
syscall
la $a0, end1
li $v0, 4
syscall
# Reset function arguments to 0
li $t0, 0 # $t0 <- sum
li $a0, 0 # $a0 <- array index
# Set arr2 as the argument for the sumArr function
la $t4, arr2
sw $t4, arrx
# Set upper bound of array index
li $t9, 5
# Call function to calculate the sum of arr2
jal sumArr
sw $v0, sum2
la $a0, sum2
# Print sum2 from $a0
li $v0, 1
syscall
la $a0, end2
li $v0, 4
syscall
# End program
li $v0, 10
syscall
sumArr:
lw $t2, arrx($a0) # $t2 <- value in the array
add $t0, $t0, $t2 # sum += A[i]
addi $a0, $a0, 4 # i++ in bytes
blt $a0, $t9, sumArr
# Move contents of $t0 to $a0
move $t0, $a0
# Return
jr $ra
# Data Segment
.data
arrx: .space 100
arr1: .word 8,12,20,40
arr2: .word 15,25,35,55,-129
sum1: .word 0
sum2: .word 0
end1: .asciiz " is the sum of arr1.\n"
end2: .asciiz " is the sum of arr2.\n"
# end of file homework2.asm
现在,这一切都运行良好,但问题是结果似乎只是从内存中未初始化的空间中拉出来的,所以我只得到像4268501128这样的数字或任何明显错误的数字,所以我认为我的问题是如何我将数组传递给函数,特别是代码的这一部分:
# Set arr1 as the argument for the sumArr function
la $t4, arr1
sw $t4, arrx
任何提示?或者我的问题完全是另一回事?
编辑:我认为我的问题可能实际上是数组没有正确初始化,或者我是如何从中获取数据的,但我无法弄清楚是否实际情况是这样,因为似乎没有关于我如何初始化它们的任何错误。