After much tinkering, I think I've got my first mini-program working ok, and I can't see any way to make it more efficient.
If anyone's masochistic enough I'd appreciate comments for improvements, or advice on where I've done things in unconventional ways.
No pressure as I can imagine that reading someone else's ML is pretty dreary, but I'm keen to improve and have never written any ML that's worked before, so I'm really proud!
The code is for a text editor, and basically inserts a new character at the address referenced in ($5FFB-$5FFC), moving everything after that point to the end of the file ($5FFD-$5FFE) up one.
Variables:
$5FFD-$5FFE = Last address in text file => Activated to $FD-$FE and used as iterator
$5FFB-$5FFC = Cursor / Insertion Point => Activated to $FB -$FC
$5FFF = Character to insert
The test data I used inserts W into 'HELLO ORLD":
@5FFD : 09 40 (End of File - becomes Iterator)
@5FFB : 06 40 (Insertion point)
@5FFF : 57 (char to insert) = W
@4000 "HELLO ORLD"
Code: Select all
@2000 LDA $5FFB : STA $FB ; Activate variables to ZP
LDA $5FFC : STA $FC
LDA $5FFD : STA $FD
LDA $5FFE : STA $FE
LDX #$00
LDY #$01
-L LDA ($FD,X) ; Copy character to next address
STA ($FD),Y
LDA $FD ; 16-bit Compare Iterator to Insertion point
CMP $FB
BNE -Z
LDA $FE
CMP $FC
BNE -Z
LDA $5FFF ; Insert new character
STA ($FD,X)
INC $5FFD ; 16-bit Increment End of File
BNE ->
INC $5FFE
-@ RTS
-Z LDA $FD ; 16-bit Decrement Iterator
BNE ->
DEC $FE
-@ DEC $FD
LDA #$00 ; Loop
BEQ -L