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

时间:2013-05-29 10:57:29

标签: arrays assembly mips

我对MIPS相对较新,并且一直在寻找扩展我的技能的示例问题。我遇到了这个问题:

  

从一个档案中减去另一个档案的元素并替换其中的值   第一个数组。

     

给定数组a和b,每个10个字长,每个值i为0   &lt; = i&lt; 10,设c = A [i] - B [i]。然后,如果c < 0设置A [i] = 0。   否则,设置A [i] = c。

     

完成代码以便正确加载数据值,   计算和存储。您必须使用循环来迭代加载   和商店

以下是我的编码尝试,但我注意到我的$12$13未分别加载ab的第一个值,因为我希望它们。任何帮助将非常感谢!

.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

1 个答案:

答案 0 :(得分:0)

当减法结果为负数时,您没有更新指针($16$17)。 你应该删除行

  nop
  b loop
  nop

位于beq $9, 10, exit标签后的negative之后。

我在代码中注意到的其他事项是,您在noplw之后添加了sw,这是不需要的。

相关问题