-1

我对 MIPS 比较陌生,并且一直在寻找示例问题来扩展我的技能。我面临这个问题:

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

给定数组 a 和 b,每个 10 个字长,对于每个值 i,其中 0 <= i < 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
4

1 回答 1

0

当减法的结果为负时,您不会更新指针 ( $16and )。$17你应该删除这些行

  nop
  b loop
  nop

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

我在您的代码中注意到的其他一点是,您nop在每个lwand之后都放了一个sw,这不是必需的。

于 2013-05-29T12:02:40.937 回答