home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / LIB / STRSOURC.ZIP / STPOS.ASM < prev   
Encoding:
Assembly Source File  |  1993-03-07  |  4.0 KB  |  82 lines

  1.  
  2. ; *******************************************************
  3. ; *                                                     *
  4. ; *     Turbo Pascal Runtime Library Version 7.0        *
  5. ; *     String Pos Function                             *
  6. ; *                                                     *
  7. ; *     Copyright (C) 1990-1993 Norbert Juffa           *
  8. ; *                                                     *
  9. ; *******************************************************
  10.  
  11.              TITLE   STPOS
  12.  
  13.  
  14. CODE         SEGMENT BYTE PUBLIC
  15.  
  16.              ASSUME  CS:CODE
  17.  
  18. ; Publics
  19.  
  20.              PUBLIC  SPos
  21.  
  22. ;-------------------------------------------------------------------------------
  23. ; Pos standard function: Pos (VAR Obj, Target: STRING): BYTE
  24. ;
  25. ; Pos searches the string Target for the first occurence of a string Obj. If Obj
  26. ; is found in Target, the index at which the first character of Obj appears in
  27. ; Target is returned. If Obj is not found in Target, zero is returned. The
  28. ; routine searches the first character of Obj in the first Len(Target)-Len(Obj)
  29. ; charcters of Target. Whenever there is a match, the following characters of
  30. ; Target are compared to the characters in Obj. If all characters match, Obj has
  31. ; been succesfully found in Target.
  32. ;
  33. ; On entry:  [SP+4] Address of Target
  34. ;            [SP+8] Address of Obj
  35. ;
  36. ; On exit:   AX     index at which Obj first found in Target (0 if not found)
  37. ;-------------------------------------------------------------------------------
  38.  
  39. SPos         PROC    FAR
  40.              CLD                       ; auto-increment for string instructions
  41.              PUSH    DS                ; save TURBO-Pascal data segment
  42.              PUSH    BP                ; save TURBO-Pascal's frame pointer
  43.              MOV     BP, SP            ; make new frame pointer
  44.              LES     DI, [BP+8]        ; address of target string
  45.              LDS     SI, [BP+12]       ; address of object string
  46.              MOV     AX, [SI]          ; AL=length of object, AH=1. char of obj.
  47.              INC     SI                ; adjust
  48.              INC     SI                ;  string pointer
  49.              XOR     CH, CH            ; init result
  50.              DEC     AX                ; length - 1 (changes AH only when AL=0)
  51.              MOV     CL, ES:[DI]       ; length of target string
  52.              SUB     CL, AL            ; targetlength <= objectlength - 1 ?
  53.              JBE     $not_found        ; yes, object not found
  54.              MOV     BH, CL            ; save length of target string
  55.              INC     DI                ; first char of target string
  56.              XCHG    AL, AH            ; AL=1. char of object, AH=length of obj.
  57.              MOV     BP, SI            ; save offset into object
  58. $search:     REPNZ   SCASB             ; scan target string for 1. char of obj.
  59.              JNZ     $not_found        ; exit, if not found
  60.              MOV     BL, CL            ; save remaining length of target string
  61.              MOV     CL, AH            ; number of char to be compared
  62.              MOV     DX, DI            ; save offset into target string
  63.              REPZ    CMPSB             ; compare remaining char of target & obj.
  64.              MOV     SI, BP            ; restore offset into object string
  65.              MOV     DI, DX            ; restore offset into target string
  66.              MOV     CL, BL            ; restore remaining length of target str
  67.              JNZ     $search           ; cont. search, if not all chars matched
  68.              SUB     BH, BL            ; Pos = targetlength - remain. targetlen.
  69.              MOV     CH, BH            ; Pos
  70. $not_found:  POP     BP                ; restore TURBO-Pascal's frame pointer
  71.              POP     DS                ; restore TURBO-Pascal data segment
  72.              MOV     AL, CH            ; Pos
  73.              XOR     AH, AH            ; clear msb
  74.              RET     8                 ; return and pop parameters
  75. SPos         ENDP
  76.  
  77.              ALIGN   4
  78.  
  79. CODE         ENDS
  80.  
  81.              END
  82.