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

  1. ;----------------------------------------------------------------
  2. ;       This is a module in the ASMLIB library.
  3. ;
  4. ; This module will copy a substring from within another string. This
  5. ; is similar to the PL/I-80 SUBSTR function. 
  6. ; On entry DE -> original string. HL -> new string, B = bytes to copy.
  7. ;   C = index to start of bytes, A = maximum length of the substring.
  8. ; On exit, if the carry is set, this indicates that an illegal length
  9. ; was used which would exceed the length of the substring or major string.
  10. ; This software comes from the 'Z-80 Subroutines By Saville and Leventhal'
  11. ; and is modified to suit RMAC and ASMLIB parameter conventions.
  12. ;
  13. ;                Written        R.C.H.     1/10/83
  14. ;                Last Update    R.C.H.       1/10/83
  15. ;----------------------------------------------------------------
  16. ;
  17.     name    'cpystr'
  18.     public    cpystr
  19.     maclib    z80
  20. ;
  21. cpystr:
  22.     xchg                ; Load suitable for ASMLIB
  23.     sta    maxlen            ; Substring maximum
  24.     sub    a            ; Clear a
  25.     stax    d            ; Make destination string length = 0
  26.     sta    cpyerr            ; Clear copy error flag
  27.     ora    b            ; Test number of bytes to copy
  28.     rz                ; Exit no character, Carry = 0
  29. ;
  30. ; Check if maximum length is 0
  31.     lda    maxlen
  32.     ora    a
  33.     jrz    erexit
  34. ;
  35. ; If starting index is 0 then error exit also
  36.     mov    a,c            ; Get starting index
  37.     ora    a
  38.     jrz    erexit            ; Error exit if index = 0
  39. ;
  40. ; If start index > length of string the error exit also
  41.     mov    a,m            ; Get major string length
  42.     cmp    c            ; Check against index
  43.     rc                ; Exit with a carry for error
  44. ;
  45. ; Check if the copy area will fit the source string else copy only to the
  46. ; end of the string.
  47. ;
  48.     mov    a,c
  49.     add    b            ; Add copy length to index
  50.     jrc    recalc            ; jump if sum > 255 (over full)
  51.     dcr    a
  52.     cmp    m
  53.     jrc    cnt1ok            ; jump if more than enough to copy
  54.     jrz    cnt1ok            ; Jump if exactly enough
  55. ;
  56. ; Caller asked for too many characters, return everything between the index
  57. ; and the end of the source string:
  58. ;
  59. recalc:
  60.     mvi    a,0ffh
  61.     sta    cpyerr            ; Save an error flag
  62.     mov    a,m            ; Load count length
  63.     sub    c
  64.     inr    a
  65.     mov    b,a            ; Load as the number of bytes
  66. ;
  67. ; Check if count is less than or equal to the maximum length of
  68. ; the destination string. If not then make the length of the destination
  69. ; string into the byte counter.
  70. ;
  71. cnt1ok:
  72.     lda    maxlen            ; Get maximum length
  73.     cmp    b            ; Compare to bytes to copy
  74.     jrnc    cnt2ok            ; This is ok then too
  75.     mov    b,a            ; Else re-load the counter
  76.     mvi    a,0ffh
  77.     sta    cpyerr            ; Flag an error again.
  78. cnt2ok:
  79.     mov    a,b
  80.     ora    a            ; Is the number of bytes to move = 0
  81.     jrz    erexit            ; ERROR Exit if so
  82.     mvi    b,00            ; Else start at the index position
  83.     dad    b            ; Index to the index pos'n.
  84.     stax    d            ; Set length of destination string
  85.     mov    c,a            ; Load as a 16 bit counter
  86.     inx    d
  87. ; Here HL -> source string start + index
  88. ;      DE -> destination string start of characters.
  89. ;      BC = Number of bytes to be moved.
  90. ;
  91.     ldir                ; Move it all
  92.     lda    cpyerr            ; Get the copy error flag
  93. okexit:
  94.     ora    a
  95.     ret
  96. ;
  97. erexit:
  98.     stc                ; Set the carry flag
  99.     ret
  100. ;
  101.     dseg
  102. maxlen:    db    00            ; Maximum length of dest string
  103. cpyerr:    db    00            ; Error flag
  104. ;
  105.     end
  106.  
  107.  
  108.  
  109.