11

I have a vague recollection of some early software that extended the Apple II BASIC language, though I'm not sure if this was Integer or Applesoft BASIC.

It provided more features for the language by using commands such as &line x1, y1, x2, y2 to augment the rather primitive HLINE and VLINE drawing commands, or &circle x, y, radius for circles. I don't know if they were actual commands (it was forty-plus years ago) but that was the basic idea.

How this was done? Did it require that BASIC be loaded into RAM and patched or was it some sort of built-in hook already in the software? What was the process the BASIC interpreter followed to allow a command to use the new functionality?

1
  • 1
    My "fdraw" project provides ampersand extensions that draw lines and circles from Applesoft BASIC. Full source code here: github.com/fadden/fdraw (AMPERFDRAW.S implements the BASIC extensions).
    – fadden
    Mar 2, 2022 at 15:32

3 Answers 3

14

After a bit of research, it turns out it was Applesoft.

The interpreter had a feature where, if it encountered an ampersand during execution of the program, it would unconditionally jump to a subroutine at location $3F5.

Extension software could use BLOAD or BRUNto get some code to a specific location and modify the three bytes at that location. This would then cause the code to execute when & was seen.

When that code eventually returned, Applesoft would continue execution after the ampersand.

So, if all you wanted was one simple thing to happen when an ampersand was encountered, that would be enough.


In order to allow for more complicated things like &line 1, 1, 20, 50, the code had to access internal memory to both figure out what the command was (so it could read parameters), and to ensure Applesoft skipped those parameters when we returned. The TXTPTR Applesoft variable was used for this.

You could call specific functions from your code to parse the line after the ampersand, using TXTPTR, and adjust it as part of that process, such as with:

; Handles something like "& 12345 * 2, 41"

JSR   $DD67      ;FRMNUM - eval numeric expression -> FAC
JSR   $E752      ;GETADR - place FAC into memory (LINNUM)
JSR   $DEBE      ;CHKCOM - check for comma
JSR   $E6F8      ;GETBYT - eval byte -> X reg

; Do something in here with registers and memory populated above.

RTS

Granted that's fairly simple but you had ways to ensure that the string "line" was the first thing after the ampersand as well (or detect different commands), giving you a variety of options. There was a GETCHR which returned the next character (or token if it was a keyword).

3
  • Note that there were quite a few extensions using the ampersand. IIRC another one I used frequently was for renumbering BASIC lines.
    – dirkt
    Mar 2, 2022 at 12:17
  • Regarding BLOAD/BRUN, it wasn't uncommon to use BRUN with the first instructions putting the address into $3F5 and reusing those 12 bytes as storage area during later execution.
    – Raffzahn
    Nov 14 at 19:21
  • I had merged several & routines copied from magazines to extend my Applesoft at that time. I had merged a line editor that was invoked with &linennumber but also a repeat/until, named procedures, etc. Used it a lot and found recently my 3 floppies full of basic programs using these extensions. It's just annoying that I still not have extracted the data of my assembly source floppy. Nov 15 at 9:27
9

While paxdiablo answered the question confirming that it was AppleSoft, I can point out three things related to it as an extension:

  1. When you load your extension, it should attempt to relocate itself as high as it can in the available memory to leave as much free RAM as possible behind. As such, it was common for ampersand extensions to be written with relative addressing if not using jump tables or self-modifying code.
  2. It was good practice to make sure that your extension chained control to whatever address was in place previously. In other words, make it possible that more than one ampersand extension could exist; if your extension didn't recognize (say) & FOO, then perhaps the next one in the chain could.
  3. There was another, lesser used AppleSoft command USR(x) which would call a machine language routine with the value of x in the accumulator (IIRC). Whatever the routine had in the accumulator when it finished would be passed as the lvalue (e.g. Y = USR(123)).

A few extensions off the top of my head: AmperWorks, ModemWorks, Beagle Basic, Beagle Graphics, and more recently I believe there was a text windowing library created by someone that used ampersand extensions.

5
  • 1
    For #1: True when it's about some larger extension. The vast majority were rather small routines placed in the usual ML scrap area at $300.
    – Raffzahn
    Mar 2, 2022 at 13:34
  • 1
    Out of curiosity, how often would code be relocatable to arbitrary addresses, and how often would it be relocatable on 256-byte boundaries? On the 6502, designing for the latter is pretty easy even with fairly primitive tools: assemble a program for two addresses, 256 bytes apart, compare, ensure that everything either matches or differs by one, and build a list of those differences.
    – supercat
    Mar 2, 2022 at 16:17
  • 1
    I had combined several ampersand extension programs from different magazines in one package. A line editor, several basic extensions (while/wend repeat/until, some graphic commands, etc). I still have the binary (probably also the source in some old floppy) but have completely forgotten how to use it. Mar 3, 2022 at 9:28
  • 1
    bjb, I appreciate the use of "themself" in light of the fact I haven't made my gender clear (male) but it seemed a little "strained". "Himself" would have been better (had you known the gender) but, when I edited, that felt weird as well. So I just took gender out of the equation. Hope you don't mind. Good answer BTW. I have a sudden hankering to go find all that Beagle Bros software I loved back in the day :-)
    – paxdiablo
    Mar 3, 2022 at 22:41
  • we also use the technique when submitting to the AppleIIBot, by POKEing directly into memory to set the handler and place our code. It saves some bytes. See here: pferrie.epizy.com/misc/appleii/a2bot.htm Mar 7, 2022 at 0:52
5

A little late to the party but I remember doing this back in the early 80's, it was definitely the & "extension" used in Applesoft. Like Patrick mentioned I also remember combining several commands into a large Applesoft extension which I relocated into the upper 16K memory bank and accessed via paging the memory bank in and out as required. There were so many useful commands such as WHILE..WEND, REPEAT..UNTIL, BOX, CIRCLE, FILL. I think I had a go at sprites too, you could fit a lot into 16K. Wish I still had that code, I am rediscovering my Apple past via emulators at present and would love to have a go at recreating some of this stuff.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .