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

  1. stdlib        segment    para public 'slcode'
  2.         assume    cs:stdlib
  3. ;
  4.         extrn    sl_malloc:far
  5. ;
  6. ; strdup- On entry, es:di points at a source string.  Strdup allocates
  7. ;      storage for a new string the same size and copies the data from
  8. ;      the source string to the new destination string.  Returns a ptr
  9. ;      to the new string in ES:dI.  Calls malloc to allocate storage
  10. ;      for the new string.
  11. ;
  12. ; inputs:
  13. ;        es:di-  Address of string to copy.
  14. ;
  15. ; outputs:
  16. ;        es:di-  Ptr to newly allocated string.
  17. ;
  18. ;
  19.         public    sl_strdup
  20. ;
  21. sl_strdup    proc    far
  22.         push    ds
  23.         push    cx
  24.         push    ax
  25.         pushf
  26.         push    si
  27. ;
  28.         mov    ax, es
  29.         mov    ds, ax
  30.         cld
  31.         mov    al, 0
  32.         mov    cx, 0ffffh
  33.         mov    si, di
  34.     repne    scasb
  35.         neg    cx
  36.         dec    cx
  37.         push    cx
  38.         call    sl_malloc
  39.         pop    cx
  40.         jc    QuitStrDup
  41.         push    di
  42.         shr    cx, 1
  43.         jnc    IsWord
  44.         lodsb
  45.         stosb
  46. IsWord:    rep    movsw
  47. ;
  48.         pop    di
  49.         pop    si
  50.         popf
  51.         pop    ax
  52.         pop    cx
  53.         pop    ds
  54.         clc
  55.         ret
  56. ;
  57. QuitStrDup:    pop    si
  58.         popf
  59.         pop    ax
  60.         pop    cx
  61.         pop    ds
  62.         stc
  63.         ret
  64. ;
  65. sl_strdup    endp
  66. ;
  67. ;
  68. stdlib        ends
  69.         end
  70.