I am trying to find out how to make a binary search subroutine on an ORDERED array, using m68k. For Java, I would do
int binSearch(int key, int &lo, int &hi)
{
if (hi < lo)
return NOT_FOUND; //RETURN with V = 1
int mid = (lo+hi) / 2;
if (key == array[mid])
return mid;
else if (key < array[mid]) // go left
return binSearch(key, lo, mid-1); // left
else
return binSearch(key, mid+1, hi); // right
}
Im trying to put that to assembly. What I have so far is
link A6,#0
movem.l D1/A1-A2,-(sp)
move.w 8(A6),D1 *key t
movea.l 10(A6),A1 *lo
movea.l 14(A6),A2 *hi
cmpa.l A1,A2 *if hi>lo
BHS else
move.l A1,D1 *low D1
add.l A2,D1 *adds hi
asr.l #1,D1 * divide by 2
Basically, what do I do at this point? Do I compare D1 to the number im searching for, and then depending on if it is lower and higher, call the subroutine again? Does D1 hold the number at the midway point like I want it to do, or am I wrong? Thank you in advance!