home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / asmlib.lbr / INSSTR.AZM / INSSTR.ASM
Encoding:
Assembly Source File  |  1991-06-25  |  3.0 KB  |  135 lines

  1. ;----------------------------------------------------------------
  2. ;         This is a module in the ASMLIB library
  3. ;
  4. ; This module will insert a string into another string.
  5. ; This is from 'Z-80 Subroutines By Saville and Leventhal'
  6. ;
  7. ; On entry
  8. ;    DE -> String to insert into
  9. ;    HL -> string to insert
  10. ;     B = Maximum length of the result string
  11. ;     C = index into string to insert into.
  12. ;
  13. ;On Exit
  14. ;   Carry = 1 indicates start index = 0 or length of the substring is 0
  15. ;   if the result string is > 255 then then the carry is set and only enough
  16. ;   characters are moved.
  17. ;
  18. ;                Written        R.C.H.    1/10/83
  19. ;                Last Update    R.C.H.    9/10/83
  20. ;----------------------------------------------------------------
  21. ;
  22.     name    'insstr'
  23.     public    insstr
  24.     maclib    z80
  25. ;
  26. insstr:
  27.     sub    a
  28.     sta    inserr            ; Clear error flag
  29.     xchg                ; reset pointers for asmlib
  30.     ldax    d            ; Get substring length
  31.     ora    a
  32.     rz
  33. ;
  34. ; If starting index = 0 then error exit
  35.     mov    a,c
  36.     ora    a
  37.     stc
  38.     rz                ; Return a carry if index = 0
  39. ;
  40. ; Check if the insertion will make the string too long
  41. ;
  42.     ldax    d
  43.     add    m            ; Add string to substring
  44.     jrc    trunc            ; Truncate if > 255
  45.     cmp    b            ; Compare to string max length
  46.     ldax    d
  47.     jrc    idxlen            ; Jump if total < max length
  48.     jrz    idxlen            ; Jump if total = max len
  49. ;
  50. ; Here the substring does not fit so truncate it. Set the error flag
  51. ; to indicate this. 
  52. ;
  53. trunc:
  54.     mvi    a,0ffh
  55.     sta    inserr            ; Save insert error flag
  56.     mov    a,b
  57.     sub    m            ; 
  58.     rc                ; Return if string too long
  59.     stc
  60.     rz                ; Or if no room for substring
  61. ;
  62. ; Check if the index is within limits.
  63. ;
  64. idxlen:
  65.     mov    b,a            ; Get length of substring
  66.     mov    a,m            ; Get string length
  67.     cmp    c            ; Compare to index
  68.     jrnc    lenok            ; Jump if index inside string
  69. ;
  70. ; Here and the index is not within the string so we concatenate
  71. ; the two strings together.
  72. ;
  73.     mov    c,a
  74.     add    b            ; Add length of string
  75.     mov    m,a            ; Set new string length
  76. ;
  77.     xchg
  78.     mov    a,c
  79.     inr    a
  80.     add    e
  81.     mov    e,a
  82.     jrnc    idxl1
  83.     inr    d
  84. idxl1:
  85.     mvi    a,0ffh            ; Indicate insertion error
  86.     sta    inserr
  87.     jr    mvesub            ; Move the string
  88. ;
  89. ; Here and we must make room for the substring to be inserted into
  90. ; the string.
  91. ;
  92. lenok:
  93.     push    b
  94.     push    d
  95.     mov    e,a
  96.     mvi    d,0
  97.     add    b
  98.     mov    m,a            ; Store new string length
  99. ; Calculate number of characters to move
  100.     mov    a,e
  101.     sub    c
  102.     inr    a            ; Number to move
  103.     dad    d            ; HL -> last characters in string
  104.     mov    e,l
  105.     mov    d,h            ; Copy
  106. ; Generate destination address
  107.     mov    c,b            ; Length of substring
  108.     mvi    b,0
  109.     dad    b
  110.     xchg                ; Now HL = start, DE = dest
  111.     mov    c,a
  112.     lddr                ; Open the substring
  113.     xchg
  114.     inx    d            ; De is new destination address
  115.     pop    h
  116.     pop    b
  117. ;
  118. ; Here we can move the substring into the string, there is room for it.
  119. ;
  120. mvesub:
  121.     inx    h
  122.     mov    c,b
  123.     mvi    b,00            ; Clear top of counter
  124.     ldir                ; Do the move
  125.     lda    inserr            ; Load the insert error flag
  126.     rar
  127.     ret
  128. ;
  129.     dseg
  130. inserr:    db    00            ; Insert error flag
  131.     end
  132.  
  133.  
  134.  
  135.