title: MIPS Assembly Language Search
body: <p>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.</p>
  
  <p>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 :).</p>
  
  <p>My issue:</p>
  
  <pre><code>#
  
  ##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 &gt; 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
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks :)</p>