MIPS中的排序算法实现

时间:2017-02-16 05:27:02

标签: assembly mips

我写了一个相当于以下C Quicksort程序的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中实现算法时我是否犯了一些错误?或者我做的其他错误? 提前谢谢你。

1 个答案:

答案 0 :(得分:1)

我认为有一些错误。

首先,设置R(即$t3)时,它被设置为数组计数但应设置为计数-1。

其次,根据C代码,bge for循环退出应为bgt

第三,要跟踪C代码,bgtrightIndex应为b(即无条件分支)

我已经创建了两个版本的代码。带注释的一个和一个修复了错误和一些简化[请原谅无偿风格的清理]。

这是带注释的:

# $s1 is count of array
# $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 address 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

# NOTE/BUG: if C were the array count, this sets R = C, but we need R = C - 1
# so change this as follows:
    ###add      $t3,$s1,$zero
    addi        $t3,$s1,-1

# NOTE/BUG: based on the C code, the bge should be bgt
BinSearch:
    ###bge      $t0,$t3,exit
    bgt     $t0,$t3,exit
    lw      $t6,myArray($t2)

    blt     $t6,$s2,leftindex       # if(A[mid]<k) goto left
    beq     $t6,$s2,found

# NOTE/BUG: to track the C code, this should be uncondition
    ###bgt      $t6,$t2,rightindex
    b       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

这是清理和简化的:

# $s1 is count of array
# $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 address of middle entry (i.e. $t1*4 )
# myArray is an array I took as input , with $s1 elements
    li      $t0,0                   # $t0 stores left
    addi    $t3,$s1,-1              # $t3 stores right

BinSearch:
    bgt     $t0,$t3,exit            # L<=R (i.e. L>R) ? if no, we're done

    add     $t1,$t3,$t0             # mid = left + right
    srl     $t1,$t1,1               # mid /= 2

    sll     $t2,$t1,2
    lw      $t6,myArray($t2)

    blt     $t6,$s2,leftindex       # if(A[mid]<k) goto left
    beq     $t6,$s2,found
    b       rightindex

leftindex:
    addi    $t0,$t1,1               # leftindex = mid + 1
    j       BinSearch               # goback to Binary Search

rightindex:
    addi    $t3,$t1,-1              # rightindex = mid - 1
    j       BinSearch               # goback to binarysearch

found:
    li      $v0,1
    add     $a0,$t1,$zero
    syscall
    j       exit                    # exit programme