;*******************************************************************************
;* Module    : DELETE.SUB
;* Programmer: Tony Papadimitriou <tonyp@acm.org>
;* Purpose   : Delete a range of characters from a string given index and length
;* Language  : Motorola/Freescale/NXP HC08/9S08 Assembly Language (aspisys.com/ASM8)
;* Status    : FREEWARE Copyright (c) 2016 by Tony Papadimitriou <tonyp@acm.org>
;* Original  : http://www.aspisys.com/code/hc08/delete.html
;* Note(s)   : Use: #Include delete.sub
;* History   : 11.11.12 v1.00 Original
;*******************************************************************************

#ifmain ;-----------------------------------------------------------------------
                    #ListOff
                    #Uses     mcu.inc
                    #ListOn
#endif ;------------------------------------------------------------------------
?_OBJECT_?
;*******************************************************************************
; Purpose: Delete a range of characters from a string given index and length
; Input  : Stack = String pointer, Position, Length
; Output : None
; Pascal : Delete(var s: string; index: Byte; count: Byte)

StringDeleteRange   macro     [#]String,[#]Index,[#]Count
                    mreq      1,2,3:[#]String,[#]Index,[#]Count
                    #push
                    #spauto   :sp
                    push
                    #psp
                    lda       ~3~
                    psha
                    lda       ~2~
                    psha
                    ldhx      ~1~
                    pshhx
                    call      ~0~
                    ais       #:psp
                    pull
                    #pull
                    endm

;-------------------------------------------------------------------------------

                    #spauto   :ab

StringDeleteRange   proc
                    @parms    .str,index,count    ;string pointer, position at which to start, number of chars to delete
                    push
                    #ais
          #ifhcs
                    ldhx      .str@@,sp           ;HX -> string
          #else
                    tsx
                    lda       .str@@,spx
                    ldx       .str@@+1,spx
                    tah
          #endif
                    lda       index@@,sp          ;offset within string
                    deca                          ;make it zero-based
                    @aax                          ;HX -> position to delete

                    lda       count@@,sp          ;A = number of chars to delete

Loop@@              tst       ,x                  ;ASCIZ terminator?
                    beq       Done@@              ;If so, exit

                    push

ToEOS@@             lda       1,x                 ;make next character...
                    sta       ,x                  ;... the current one
                    aix       #1                  ;point to next character
                    bne       ToEOS@@             ;repeat while not terminator

                    pull
                    dbnza     Loop@@

Done@@              @ais      #:ais
                    pull
                    rtc

                    #sp
;*******************************************************************************
                    #Exit
;*******************************************************************************
                    @EndStats

CString             fcs       'Hello World!'

vstring             @var      ::CString

                    #spauto

Start               proc
                    @rsp

                    clrhx
Copy@@              lda       CString,x
                    sta       vstring,x
                    aix       #1
                    bne       Copy@@
          ;---
                    @StringDeleteRange #vstring,#5,#2
          ;---
                    bra       *

                    @vector   Vreset,Start