我正在学习MIPS。我试着在Quicksort上编写一个程序(没有递归)。 我写了以下代码。它需要排序的元素数量的元素和数字作为输入搜索,返回数字的数组索引,
.data
.align 2
myArray: .space 4000 #1000 elements atmost
prompt1: .asciiz "Enter n : "
prompt2: .asciiz " "
prompt3: .asciiz "\nEnter Sorted Array: "
prompt4: .asciiz "Enter number to be sorted"
.text
main:
li $v0, 4
la $a0, prompt1
syscall
li $v0, 5
syscall
move $s1, $v0, #n
addi $s0, $s1, -1
li $v0, 4
la $a0, prompt4
syscall
li $v0, 5
syscall
move $s2, $v0 #stores k , number to be searched
addi $t0, $zero, 0 #clear values in $t0
li $v0, 4
la $a0, prompt3
syscall
input:
li $v0, 5 # reading inputs
syscall
add $t1, $t0, $zero
add $t3, $v0, $zero
sll $t1, $t0, 2
sw $t3, myArray($t1)
addi $t0, $t0, 1
slt $t1, $s0, $t0
beq $t1, $zero, input
addi $t0, $zero, 0 # $t0 stores left
sll $t1, $s0, 2 #t1 stores right
add $t2, $t1, $t0
srl $t2, $t2, 1 # $t2 stores mid
BinSearch:
bge $t0, $t1 , exit
lw $t6, myArray($t2) #showing Error here
blt $t6, $s2, leftindex # if(A[mid]<k) goto left
beq $t6, $s2, found
bgt $t6, $t2, rightindex
leftindex:
addi $t0, $t2, 1 # left index leftindex= mid + 1
add $t2, $t1, $t0 # mid = left + right
srl $t2, $t2, 1 # mid/2
j BinSearch #goback to Binary Search
found:
li $v0, 1 #if(A[leftindex]==k)
addi $a0, $t0, 0 #print index of k
syscall
j exit #exit programme
rightindex:
addi $t1, $t2, -1 #rightindex = mid -1
add $t2, $t1, $t0 #mid = left+right
srl $t2, $t2, 1 #mid/2
j BinSearch #goback to binarysearch
exit:
li $v0, 10
syscall
它给了我错误:异常4 [实例/数据获取中的Adderess错误]
它在第70-72行给出了我的错误
lw $t6, myArray($t2)
blt $t6, $s2, leftindex
我在代码中也提到该行作为评论
请告诉我我做错了什么。