我正在尝试在汇编中实现冒泡排序,并且我在下面的代码中出现了一个超出范围的错误。我无法弄清楚如何解决这个问题。我希望有人可以看看,看看哪里可以纠正错误。
.data
vector: .word 1, 3, 5, 7, 9, 0, 2, 4, 6, 8
size: .word 10
.text
main:
li $v0, 4 # enables syscall to load address of string
la $a0, vector # pass the address of array (base of array) in $a0
addi $a1, $zero, 10 # pass the array size in $a1
jal sort
la $a0, vector
addi $a1, $zero, 10 # pass the array size in $a1
jal print_array
li $v0, 10
syscall
print_array:
addi $sp, $sp, -4 # make room for 1 word on the stack
sw $s0, 0($sp) # save $s0 onto the stack
addi $t0, $zero, 0# initialize the array loop iterator $t0
addi $t1, $a0, 0 # save $a0, holding array base, into $t1
# as it will get overwritten by the print utility
loop:
beq $t0, $a1, end # test if iterator $t0 is equal to array size $a1
lw $a0, 0($t1) # bring array contents at $t1 into argument $a0
li $v0, 1 # enable printing
syscall # print what's in $a0
addi $t0, $t0, 1 # increment array iterator
addi $t1, $t1, 4 # increment address in memory by 1 word
lw $s0, 0($sp)
addi $sp, $sp, 4
j loop
end:
jr $ra
sort:
move $t9, $v0 # t9=Address of string
lw $t1, size # $t1 = size
li $t0, 0 # $t0 = 0 (i)
loop1:
beq $t1, $t0, exit # if $t0 == size, exit
addi $t4, $t0, -1 # $t4(J) = counter-1
loop2:
bltz $t4, exit2 # j < 0
mul $t5, $t4, 2 # $t5 = 2 * j
add $t5, $t9, $t5 # $t5 = a[2j]
**lb $t2, 0($t5) # $t2 = a[j]** ##error here
lb $t3, 1($t5) # $t3 = a[j+1]
ble $t3, $t2, else # a[j+1] >= a[j]
j swap
else:
addi $t4, $t4, -1 # --j
j loop2
exit:
move $v0, $t9 #Return $t9 (sorted string)
jr $ra
exit2:
addi $t0, $t0, 1 # ++i
j loop1
swap:
sb $t3, 2($t5)
sb $t2, 1($t5)
j loop2