home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / stdlib.zip / STRINS2L.ASM < prev    next >
Assembly Source File  |  1990-08-08  |  3KB  |  146 lines

  1. stdlib        segment    para public 'slcode'
  2.         assume    cs:stdlib
  3.         extrn    sl_malloc:far
  4. ;
  5. ;
  6. ; strins2l-     Inserts one string within another.
  7. ;
  8. ; inputs:
  9. ;
  10. ;    ES:DI-     Points at destination string, the one into which the source
  11. ;               string will be appended.
  12. ;
  13. ;    CS:RET-    Points at the string to insert.
  14. ;
  15. ;    CX-    Index into source string (ES:DI) to begin insertion.
  16. ;
  17. ; outputs:
  18. ;
  19. ;    ES:DI-    Points at new string on the heap.
  20. ;    Carry-    Zero if no error, One if memory allocation error.
  21. ;
  22. ;
  23. ;
  24.         public    sl_strins2l
  25. ;
  26. rtnadrs        equ    2[bp]
  27. srcseg        equ    -2[bp]
  28. source        equ    -4[bp]
  29. destseg        equ    -6[bp]
  30. dest        equ    -8[bp]
  31. insindx        equ    -10[bp]
  32. ;
  33. sl_strins2l    proc    far
  34.         push    bp
  35.         mov    bp, sp
  36.         push    dx        ;Dummy spot
  37.         push    si        ;Dummy spot
  38.         push    es
  39.         push    di
  40.         push    cx
  41.         pushf
  42.         push    ax
  43.                 push    bx
  44.         push    ds
  45.         push    dx
  46.         push    si
  47. ;
  48.         mov    si, rtnadrs
  49.         mov    source, si
  50.         mov    dx, rtnadrs+2
  51.         mov    srcseg, dx
  52.         cld
  53. ;
  54. ; Compute the length of the destination string.
  55. ;
  56.         mov    al, 0
  57.         mov    cx, 0ffffh
  58.     repne    scasb
  59. ;
  60. ; Compute the length of the string to insert.
  61. ;
  62.         mov    di, si
  63.         mov    es, dx            ;(srcseg)
  64.         mov    al, 0
  65.     repne    scasb
  66.         mov    rtnadrs, di        ;Restore return address.
  67.         neg    cx
  68.         dec    cx
  69. ;;;;;;;;;;;;;;    dec    cx            ;Plus one for the zero byte.
  70. ;
  71. ; Allocate memory for the new string:
  72. ;
  73.         call    sl_malloc
  74.         jnc    GoodMalloc
  75. ;
  76.         pop    si
  77.         pop    dx
  78.         pop    ds
  79.         pop    bx
  80.         pop    ax
  81.         popf
  82.         pop    cx
  83.         pop    di
  84.         pop    es
  85.         add    sp, 4            ;Remove junk bytes
  86.         pop    bp
  87.         stc                ;Out of memory
  88.         ret
  89. ;
  90. ;
  91. ; If we were able to malloc the string, drop down here:
  92. ;
  93. GoodMalloc:    mov    dx, es            ;Save ptr to new string
  94.         mov    bx, di
  95.         lds    si, dword ptr Dest
  96. ;
  97. ; Copy the first part of the destination string to the new string.
  98. ;
  99.         mov    cx, insindx
  100. CpyDest1:    lodsb
  101.         stosb
  102.         cmp    al, 0
  103.         loopne    CpyDest1
  104.         jnz    SkipDec
  105.         dec    di            ;Back up a character if we
  106.         dec    si            ; hit a zero byte.
  107. ;
  108. SkipDec:    push si                ;Save ptr to middle of dest.
  109. ;
  110. ; Copy the source string into the middle.
  111. ;
  112.         lds    si,source
  113. CpySrc:        lodsb
  114.         stosb
  115.         cmp    al, 0
  116.         jnz    CpySrc
  117.         dec    di
  118. ;
  119. ; Copy the remainder of the destination string to the new string.
  120. ;
  121.         pop    si            ;Retrieve ptr into dest.
  122.         mov    ds, DestSeg
  123. CpyDest:    lodsb
  124.         stosb
  125.         cmp    al, 0
  126.         jnz    CpyDest
  127.         mov    es, dx            ;Retrieve ptr to new string.
  128.         mov    di, bx
  129. ;
  130.         pop    si
  131.         pop    dx
  132.         pop    ds
  133.         pop    bx
  134.         pop    ax
  135.         popf
  136.         pop    cx
  137.         add    sp,8            ;Don't restore es:di or junk.
  138.         pop    bp
  139.         clc                ;Allocated string okay.
  140.         ret
  141. sl_strins2l    endp
  142. ;
  143. ;
  144. stdlib        ends
  145.         end
  146.