'=========================================================================== ' Subject: BINARY SEARCH SUBROUTINE Date: 03-10-00 (13:16) ' Author: Randall L. Glass Code: PB ' Origin: rlglass@yahoo.com Packet: PB.ABC '=========================================================================== '------------------------------------------------------------------------ ' Correction For PowerBasic Inline Assembly Search Subroutines ' ' Assembly binary search subroutine correction ' ' Donated to Public Domain By Randall Glass ' E-Mail Address rlglass@yahoo.com ' ' '---------------------------------------------------------------------------- ' For some reason i decided to retest my assembly search programs. ' This time I added a test routine to let my computer look for problems. ' all of them work perfectly except for my binary search program ' it worked perfectly except it did not find the very last string in ' in the string array. ' '-------------------------------------------------------------------------- ' corrected Binary search subroutine FUNCTION BinSearch% (byval Lower%,byval Upper%,Find$,a$(),BYVAL ArrayHandleAddr???) Public DIM FindPtr AS DWORD,Middle AS INTEGER,Ln AS WORD ! LES BX,Find$ ! MOV AX,ES:[BX] ! PUSH AX ! CALL GetStrLoc ; find where the data is located ! JCXZ BinSearchDone1 ! JMP Continue BinSearchDone1: ! JMP BinSearchDone Continue: ! MOV FindPtr???[2],DX ; put segment in ES ! MOV FindPtr???[0],AX ; put segment in ES ! MOV Ln??,CX 'WHILE Lower% < Upper% BinaryWhile: ! MOV BX,Lower% ! MOV DX,Upper% ! cmp BX,DX ! ja BinSearchDone ; !!!! this was jae caused my problem 'if Upper% >= Lower% then Quit ! MOV AX,BX ;CX = Upper% ! ADD AX,DX ! shr AX,1 ;AX = Middle% ' Middle% = (Upper% + Lower%) \ 2 'start testing in middle ! MOV Middle%,AX ! LES DI,ArrayHandleAddr??? ! SHL AX,1 ! ADD DI,AX ! MOV AX,ES:[DI] ! PUSH AX ! call GetStrLoc ; find where the data is located ! JCXZ BinSearchDone ! mov ES, DX ; put segment in ES ! mov DI, AX ; put offset in DI ! MOV DX,Ln?? ! MOV BX,CX ! CMP DX,CX ! JG NoMin ! MOV CX,DX NoMin: ! PUSH DS ! LDS SI,FindPtr??? NeMinLen: ! CMPSB ! JA AisGTFind ! JB AisLTFind ! LOOP NeMinLen ! CMP BX,DX ! Jl AisGtFind ! Je BinaryFoundIt AisLTFind: ! POP DS ! MOV AX,Middle% ! MOV Upper%,AX ! JMP BinaryWhile AisGtFind: ! POP DS ! MOV AX,Middle% ! INC AX ! MOV Lower%,AX !JMP BinaryWhile BinaryFoundIt: ! POP DS ! MOV AX,Middle% ! MOV FUNCTION,AX BinSearchDone: END FUNCTION