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

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