在MIPS中,我有这个代码,我不知道如何制作它,以便有大小由向量控制的数组。喜欢 - “请输入矢量的大小。”你选择五个,所以数组大小是五个。 “输入数组1的数字”。您输入10 10 10 10 10
。然后它要求你输入数组2的大小。最后它显示了数组1和数组2的总和和平均数。这是我到目前为止所拥有的。
.data
prompt: .asciiz "Enter 10 Elements: \n"
avg: .asciiz "Average of these numbers is: "
sum: .float 0
count: .float 10
.text
main:
#size of the vector using prompt
la $a0 prompt
li $v0 4
syscall
li $t1, 40 # t1 has count
li $t2, 0 # t2 is iterator
l.s $f12, sum # t3 has sum
while: bge $t2, $t1, end
li $v0, 6
syscall
add.s $f12, $f12, $f0
addi $t2, $t2, 4
b while
end:
la $a0 avg
li $v0, 4
syscall
lwc1 $f10, count
div.s $f12, $f12, $f10
li $v0, 2
syscall
li $v0, 10
syscall
li $v0, 2
lwc1, $f12 sum
答案 0 :(得分:1)
我花了一点时间来刷新我对MIPS的记忆。我写了一个小程序,询问用户矢量的大小。使用该大小然后动态分配两个整数数组并提示用户填充所述数组。一旦数组被填充,通过用户输入,程序然后打印第一个数组,然后是其总和,然后是第二个数组,后跟其总和。
我注意到您的代码使用了浮点数。由于我不知道这是家庭作业还是学校项目,我想我会让你弄清楚如何转换下面的代码来使用浮点数。另外,我故意省略代码来计算平均值,原因相同(但我确实留下了可能添加代码的注释)。修改程序以使用浮点数并添加平均计算不应该很难。如果您在本答复的评论部分需要进一步的帮助,请与我们联系。
对于代码中某些注释的间距感到抱歉,从我使用的程序复制并没有那么顺利。
# ====== 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 --