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 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