!lm10
!rm76
Applesoft GOTO from Assembly Language.........Bob Sander-Cederlof

Bob Potts called the other day with an interesting question.  Suppose you want to jump to a particular line (by line number) of an Applesoft program, rather than simply returning from an assembly language program.

For example, I might call an assembly language subroutine at $300 with "CALL 768".  After it does its job, the subroutine may decide either to return to the following Applesoft statement by an "RTS" instruction, or to GOTO a particular line number in the program.  (Perhaps an error processing subroutine in the Applesoft code.)  Can it be done?

Yes, and it is fairly simple.  First we need to put the binary value of the line number into locations $50 and $51.  Then we must jump to $D944 in the Applesoft ROMs to finish the GOTO operation.  Here is the code to jump to line number 1350, for example:
!lm15

GOTO1350 LDA #1350   LOW BYTE OF "1350"
         STA $50
         LDA /1350   HIGH BYTE OF "1350"
         STA $51
         JMP $D955   APPLESOFT GOTO PROCESSOR

!lm10
That's all there is to it!

I wrote a tiny little subroutine to demonstrate that this works.  It expects to find the line number in $2FE and $2FF.  You can POKE it there before CALLing 768.  Here is my subroutine:

   <code here>

Now here is a test program in Applesoft.  Can you tell what it will do before you try it?  The first two lines poke in the GOTO subroutine.  The next five lines call the subroutine for successive values 1000, 2000, 3000 etc. up to 9000.  The code in line 10000 jumps back to line 140 to continue the loop.  Try it!
