OK, i suceeded to port and optimize a little. But the results are not fully tested. So far, the algo seems fast, but the resulting position are not accurated.
[List: D$ 2, 3, 5, 7, 11, 13, 17 ,19, 23]
[ValuetoFInd: D$ 5]
call BinarySearch List, 9, ValuetoFInd
Proc BinarySearch:
Arguments @Array, @ArrayLen, @ValuetoFind
Local @iCounter
Uses ebx, edi, ecx, esi
mov eax 0-1
On D@ArrayLen = 0, ExitP
mov D@iCounter 0
; calculate the closest power of 2 (log(X-1))/log(2)
mov esi D@ArrayLen
dec esi | jz L1>
mov ecx 32
bsr esi ecx
shl esi cl
L1:
mov edi D@ValuetoFind
mov ecx D@Array
mov ebx esi
...If ebx <> 0
.Do
mov eax D$ecx+esi*4
..If eax <= D$edi
mov D@iCounter esi
..Else
shr ebx 1 | jz L1>>
L2:
mov eax D@iCOunter | or eax ebx
mov eax D$ecx+eax*4
If eax <= D$edi
mov eax D@iCOunter | or eax ebx | mov D@iCounter eax
End_If
shr ebx 1 | jz L1>>
jmp L2<
..End_If
shr ebx 1
mov esi D@iCounter | or esi ebx
.Loop_Until ebx = 0
L1:
...End_If
;;
..If D@iCounter = 0
mov eax D@Array
mov ecx D@ValuetoFind
mov edx D$eax
If edx > D$ecx
mov eax 0-1
Else
mov eax D@iCounter
End_If
..Else
mov eax D@iCounter
..End_If
;;
mov eax D@Array
mov ecx D@ValuetoFind
mov edx D$eax
.If_Or D@iCounter = 0, edx <= D$ecx
mov eax D@iCounter
.Else
mov eax 0-1
.End_If
EndP
The problem seems that either he goes to the proper position on return, or it goes back 1 position
For example, when using
[List: D$ 2, 3, 5, 7, 11, 13, 17 ,19, 23]
[ValuetoFInd: D$ 17]
call BinarySearch List, 9, ValuetoFInd
He found value 17, but it assumes it is at position 5, (falling the number 13), insteas at position 6 (when it is located the value 17)