home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TP_ADV.ZIP / LIST0422.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-07-31  |  2.6 KB  |  89 lines

  1.           INCLUDE MACROS.ASM
  2.  
  3. CODE      SEGMENT BYTE PUBLIC
  4.           ASSUME CS:CODE
  5.  
  6.           PUBLIC SearchBlock
  7.  
  8. ;SearchBlock(var FindStr; FindSize : Word; var Block;
  9. ;            BlockSize : Word) : Word;
  10.  
  11. FindStr          EQU DWORD PTR [BP+0Eh]
  12. FindSize         EQU WORD  PTR [BP+0Ch]
  13. Block            EQU DWORD PTR [BP+08h]
  14. BlockSize        EQU WORD  PTR [BP+06h]
  15.  
  16. SearchBlock      PROC FAR
  17.        PUSH      BP
  18.        MOV       BP,SP           ; stack frame with BP
  19.        PUSH      DS
  20.        XOR       AX,AX
  21.        MOV       CX,BlockSize
  22.        JCXZ      ExitCode        ; if BlockSize = 0 then all done
  23.        MOV       BX,CX
  24.  
  25.        MOV       DX,FindSize
  26.        OR        DX,DX
  27.        JZ        ExitCode        ; if FindSize = 0 then all done
  28.  
  29.        CMP       DX,BX
  30.        JA        ExitCode        ; FindStr is bigger than Block
  31.  
  32.        SUB       CX,DX           ; adjust CX for size of FindStr
  33.        INC       CX
  34.  
  35.        CLD                       ; forward string ops
  36.  
  37.        LES       DI,Block        ; the block to search is dest
  38.        _Normal_  ES,DI
  39.  
  40. FindFirstChar:
  41.        LDS       SI,FindStr      ; the string to find is source
  42.  
  43.        LODSB                     ; load first byte into AL
  44.        REPNE     SCASB           ; look for match of first byte
  45.        JNZ       NotFound        ; if Z flag not set then exit
  46.  
  47.        MOV       AX,CX           ; save CX in AX
  48.        MOV       CX,DX           ; get FindSize
  49.  
  50.        PUSH      DI              ; push posit in block in case
  51.                                  ; we need to restart search
  52.  
  53.        DEC       CX              ; we already found first char
  54.        JCXZ      Bingo           ; if FindSize was 1 then we
  55.                                  ; found it!
  56.  
  57. FindRest:
  58.        REPE      CMPSB           ; cmp source to dest while =
  59.        JZ        Bingo           ; if Z is set then we found it
  60.  
  61.        POP       DI              ; restore index into dest
  62.        MOV       CX,AX           ; restore loop counter
  63.        JCXZ      NotFound        ; no more block to search
  64.        INC       CX
  65.        LOOP      FindFirstChar   ; re-start the search
  66.  
  67. NotFound:
  68.        XOR       AX,AX           ; if we make it here then the
  69.        JMP       SHORT ExitCode  ; jump to exit code
  70.  
  71. Bingo:
  72.        POP       DI              ; we must pop DI off the stack
  73.  
  74.        SUB       BX,DX           ; account for FindSize
  75.        INC       BX
  76.        SUB       BX,AX           ; calculate posit
  77.  
  78.        MOV       AX,BX           ; store as function result
  79.  
  80. ExitCode:
  81.        POP       DS
  82.        POP       BP
  83.        RET       0Ch
  84. SearchBlock      ENDP
  85.  
  86. CODE   ENDS
  87.  
  88.        END
  89.