MAGIC GOSUB
Magic Gosub lets any ordinary Applesoft program GOSUB to a text label, instead of a line number. This makes programming in Applesoft BASIC much easier. For example, you can have code that looks like this:
10 REM "MAIN_MENU"
50 GOSUB "SHOW_MENU"
60 GOSUB "PROMPT"
70 GOSUB "HANDLE_ENTRY"
90 GOSUB @"MAIN_MENU" (the @ makes this a GOTO, not GOSUB)
99 END
1000 REM SUB^SHOW_MENU
...
1099 RETURN
5000 REM SUB^PROMPT
...
5099 RETURN
51300 REM SUB^HANDLE_ENTRY
...
51399 RETURN
It doesn't matter what the line numbers of your subroutines and entry points are, and if you change them, no biggie -- you don't have to go back and fix all your GOSUBs and GOTOs. It's that easy!
Magic Gosub works on any Apple II with Applesoft BASIC. Applesoft itself is not modified in any way.
You can use Magic Gosub without BLOADing anything by adding ten special REM statements to your program in lines 0-9. If you use an emulator into which you can paste, click here, copy what you see into your virtual Apple II, and then start your program from line 10 or higher.
Alternatively, if you can't paste into an emulator, or you want a BLOADable version, download Magic Gosub in your preferred format:
140K DOS 3.3 disk image
140K ProDOS disk image
800K ProDOS disk image
ShrinkIt archive
Everything else you might want to know:
- you can define a label in a REM statement in any of these three equivalent ways:
REM ^DoSomething
REM SUB^DoSomething
REM "DoSomething" [this is the Martin feature]
- the label definition ends with space, colon, or quote, and everything after one of those is ignored
(so you can add comments, e.g. REM SUB^MyLabel:my comment)
- the GOSUB target label needs to be in quotes, e.g.
GOSUB "DoSomething"
- you can prefix a label target with @ to perform a GOTO instead (e.g.
GOSUB @"GoSomewhere") [this is the Stavros
feature]
- label names are case sensitive, and most characters besides space, colon, and quote can be used
- the label search is performed from the top of the program
- The BLOADable version is 192 bytes long and can be run from any memory location (default is $300). To use it, start your program with these two lines. If you wish, you can specify a different BLOAD address and change the CALL accordingly.
0 IF (PEEK(248)<>248) THEN CALL 768
1 PRINT CHR$(4);"BLOAD MAGICGOSUB"
- If you want to use Slammer for other routines along with Magic Gosub, then copy and paste the lines from here instead of the above link, and start your program from line 17 or higher. (If you also want NuInput, then use this and start your code from line 30.) If copy and paste isn't an option, you can encode the BLOADable version of Magic Gosub with Slaminator, starting at line 11, and when it's done, insert the following line.
10 IF (PEEK(248)<>248) THEN CALL 250
You can view the assembly source code, or download it with its assembler (non Apple II friendly)
How Magic Gosub came to be (optional reading): One day, a few years ago, I wanted to program in Applesoft BASIC after a very long hiatus, during which I became accustomed to more structured programming languages. So I decided that I would create rules for writing Structured Applesoft. As part of that, I realized that I really wanted named GOSUB targets instead of having to use line numbers, so I began experimenting, and discovered that Applesoft uses line 0 as a target if it doesn't understand what comes after a GOSUB or GOTO.
So I figured that if line 0 called a machine language routine that could search for the name, it could be done. But then I'd need distribute a file to BLOAD along with any Applesoft program I wrote: yuck. I looked into methods to deliver a routine within a program itself, and was dissatisfied with the options, so I instead came up with Slammer, and while writing its utility, I decided that Applesoft's INPUT statement sucked and there should be a better one, so I developed NuInput, and then got distracted with other stuff and never actually got around to the labeled GOSUB routine which was the reason I made those other things in the first place. And then there I was at KansasFest 2012, and I decided to create Magic Gosub as my HackFest entry. And now I've polished it and am releasing it. Hope ya enjoy it.
Got a question about Magic Gosub?
Apple II Extravaganza Home Page