Arrays Mips-尝试从数组加载值时地址超出范围
我需要做的是编写函数rot1,这样它可以旋转任意大小的数组,比如10,20,30,40->40,30,20,10。我得到一个地址超出范围错误的一行看到交换功能,但我完全卡住了。有人介意让我走上正轨吗Arrays Mips-尝试从数组加载值时地址超出范围,arrays,runtime-error,mips,Arrays,Runtime Error,Mips,我需要做的是编写函数rot1,这样它可以旋转任意大小的数组,比如10,20,30,40->40,30,20,10。我得到一个地址超出范围错误的一行看到交换功能,但我完全卡住了。有人介意让我走上正轨吗 .data .align 2 a1: .word 10,20,30,40 # first array, size 4 .text la $a0,a1 li $a1,4 jal rotl rotl: addi $sp
.data
.align 2
a1: .word 10,20,30,40 # first array, size 4
.text
la $a0,a1
li $a1,4
jal rotl
rotl:
addi $sp,$sp,-8 # Save $s0 and $s1 on the stack
sw $s0,0($sp)
sw $s1,4($sp)
# Finding end of array (array size of 4 ending would be 12)
subi $t3,$a1,1
add $t3,$t3,$t3
add $t3,$t3,$t3 # end of array
# loading inital values
li $t2,0 # first value in array
li $s1,0 # s1 is our loop counter
swap: slt $t0,$s1,$a1 # See if we're done yet
beq $t0,$zero,rotdone # exit loop if s1 >= a1 (i.e., if counter >= array size)
add $t2,$t2,$a0
add $t3,$t3,$a0
lw $t1,($t2) # store first index value (10) in $t1 ##Error here: line 149: Runtime exception at 0x00400190: address out of range 0x20020004
lw $t4,($t3) # store last index value (40) in $t4
sw $t4,($t2) # swap first index value for last index value (10->40)
sw $t1,($t3) # swap last index value for first index value (40->10)
addi $t2,$t2,4 # add 4 to $t2 so we move to next index (20)
subi $t3,$t3,4 #sub 4 from $t3 so we move back an index (30)
addi $s1,$s1,1 # add 1 to loop counter
j swap
您的代码中有几个错误。从你给出的例子中,你没有尝试做一个旋转,而是一个反转,从10,20,30,40开始的旋转应该是40,10,20,30,但是你说它应该是40,30,20,10。 因此,假设您正在尝试进行反转,那么您应该: 仅交换一半在您的示例中,您交换了4次,前两次将执行实际的反转,后两次将恢复原始序列 从交换循环中删除$t2和$t3与$a0的添加。在交换开始之前,应该只加载这些寄存器一次 检查在rotl例程结束后将执行的操作,当它被写入时,它将再次继续执行交换 总之,这可能解决前两个问题:
add $a1, $a1, 1
srl $a1, $a1, 1
move $t2, $a0
add $t3, $t3, $a0
swap:
slt $t0,$s1,$a1 # See if we're done yet
beq $t0,$zero,rotdone # exit loop if s1 >= a1 (i.e., if counter >= array size)
lw $t1,($t2) # store first index value (10) in $t1
lw $t4,($t3) # store last index value (40) in $t4
sw $t4,($t2) # swap first index value for last index value (10->40)
sw $t1,($t3) # swap last index value for first index value (40->10)
addi $t2,$t2,4 # add 4 to $t2 so we move to next index (20)
subi $t3,$t3,4 #sub 4 from $t3 so we move back an index (30)
addi $s1,$s1,1 # add 1 to loop counter
j swap
谢谢你的回复。你让我意识到我应该旋转阵列。我把旋转误认为是反转。