SUBINS PLA TAY PLA TAX PLA STA ADDR2 PLA STA ADDR2+1 PLA STA :MLEN PLA STA :SINDEX PLA STA ADDR1 PLA STA ADDR1+1 TXA PHA TYA PHA LDA #0 STA :SCERR ; ASSUME NO ERR WILL BE FOUND LDY #0 LDA (ADDR1),Y STA :S1LEN ; GET LENGTH OF STRING LDA (ADDR2),Y STA :S2LEN ; GET LENGTH OF SUB BNE :IDX0 JMP :OKEXIT ; EXIT OF NO INSERT/ERR :IDX0 LDA :SINDEX BNE :CHKLEN ; BR OF INDEX NOT 0 JMP :EREXIT ; ELSE ERROR EXIT :CHKLEN LDA :S2LEN ; GET SUBSTR LENGTH CLC ADC :S1LEN BCS :TRUNC ;TRUN IF S1+S2 LENGTH > 255 CMP :MLEN ; BCC :IDXLEN ; BR IF S1+S2 LEN < MAX LENGTH BEQ :IDXLEN ; BR IF EQUAL :TRUNC LDA :MLEN ; SUBSTR LEN = MLEN - STR LEN SEC SBC :S1LEN BCC :EREXIT BEQ :EREXIT ; ERR IF MLEN < STR LEN OR 0 STA :S2LEN LDA #$0FF STA :SCERR ; INDICATE SUBSTR WAS TRUNCATED :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 :LENOK LDA :S1LEN SEC SBC :SINDEX TAX INX ; X= NUM OF CHARS TO MOV 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 :MVESUB LDA #1 STA :SIDX ; 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 :S1LEN DS 1 :S2LEN DS 1 :SUBLEN DS 1 :MLEN DS 1 :SINDEX DS 1 :SIDX DS 1 :SBIDX DS 1 :SCERR DS 1