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

  1. ;----------------------------------------------------------------
  2. ;          This is a module in the ASMLIB library.
  3. ;
  4. ; This module will delete a substring from within a string.
  5. ; This comes from 'Z-80 Subroutines By Saville and Leventhal'.
  6. ;
  7. ; On  ENTRY
  8. ;    DE -> start of string
  9. ;     B =  Number of bytes to delete
  10. ;     C = Starting index into the string to start deleting from.
  11. ; On EXIT
  12. ;    Carry = 1 means an error on input paramteres.
  13. ;
  14. ;            Written        R.C.H.        1/10/83
  15. ;            Last Update    R.C.H.        1/10/83
  16. ;----------------------------------------------------------------
  17. ;
  18.     name    'delstr'
  19.     public    delstr
  20.     maclib    z80
  21. ;
  22. delstr:
  23.     sub    a            ; Set A = 0
  24.     sta    delerr            ; Save in the flag
  25.     ora    b            ; Is B = A = 0 ?
  26.     rz                ; Return if no characters to delete
  27.     mov    a,c            ; Load starting index
  28.     ora    a            ; Test start index
  29.     stc
  30.     rz                ; Error if the index is 0
  31. ;
  32. ; Check if the index is within the string limits, error if not.
  33.     ldax    d            ; Get string length
  34.     cmp    c            ; Check string size against index
  35.     rc                ; Return with error flag
  36. ;
  37. ; Check if enough characters are available to be deleted. If not then
  38. ; delete only to the end of the string.
  39. ;
  40.     push    h            ; Save this non-participant
  41.     xchg                ; Load atring address into HL
  42.     mov    a,c
  43.     add    b            ; Add number to delete to index
  44.     jrc    trunc            ; Truncate if > 255
  45.     mov    e,a
  46.     dcr    a
  47.     cmp    m            ; compare to length
  48.     jrc    cntok            ; Jump if enough available
  49.     jrz    trunc            ; Truncate but no errors
  50.     mvi    a,0ffh            ; Load an error flag
  51.     sta    delerr
  52. ;
  53. ; Truncate the string, no compacting needed.
  54. ;
  55. trunc:
  56.     mov    a,c
  57.     dcr    a
  58.     mov    m,a            ; String length = index - 1
  59.     lda    delerr            ; Load error flag
  60.     rar
  61.     pop    h            ; Restore
  62.     ret
  63. ;
  64. ; Here when all counts are ok.
  65. ; Move characters about deleted characters down.
  66. cntok:
  67.     mov    a,m
  68.     mov    d,a            ; Save
  69.     sub    b            ; set new length
  70.     mov    m,a            ; Load string length
  71. ; Calculate number of characters to move.
  72. ;
  73.     mov    a,d
  74.     sub    e            ; subtract index + number of bytes
  75.     inr    a            ; A = characters to move
  76.  
  77. ;
  78. ; Calculate source and destination addresses for the move.
  79. ;
  80.     push    h
  81.     mvi    b,00
  82.     dad    b            ; Now HL = base + index
  83.     xthl                ; source = base + index + number
  84.     mvi    d,00
  85.     dad    d            ; HL = source above deleted area
  86.     pop    d            ; Load destination address
  87.     mov    c,a            ; Load the count
  88. ;
  89. ; Here HL -> start of deleted area
  90. ;      DE -> start of characters to move
  91. ;      BC =  count
  92. ;
  93.     ldir
  94. okexit:
  95.     ora    a            ; Clear carry, no errors
  96.     pop    h            ; Restore
  97.     ret
  98. ;
  99.     dseg
  100. delerr:    db    00            ; Error deleting.
  101.  
  102.     end
  103.  
  104.  
  105.  
  106.