home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / stdlib.zip / STRSTRL.ASM < prev    next >
Assembly Source File  |  1990-07-14  |  2KB  |  95 lines

  1. stdlib        segment    para public 'slcode'
  2.         assume    cs:stdlib
  3. ;
  4. ;
  5. ; strstrl- Returns the position of a substring in another string.
  6. ;
  7. ; inputs:
  8. ;
  9. ;    es:di- address of string to search through.
  10. ;    return address- address of substring to search for.
  11. ;
  12. ;
  13. ; returns: 
  14. ;
  15. ;    cx- position of character in string (if present).
  16. ;    carry=0 if character found.
  17. ;    carry=1 if character is not present in string.
  18. ;
  19.         public    sl_strstrl
  20. ;
  21. sl_strstrl    proc    far
  22.         push    bp
  23.         mov    bp, sp
  24.         push    ds
  25.         push    es
  26.         pushf
  27.         push    si
  28.         push    di
  29.         push    ax
  30.         push    bx
  31.         push    dx
  32.         cld
  33.         mov    ax, es
  34.         mov    ds, ax
  35.         mov    si, di
  36.         les    di, 2[bp]
  37. ;
  38.         mov    bx, di        ;Save ptr to substring.
  39. ;
  40. ; Compute the length of the substring:
  41. ;
  42.         mov    cx, 0ffffh
  43.         mov    al, 0
  44.     repne    scasb
  45.         neg    cx
  46.         dec    cx
  47.         dec    cx
  48.         mov    dx, cx        ;Save length of smaller string.
  49.         mov    2[bp], di    ;Save new return address.
  50. ;
  51.         mov    ax, si        ;Save ptr to string.
  52. StrLp:        mov    cx, dx
  53.     repe    cmpsb            ;Compare the strings
  54.         jz    StrsAreEql    ;Jump if substring exists.
  55.         inc    ax        ;Bump pointer into string.
  56.         mov    si, ax        ;Restore pointers.
  57.         mov    di, bx
  58.         cmp    byte ptr [si], 0 ;Done yet?
  59.         jne    StrLp
  60. ;
  61. ; Bad news down here, the substring isn't present in the source string.
  62. ;
  63.         xor    cx, cx
  64.         pop    dx
  65.         pop    bx
  66.         pop    ax
  67.         pop    di
  68.         pop    si
  69.         popf
  70.         pop    es
  71.         pop    ds
  72.         pop    bp
  73.         stc
  74.         ret
  75. ;
  76. StrsAreEql:
  77.         mov    cx, ax            ;Save ptr to string
  78.         pop    dx
  79.         pop    bx
  80.         pop    ax
  81.         pop    di
  82.         sub    cx, di            ;Compute index to substring.
  83.         pop    si
  84.         popf
  85.         clc
  86.         pop    es
  87.         pop    ds
  88.         pop    bp
  89.         ret
  90. sl_strstrl    endp
  91. ;
  92. ;
  93. stdlib        ends
  94.         end
  95.