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