home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / TPOWER53.ZIP / TPASM.ARC / TPSEARCH.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-07-10  |  4.7 KB  |  196 lines

  1. ;******************************************************
  2. ;           TPSEARCH.ASM    5.07
  3. ;           String handling routines
  4. ;     Copyright (c) TurboPower Software 1987.
  5. ; Portions copyright (c) Sunny Hill Software 1985, 1986
  6. ;     and used under license to    TurboPower Software
  7. ;         All rights reserved.
  8. ;******************************************************
  9.  
  10.     INCLUDE    TPCOMMON.ASM
  11.  
  12. ;******************************************************    Code
  13.  
  14. CODE    SEGMENT    BYTE PUBLIC
  15.  
  16.     ASSUME    CS:CODE
  17.  
  18.     PUBLIC    Search,    SearchUC
  19.  
  20.     EXTRN    UpCasePrim : FAR
  21.  
  22. UpcaseAL    MACRO            ;UpCase    character in AL
  23.         PUSH    BX
  24.         CALL    UpCasePrim
  25.         POP    BX
  26.         ENDM
  27.  
  28. UpcaseAH    MACRO            ;UpCase    character in AL
  29.         XCHG    AL,AH
  30.         UpcaseAL
  31.         XCHG    AH,AL
  32.         ENDM
  33.  
  34.  
  35. ;******************************************************    Search
  36.  
  37. ;function Search(var Buffer; BufLength : Word;
  38. ;         var Match;  MatLength : Word) : Word;
  39.  
  40. ;Search    through    Buffer for Match.
  41. ;BufLength is length of    range to search.
  42. ;MatLength is length of    string to match.
  43. ;Returns number    of bytes searched to find Match, 0FFFFh    if not found.
  44.  
  45. ;equates for parameters:
  46. MatLength    EQU    WORD PTR [BP+6]
  47. Match        EQU    DWORD PTR [BP+8]
  48. BufLength    EQU    WORD PTR  [BP+12]
  49. Buffer        EQU    DWORD PTR [BP+14]
  50.  
  51. Search    PROC FAR
  52.  
  53.     StackFrameBP
  54.     PUSH    DS            ;Save DS
  55.     CLD                ;Go forward
  56.  
  57.     LES    DI,Buffer        ;ES:DI => Buffer
  58.     MOV    BX,DI            ;BX = Ofs(Buffer)
  59.  
  60.     MOV    CX,BufLength        ;CX = Length of    range to scan
  61.     MOV    DX,MatLength        ;DX = Length of    match string
  62.  
  63.     TEST    DX,DX            ;Length(Match) = 0?
  64.     JZ    Error            ;If so,    we're done
  65.  
  66.     LDS    SI,Match        ;DS:SI => Match    buffer
  67.     LODSB                ;AL = Match[1];    DS:SI => Match[2]
  68.     DEC    DX            ;DX = MatLength-1
  69.     SUB    CX,DX            ;CX = BufLength-(MatLength-1)
  70.     JBE    Error            ;Error if BufLength is less
  71.  
  72. ;Search    for first character in Match
  73. Next:    REPNE    SCASB            ;Search    forward    for Match[1]
  74.     JNE    Error            ;Done if not found
  75.     TEST    DX,DX            ;If Length = 1 (DX = 0)    ...
  76.     JZ    Found            ; the "string" was found
  77.  
  78.     ;Search    for remainder of Match
  79.  
  80.     PUSH    CX            ;Save CX
  81.     PUSH    DI            ;Save DI
  82.     PUSH    SI            ;Save SI
  83.  
  84.     MOV    CX,DX            ;CX = Length(Match) - 1
  85.     REPE    CMPSB            ;Does rest of string match?
  86.  
  87.     POP    SI            ;Restore SI
  88.     POP    DI            ;Restore DI
  89.     POP    CX            ;Restore CX
  90.  
  91.     JNE    Next            ;Try again if no match
  92.  
  93. ;Calculate number of bytes searched and    return
  94. Found:    DEC    DI            ;DX = Offset where found
  95.     MOV    AX,DI            ;AX = Offset where found
  96.     SUB    AX,BX            ;Subtract starting offset
  97.     JMP    SHORT SDone        ;Done
  98.  
  99. ;Match was not found
  100. Error:    XOR    AX,AX            ;Return
  101.     DEC    AX            ;Return    FFFF
  102.  
  103. SDone:    POP    DS            ;Restore DS
  104.     ExitCode 12
  105.  
  106. Search    ENDP
  107.  
  108. ;******************************************************    SearchUC
  109.  
  110. ;function SearchUC(var Buffer; BufLength : Word;
  111. ;           var Match;  MatLength : Word) : Word;
  112.  
  113. ;Search    through    Buffer for Match (CASE-INSENSITIVE)
  114. ;BufLength is length of    range to search.
  115. ;MatLength is length of    string to match.
  116. ;Returns number    of bytes searched to find Match, 0FFFFh    if not found.
  117.  
  118. SearchUC  PROC FAR
  119.  
  120.     StackFrameBP
  121.     PUSH    DS            ;Save DS
  122.     CLD                ;Go forward
  123.  
  124.     LES    DI,Buffer        ;ES:DI => Buffer
  125.     MOV    BX,DI            ;BX = Ofs(Buffer)
  126.  
  127.     MOV    CX,BufLength        ;CX = Length of    range to scan
  128.     MOV    DX,MatLength        ;DX = Length of    match string
  129.  
  130.     TEST    DX,DX            ;Length(Match) = 0?
  131.     JZ    SUCError        ;If so,    we're done
  132.  
  133.     LDS    SI,Match        ;DS:SI => Match    buffer
  134.     LODSB                ;AL = Match[1];    DS:SI => Match[2]
  135.     UpcaseAL            ;Uppercase it
  136.     DEC    DX            ;DX = MatLength-1
  137.     SUB    CX,DX            ;CX = BufLength-(MatLength-1)
  138.     JBE    SUCError        ;No match if BufLength is less
  139.  
  140. ;Search    for first character in Match
  141. SUCNext:
  142.     JCXZ    SUCError        ;done if CX is 0 **
  143.     MOV    AH,ES:[DI]        ;Get next character of buffer
  144.     INC    DI            ;To next position
  145.     UpcaseAH            ;Uppercase it
  146.     CMP    AH,AL            ;A match?
  147.     LOOPNE    SUCNext            ;Loop while CX<>0 and AH<>AL
  148.     JNE    SUCError        ;Done if not found
  149. ;**    JCXZ    SUCError        ;wrong position    for the    check
  150.     OR    DX,DX            ;If Length = 1 (DX = 0)    ...
  151.     JZ    SUCFound        ; the "string" was found
  152.  
  153.     ;Search    for remainder of Match
  154.  
  155.     PUSH    AX            ;Save AX
  156.     PUSH    CX            ;Save CX
  157.     PUSH    DI            ;Save DI
  158.     PUSH    SI            ;Save SI
  159.  
  160.     MOV    CX,DX            ;CX = Length(Match) - 1
  161. SUCNextM:
  162.     LODSB                ;Next match character in AL
  163.     UpcaseAL            ;Uppercase it
  164.     MOV    AH,ES:[DI]        ;Next buffer character in AH
  165.     INC    DI            ;Increment index
  166.     UpcaseAH            ;Uppercase it
  167.     CMP    AH,AL            ;A match?
  168.     LOOPE    SUCNextM        ;Loop while AH=AL and CX<>0
  169.  
  170.     POP    SI            ;Restore SI
  171.     POP    DI            ;Restore DI
  172.     POP    CX            ;Restore CX
  173.     POP    AX            ;Restore AX
  174.  
  175.     JNE    SUCNext            ;Try again if no match
  176.  
  177. ;Calculate number of bytes searched and    return
  178. SUCFound: DEC    DI            ;DX = Offset where found
  179.     MOV    AX,DI            ;AX = Offset where found
  180.     SUB    AX,BX            ;Subtract starting offset
  181.     JMP    SHORT SUCDone        ;Done
  182.  
  183. ;Match was not found
  184. SUCError: XOR    AX,AX
  185.     DEC    AX            ;Return    FFFF
  186.  
  187. SUCDone:POP    DS            ;Restore DS
  188.     ExitCode 12
  189.  
  190. SearchUC  ENDP
  191.  
  192.  
  193. CODE    ENDS
  194.  
  195.     END
  196.