home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Current Shareware 1994 January
/
SHAR194.ISO
/
dos_util
/
pcmcvt.zip
/
PCMASM.ZIP
/
SEARCH.INC
< prev
next >
Wrap
Text File
|
1993-05-22
|
3KB
|
74 lines
;SEARCH.INC
;Copyright 1993 (c) Jay Munro
;Based on algorithm in CSInstr from Crescent Software
;SearchString compares two strings, returning position in AX if match. If
; a partial match occurs, then the partial position is in BX
SearchString Proc Near Uses ES DI, Strt:Word, SrcStr:Word, SLen:Word, Search:Word, SrcLen:Word
Cld ;forward moves on LodSb
Mov DX,Strt ;get the address for StrStr
Dec DX ;adjust StrStr so 1st character = 0 offset
Or DX,DX ;make sure we didn't pass a zero length
Js NotFound ;no, get out and assign SearchString = 0
Mov CX,SLen ;put LEN(SrcStr) into CX
Jcxz NotFound ;it's null, get out and assign SearchString = 0
Sub CX,DX ;consider only remaining length of SrcStr
Jle NotFound ;not enough remains, it couldn't be in there
Mov BX,SrcStr ;now BX points to the beginning of SrcStr
Mov SI,BX ;set SI to point to the beginning of SrcStr
Add SI,DX ;now SI points to the correct Strt offset
Mov ES,BX ;remember SrcStr Starting address in ES
Xor BX,BX ;set up BX for exit in case Search is null
Mov DX,SI ;remember where we are in SrcStr, in case
; a false partial match takes us on a wild
; goose chase - lets us resume in SrcStr
Mov DI,Search ;now DI points to the beginning of Search
Mov BP,SrcLen ;put LEN(Search) into BP
Or BP,BP ;is Search null?
Jz Found ;yes, get out and assign SearchString = Strt
Jmp Short StrtScan
Reset:
Inc DX ;point to next character in SrcStr
Mov SI,DX ;point SI to the next character too
Dec CX ;consider only remaining length of SrcStr
StrtScan:
Xor BX,BX ;zero out BX as an index into Search
Cmp BP,CX ;is the rest of Search longer than SrcStr?
Jg NotFoundP ;yes, it couldn't possibly be in there
Okay:
Cmp BX,BP ;did we exhaust Search?
Je Found ;yes, and if we made it this far it's there
Mov AH,[DI+BX] ;get the current character in Search
Inc BX ;point to the next character for next time
Lodsb ;load the current SrcStr character into AL
Cmp AL,AH ;are they the same?
Jnz Reset ;no, consider the next character in SrcStr
Jmp Short Okay ;yes, continue comparing
NotFound:
Xor SI,SI ;clear SI to assign AX = 0 below
Mov BX,SI ;clear BX too
Jmp Short SrchExit
NotFoundP: ;partial not found
Xor SI,SI ;clear SI to assign AX = 0 below
Mov BX,-1 ;lets caller know that we ran out of string
Jmp Short SrchExit
Found:
Mov DX,ES ;retrieve the original SrcStr Start address
Sub SI,DX ;calculate the number of characters searched
Sub SI,BX ;because we ended up past where it was found
Inc SI ;the inevitable "fudge factor"
SrchExit:
Mov AX,SI ;assign SearchString = SI
Ret ;return to caller
SearchString Endp