* *``````````````````````````````* * SUBINS :: INSERT SUBSTRING * *- -* * INSERT A SUBSTRING INTO * * ANOTHER STRING. * *- -* * CLOBBERS: * * * * FLAGS: ????---- REG: AXYM * *- -* * CYCLES: ??? * * SIZE: * *- -* * USAGE: * * * * LDA #STR ; STRING TO INS TO * * PHA * * LDA #SUB ; SUBSTRING TO INS * * PHA * * LDA # 255 CMP :MLEN ; BCC :IDXLEN ; BR IF S1+S2 LEN < MAX LENGTH BEQ :IDXLEN ; BR IF EQUAL * ** SUBSTRING DOES NOT FIT, SO TRUNCATE IT * :TRUNC LDA :MLEN ; SUBSTR LEN = MLEN - STR LEN SEC SBC :S1LEN BCC :EREXIT BEQ :EREXIT ; ERR IF MLEN < STR LEN OR 0 ; (ORIGINAL STRING WAS TOO LONG) STA :S2LEN LDA #$0FF STA :SCERR ; INDICATE SUBSTR WAS TRUNCATED * ** CHECK THAT INDEX IS WITHIN STRING. IF NOT, CONCAT ** SUBSTR ONTO THE END OF THE STRING. * :IDXLEN LDA :S1LEN CMP :SINDEX ; BCS :LENOK ; BR IF INDEX WITHIN STR LDX :S1LEN ; ELSE CONCAT SUB AT END OF STR INX STX :SINDEX ; START RIGHT AFTER END OF STR LDA #$0FF STA :SCERR ; INDICATE ERR IN INSERT LDA :S1LEN CLC ADC :S2LEN STA :S1LEN ; ADD LENGTHS TOGETHER JMP :MVESUB ; PERFORM MOVE, NOTHING ELSE TODO * ** OPEN UP A SPACE IN THE SOURCE STRING FOR THE ** SUBSTRING BY MOVING THE CHARACTERS FROM THE END ** OF THE SOURCE STRING DOWN TO INDEX, UP BY ** THE SIZE OF THE STRING. * :LENOK * ** CALC NUMBER OF CHARS TO MOVE ** COUNT = STR LEN - START INDEX + 1 * LDA :S1LEN SEC SBC :SINDEX TAX INX ; X= NUM OF CHARS TO MOV * ** SET THE SOURCE INDEX AND CALC DEST INDEX * LDA :S1LEN STA :SIDX ; SRC ENDS AT ORIG STR END CLC ADC :S2LEN STA :SBIDX ; DEST ENDS FURTHER BY SUB LEN STA :S1LEN ; SET NEW LENGTH TO THIS ALSO * :OPNLP LDY :SIDX LDA (ADDR1),Y LDY :SBIDX STA (ADDR1),Y ; MOVE IT UP IN MEM DEC :SIDX DEC :SBIDX ; DE DEST IDX, COUNTER DEX BNE :OPNLP ; CONT UNTIL COUNTER = 0 * ** MOVE THE SUBSTR INTO THE OPEN AREA * :MVESUB LDA #1 STA :SIDX ; START AT ONE IN THE SUBSTR ; START AT INDEX IN THE STRING LDX :S2LEN ; X = NUM OF CHARS TO MOVE * :MVELP LDY :SIDX LDA (ADDR2),Y ; GET NEXT CHAR LDY :SINDEX STA (ADDR1),Y INC :SIDX ; INC SUBSTR INDEX INC :SINDEX ; INC STR INDEX DEX ; DEC COUNTER BNE :MVELP ; CONT UNTIL COUNTER = 0 LDA :SCERR ; GET ERROR FLAG BNE :EREXIT ; BR IF SUBSTR WAS TRUNCED * :OKEXIT CLC BCC :EXIT :EREXIT SEC ; ERROR EXIT :EXIT LDA :S1LEN LDY #0 STA (ADDR1),Y RTS * ** DATA * :S1LEN DS 1 :S2LEN DS 1 :SUBLEN DS 1 :MLEN DS 1 :SINDEX DS 1 :SIDX DS 1 :SBIDX DS 1 :SCERR DS 1 *