MIPS汇编语言搜索

时间:2016-02-22 06:23:27

标签: assembly mips dynamic-arrays qtspim

过去两天我一直在努力弄清楚我的程序有什么问题。我已经停止输入评论了,所以我可以调试并让事情尽可能顺利地运行。

我的程序是一个二进制搜索算法,用于位于堆中的动态数组。在排序数组之后的Anwyays,我遇到了一些内存分配问题,现在由于某种原因它不输出结果str或比较它只是循环遍历函数,一个无限循环?我是这种语言的新手,我很感激帮助:)。

我的问题:

#

##Binary Search
##Register Usage:
#a1 holds base address of array
#a2 holds address of last entry
#t3 holds the search value
#t4 holds size of array
#v0 holds the address if found, if not then a 0 will be held in $v0
binarysearchSet:
    #Prompt user for key
    li $v0, 4; #System call code for print_str
    la $a0, bin_search; #bin_search is the argument to print
    syscall; #print the string
#Read the key
    li $v0, 5; #System call code to read the input
    syscall; #read int input
    move $t3,$v0; #copies search value to t3

#find address of last entry and store
findLast:
    addi  $t0, $s0, -1; # N-1 stored in t0
    mul  $t0,$s2,4; #Calculate address offset
    add  $t0,$t0,$s1; #Calculate absolute address
    #lw   $a0,($t0); #load number from heap
    move $a2,$t0; #move address of last entry to a2
    jal binsearch; #Subroutine call

binsearch:
    subu $t0, $a2, $s1; #Calculate size of array
    mul  $t0,$s2,4; #Calculate address offset
    add  $t0,$t0,$s1; #Calculate absolute address
    move $t4, $t0; #Stores size of array in t4
    bnez $t4, searching; #Checks if size > 0 then
    move $v0, $s1;
    lw $t0,($v0);
    beq $t3, $t0, FIN;
    li $v0, 4;
    la $a0, result2;
    #li $v0, 0;
    b FIN;

searching:
    sra $t0, $t4, 1; #Compute offset of middle
    sll $t0, $t4, 2; #divides sum by 2 and calculates offset
    addu $v0, $s1, $t0; #compute address of middle
    #lw $t0, ($v0); #middle entry is stored in t0
    beq $t3, $v0, FIN; #Checks if target value == middle
    blt $t3, $v0, move_back; #if middle is less than search value goto move_back

move_forward:
    addu $a1, $v0, 4;#Search continues forward
    jal binsearch; #recursive call to binsearch
    b FIN;

move_back:
    move $a2, $v0; #the search goes backwards
    jal binsearch; #recursive call to binsearch

FIN:
    li $v0, 4; #System call code for print_str
    la $a0, result; #Argument to be printed is result
    syscall; #Print
   # jr $ra;

#INFINTIE LOOP CAN'T figure out why?

###Clean exit form program

我使用排序算法首先获取数组的大小,然后输入元素直到数组被填充。另外,我想指出我使用堆而不是堆栈来实现我的算法。

谢谢:)

0 个答案:

没有答案