简体   繁体   English

MIPS-使用循环故障将值存储到数组中

[英]MIPS - storing values into an array using a loop trouble

I am relatively new to MIPS and have been finding example problems to expand my skills. 我是MIPS的新手,一直在寻找示例问题来扩展我的技能。 I am faced with this problem: 我面临着这个问题:

Subtract elements of one arrary from another and replace values in the first array. 从另一个数组中减去一个数组的元素,并替换第一个数组中的值。

Given arrays a and b, each 10 words long, for each value i where 0 <= i < 10, let c = A[i] - B[i]. 给定数组a和b,每个数组长10个字,对于每个值i,其中0 <= i <10,令c = A [i]-B [i]。 Then, if c < 0 set A[i] = 0. Otherwise, set A[i] = c. 然后,如果c <0,则设置A [i] =0。否则,设置A [i] = c。

Complete the code so that the data values are correctly loaded, computed and stored. 完成代码,以便正确加载,计算和存储数据值。 You must use a loop to iterate through the loads and stores 您必须使用循环来遍历加载和存储

Here is my coded attempt, however I notice my $12 and $13 are not loading in the first value of a and b respectively as I would like them to. 这是我的编码尝试,但是我注意到我的$12$13并没有按我希望的那样分别加载ab的第一个值。 Any help would be very greatly appreciated! 任何帮助将不胜感激!

.data 0x10010000
.word 23     # a[0]
.word 6
.word 11
.word 7
.word 44
.word 32
.word 9
.word 16
.word 29
.word 13

.data 0x10010040
.word 6  # b[0]
.word 22
.word 9
.word 1
.word 3
.word 15
.word 10
.word 4
.word 30
.word 8


.text
.globl main



main:

lui $16, 0x1001  # $16 contains the address of a[0]

add $17, $16, 0
ori $17, $17, 0x0040     # $17 contains the address of b[0]
li $9, 0                #initiate iterator value
loop: 
lw $12, 0($16)      #load register with value of a
nop
lw $13, 0($17)      #load register with value of b
nop

li $8, 0                #set c to 0
sub $8, $12, $13        # c = A(i) - B(i)
blt $8, 0, negative     # if c is negative branch
nop
li $12, 0
add $12, $12, $8        #else A(i) = c
addi $9, $9, 1          #add 1 to iteration value
sw $12, 0($16)          #store value back into a
nop
bne $9, 10, update      #check that we have not the last array
nop
b exit                  #if so finish program
nop
negative:
li $12, 0
sw $12, 0($16)          #store value back into a
addi $9, $9, 1          #add 1 to iteration value
beq $9, 10, exit
nop
b loop
nop
update:
add $16, $16, 4         #move to the next value in a
add $17, $17, 4         #move to next value in b
b loop
nop
exit:
b exit
nop

You are not updating your pointers ( $16 and $17 ) when the result of the subtraction is negative. 当减法的结果为负时,您不会更新指针( $16$17 )。 You should remove the lines 您应该删除行

  nop
  b loop
  nop

located after the beq $9, 10, exit after the negative label. 位于beq $9, 10, exit之后,在negative beq $9, 10, exit之后beq $9, 10, exit

Something else I noticed in your code is that you put a nop after every lw and sw , which is not needed. 我在您的代码中注意到的另一点是,您不需要在每lwsw之后加一个nop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM