0

I've been trying to figure out whats wrong with my program for the past two days.. got to the point where I stopped typing comments just so I could debug and get things running as smoothly as possible.

My program is a binary search algorithm being used on a dynamic array located in a heap. Anwyays after sorting the array, I ran into some memory allocation issues and now for some reason it doesn't output the result str or compare it just loops over the function, an infinite loop? I'm new to this language and I'd appreciate the help :).

My issue:

#

##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

I'm using a sorting algorithm to firstly take the size of the array, then to input elements until the array is filled. Also I'd like to point out I'm using a Heap rather than a stack to implement my algorithm.

Thanks :)

Alexander
  • 9
  • 1
  • 3
  • `addi $t0, $a0, -1; # N-1 stored in t0` Why `$a0`? That doesn't make any sense, since the last thing you assigned to it was the address of `bin_search`. – Michael Feb 22 '16 at 06:47
  • This isn't the whole program, bin_search is used to take down the int the user wants to search in the now sorted array. This input is then copied over from v0 to t3 for later access. Well that's my understanding :/ Oh wait I think i get you now, it should be addi $t0, $a1, -1 right? – Alexander Feb 22 '16 at 15:08
  • That depends on what `N` is supposed to be. – Michael Feb 22 '16 at 15:21
  • N is the size of the array - defined by int user input – Alexander Feb 22 '16 at 15:44
  • Ok, then `addi $t0, $a1, -1` is obviously not right either, because `$a1` holds the base address of the array (at least according to your comments). – Michael Feb 22 '16 at 15:54
  • I managed to sort out the registers. I'll update the code above – Alexander Feb 22 '16 at 19:33

0 Answers0