insert.xyw

insert.xyw
calendarsave
; Insert char b1 at index b2 within string s
; s b1 b2 -> 
.label insert
  POPy  ; y = index (will be overwritten later) 
  POPx  ; x = b1 (char to insert)
  POPxw ; xw = s
  ; b2 kept on stack
  $00 PSHy ADDxw POPxw ; advance string index to insertion point    
  .label insert.loop
    LDBxw POPy ; y = char at string index
    ; otherwise insert char
    PSHx STBxw 
    ; previously-retrieved char at index (y) becomes next char to insert (x)
    SWPxy 
    ; if at end of string, exit
    $00 EQUy insert.end JCNw
    ; increment string index
    INCxw
    ; repeat
    insert.loop JMPw
  .label insert.end
    RTS