1
我寫了一個MIPS相當於下面的C快速排序程序排序算法實現在MIPS
while(L<=R){
if(A[M] < n)
L=M+1;
else if(A[M]==n){
printf("%d found in %d\n",n,M);
break;
}
else
R=M-1;
M=(L+R)/2;
}
其中我寫了MIPS等同,我有檢查程序用於任何其他錯誤,但它沒有表現出一個
# $t0 is index of Leftmost entry of array considered
# $t3 is index of Rightmost entry of array considered
# $t1 is index of MIddle entry of array considered
# $t2 is the adderess of middle entry (i.e. $t1*4)
# myArray is an array I took as input , with $s1 elements
addi $t0, $zero, 0 # $t0 stores left
add $t1, $s1, $t0
srl $t1, $t1, 1 #stores mid
sll $t2, $t1, 2
add $t3, $s1, $zero
BinSearch:
bge $t0, $t3 , exit
lw $t6, myArray($t2)
blt $t6, $s2, leftindex # if(A[mid]<k) goto left
beq $t6, $s2, found
bgt $t6, $t2, rightindex
leftindex:
addi $t0, $t1, 1 # left index leftindex= mid + 1
add $t1, $t3, $t0 # mid = left + right
srl $t1, $t1, 1
sll $t2, $t1, 2
j BinSearch #goback to Binary Search
found:
li $v0, 1
add $a0, $t1, $zero
syscall
j exit #exit programme
rightindex:
addi $t3, $t1, -1 #rightindex = mid -1
add $t1, $t3, $t0 #mid = left+right
srl $t1, $t1, 1
sll $t2, $t1, 2 #mid/2
j BinSearch #goback to binarysearch
我已經檢查過,如果我把數組作爲輸入或如果我犯了其他愚蠢的錯誤的錯誤。
所以我的問題是,是否有一些錯誤,我在執行算法在MIPS中犯了錯誤?或者我做了什麼其他錯誤? 預先感謝您。
我應該考慮的問題跑,而不是一個_quicksort_實現更深;) – doynax
@doynax是的。代碼段足夠稀少,所有可以完成的工作就是修復asm以匹配C代碼。即使在C代碼中,片段如何適應也不清楚。我必須_assume_ OP的C原始碼是正確的(即教科書quicksort impl)。剛剛問[有效]關於asm是否與C匹配的問題,所以... –
對不起,我沒有很好笑。 C代碼是二進制搜索的教科書實現,如果目標是實現排序,這可能會成爲一個問題。 – doynax