【问题标题】:How to make a vector that controls the size of two arrays如何制作一个控制两个数组大小的向量
【发布时间】:2016-03-09 02:10:52
【问题描述】:

在 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

【问题讨论】:

  • 我搞砸了,打算将其标记为 MIPS,而不是 c++。对不起。
  • 一切都很好:)“两个数组的大小由一个向量控制”是什么意思?我有一段时间没有使用 MIPS,但我想提供帮助。
  • 好的,谢谢。我要做的是编写一个汇编程序,首先提示用户输入向量的大小,然后提示用户从键盘输入两个数字或向量列表。然后它将显示原始向量及其总和平均值。这有帮助吗?
  • 提示用户输入向量的大小,因为向量的大小就是数组的大小。向量大小 = “用户输入”------ Array1 大小 = “向量大小” 与 Array2 相同。这有意义吗?
  • 我这么认为?假设它要求您输入向量的大小并选择 5。它要求您输入 Array1 的数字。您将输入 5 个数字,因为您选择 5 作为向量。与 Array2 相同,由于矢量,您只能输入 5 个数字。

标签: arrays assembly mips


【解决方案1】:

我花了一点时间来刷新我对 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 --

我发现一些有用的链接

【讨论】:

  • 非常感谢!抱歉,我没有尽快回复,我不得不帮助我的姐妹们,而且我上了大学。谢谢你所做的一切。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多