home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH15 / INSERT.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-25  |  1.8 KB  |  88 lines

  1. ; INSERT- Inserts one string into another.
  2. ;
  3. ; On entry:
  4. ;
  5. ; DS:SI Points at the source string to be inserted
  6. ;
  7. ; ES:DI Points at the destination string into which the source
  8. ; string will be inserted.
  9. ;
  10. ; DX Contains the offset into the destination string where the
  11. ; source string is to be inserted.
  12. ;
  13. ;
  14. ; All registers are preserved.
  15. ;
  16. ; Error condition-
  17. ;
  18. ; If the length of the newly created string is greater than 255,
  19. ; the insert operation will not be performed and the carry flag
  20. ; will be returned set.
  21. ;
  22. ; If the index is greater than the length of the destination
  23. ; string,
  24. ; then the source string will be appended to the end of the destin- ; ation string.
  25.  
  26. INSERT        proc    near
  27.         push    si
  28.         push    di
  29.         push    dx
  30.         push    cx
  31.         push    bx
  32.         push    ax
  33.         clc            ;Assume no error.
  34.         pushf
  35.         mov    dh, 0        ;Just to be safe.
  36.  
  37. ; First, see if the new string will be too long.
  38.  
  39.         mov    ch, 0
  40.         mov    ah, ch
  41.         mov    bh, ch
  42.         mov    al, es:[di]    ;AX = length of dest string.
  43.         mov    cl, [si]    ;CX = length of source string.
  44.         mov    bl, al        ;BX = length of new string.
  45.         add    bl, cl
  46.         jc    TooLong        ;Abort if too long.
  47.         mov    es:[di], bl    ;Update length.
  48.  
  49. ; See if the index value is too large:
  50.  
  51.         cmp    dl, al
  52.         jbe    IndexIsOK
  53.         mov    dl, al
  54. IndexIsOK:
  55.  
  56. ; Now, make room for the string thatâ•’s about to be inserted.
  57.  
  58.         push    si        ;Save for later.
  59.         push    cx
  60.  
  61.         mov    si, di        ;Point SI at the end of current
  62.         add    si, ax        ; destination string.
  63.         add    di, bx        ;Point DI at the end of new str.
  64.         std
  65.     rep    movsb            ;Open up space for new string.
  66.  
  67. ; Now, copy the source string into the space opened up.
  68.  
  69.         pop    cx
  70.         pop    si
  71.         add    si, cx        ;Point at end of source string.
  72.     rep    movsb
  73.         jmp    INSERTDone 
  74.  
  75. TooLong:        popf
  76.         stc
  77.         pushf
  78.  
  79. INSERTDone:        popf
  80.         pop    ax
  81.         pop    bx
  82.         pop    cx
  83.         pop    dx
  84.         pop    di
  85.         pop    si
  86.         ret
  87. INSERT        endp
  88.