我花了一点时间来刷新我对 MIPS 的记忆。我编写了一个小程序,询问用户向量的大小。然后使用该大小动态分配两个整数数组并提示用户填充所述数组。通过用户输入填充数组后,程序会打印第一个数组及其总和,然后是第二个数组及其总和。
我注意到您的代码使用浮点数。由于我不知道这是家庭作业还是学校项目,我想我会让你弄清楚如何将下面的代码转换为使用浮点数。此外,出于同样的原因,我故意省略了计算平均值的代码(但我确实将 cmets 留在了可能添加代码的地方)。修改程序以使用浮点数并添加平均计算应该不难。如果您需要此答案的 cmets 部分的进一步帮助,请告诉我。
对不起,代码中一些cmets的间距,从我使用的程序中复制的不是那么顺利。
矢量程序
# ====== DATA SEGMENT ====== #
.data
prompt: .asciiz "Enter Size of the Vector:\n"
enter: .asciiz "Enter the "
first: .asciiz " numbers in the first array:\n"
second: .asciiz " numbers in the second array:\n"
sum: .asciiz "Sum of these numbers is: "
avg: .asciiz "Average of these numbers is: "
open: .asciiz "[ "
close: .asciiz " ]\n"
comma: .asciiz ", "
lf: .asciiz "\n"
a1: .word 0 # first array
a2: .word 0 # second array
# syscall variables
printi: .word 1 # print_int
printf: .word 2 # print_float
prints: .word 4 # print_string
readi: .word 5 # read_int
readf: .word 6 # read_float
sbrk: .word 9
exit: .word 10
# ====== TEXT SEGMENT ====== #
.text
main:
# initialize and describe registers
li $s0, 0 # vector size
li $s1, 0 # first array's sum
li $s2, 0 # second array's sum
li $s6, 0 # temporary sum store
li $s7, 0 # temporary array address store
li $t9, 0 # loop iterator
# prompt for vector size
la $a0, prompt
lw $v0, prints
syscall
# get vector size integer into $s0
lw $v0, readi
syscall
add $s0, $zero, $v0 # store vector size in $s0
# dynamically allocate first array
mul $t1, $s0, 4 # put size * 4 in $t1
add $a0, $zero, $t1 # put mul result in $a0
lw $v0, sbrk # allocate size * 4 bytes in memory
syscall
sw $v0, a1 # put address returned by sbrk in a1
# dynamically allocate second array
lw $v0, sbrk # allocate another size * 4 bytes in memory
syscall
sw $v0, a2 # put address returned by sbrk in a2
# prompt for first array elements
jal print_enter
la $a0, first
lw $v0, prints
syscall
# fill the first array and calculate sum
lw $s7, a1
jal array_fill
add $s1, $zero, $s6 # put first sum in $s1
# prompt for second array elements
jal print_enter
la $a0, second
lw $v0, prints
syscall
# fill the second array and calculate sum
lw $s7, a2
jal array_fill
add $s2, $zero, $s6 # put second sum in $s2
# print the first array
lw $s7, a1
jal print_array
# print the sum of the first array
la $s6, ($s1)
jal print_sum
# TODO: Add array1 average calculation and output
# print the second array
lw $s7, a2
jal print_array
# print the sum of the second array
la $s6, ($s2)
jal print_sum
# TODO: Add array2 average calculation and output
b end
print_enter:
# print "Enter the "
la $a0, enter
lw $v0, prints
syscall
# print the number of elements to enter
add $a0, $zero, $s0
lw $v0, printi
syscall
jr $ra
array_fill:
la $s6, ($zero) # set tmp sum to 0
la $t9, ($zero) # set iterator to 0
array_fill_for: bge $t9, $s0, end_array_fill
# get integer from user
lw $v0, readi
syscall
sw $v0, ($s7) # store integer at current array index
add $s6, $s6, $v0 # keep a running sum in $s6
add $s7, $s7, 4 # get address of next array element
add $t9, $t9, 1 # increment iterator
b array_fill_for
end_array_fill:
jr $ra
print_array:
# print opening bracket of array
la $a0, open
lw $v0, prints
syscall
la $t9, ($zero) # set iterator to 0
add $t0, $s0, -1 # loop over size-1 elements
print_array_for: bge $t9, $t0, end_print_array
# print array element at current index
lw $a0, ($s7)
lw $v0, printi
syscall
# print a seperating comma
la $a0, comma
lw $v0, prints
syscall
add $s7, $s7, 4 # get address of next array element
add $t9, $t9, 1 # increment iterator
b print_array_for
end_print_array:
# print last index of array without comma
lw $a0, ($s7)
lw $v0, printi
syscall
# print closing bracket of array
la $a0, close
lw $v0, prints
syscall
jr $ra
print_sum:
# print the sum greeting
la $a0, sum
lw $v0, prints
syscall
# print the sum that's in $s6
la $a0, ($s6)
lw $v0, printi
syscall
# print a line feed
la $a0, lf
lw $v0, prints
syscall
jr $ra
end:
lw $v0, exit
syscall
测试输入/输出
Enter Size of the Vector:
4
Enter the 4 numbers in the first array:
1
2
3
4
Enter the 4 numbers in the second array:
5
6
7
8
[ 1, 2, 3, 4 ]
Sum of these numbers is: 10
[ 5, 6, 7, 8 ]
Sum of these numbers is: 26
-- program is finished running --
我发现一些有用的链接