Cool AppleSoft BASIC trick I never thought of before

From: Doug Coward <mranalog_at_home.com>
Date: Wed May 17 12:01:02 2000

Sam said:
> On the Apple ][, in AppleSoft BASIC, you can set what the start address
> of your BASIC program will be in memory by poking it into location 103-104
> ($68-$69 hex).
> The default is 2049 ($801): 103=1 and 104=8 ($68=1, $69=8).
> QUESTION: Does anyone know why the default is $801 instead of $800? I
> always wondered why that was. Anyway...

 _MANY_ BASICs have the same format for storing a
program line. The first two bytes is the address
"link" to the next line of the program. The second
two bytes is the "line number" for this line of
the program. The line number is followed by the text
of the program line, with all of the BASIC keywords
tokenized, and terminated with a zero. The next
address location after the zero is the address
pointed to by the link of the previous line and
it's also the beginning of the next line of the
program. At the end of a BASIC program, the zero
at the end of the last line is followed by two
zeros meaning that the next "link" is zero.
 When BASIC is initialized, (and the start of
BASIC is normally $801), the OS usually stores
zeros at $800,$801,and $802, and set the beginning
of BASIC program space at $801, indicating that
$800 is the end of the previous line and $801,$802
is the end of the program. So the BASIC program
space DOES start at $800, but the beginning of the
BASIC program is $801. _I_believe_ the need for a
zero indicating the end of the previous line at
the beginning of BASIC program space is probably
a hold over from some ANCIENT incarnation of
BASIC and is not really necessary, but I have
not tried this with different kinds of BASIC.
There should be no problem with setting the
beginning of the BASIC program area to say....
$4057 before loading your program. As the program
is loaded, the links are changed to reflect
the new program location.
 
> I run either of the programs and I get an endlessly alternating display of
> "PROGRAM 1!" and "PROGRAM 2!". In other words, it works! Cool.

 BASIC also keeps a pointer to the current
line being executed. Instead of "running"
each program, try having the first program
(at the end) set the current line pointer
to $4001 and vicey versy.

> It seems to me this could be mutated into a sort of practical method of
> multi-tasking BASIC programs.

 There are quite a few zero page locations
you need to save and restore, but multi-tasking
BASIC is very possible.
 You could also write a BASIC program that built
it's own custom subroutines in a different memory
area on the fly, execute them, and return to
itself.
 --Doug
====================================================
Doug Coward dcoward_at_pressstart.com (work)
Sr. Software Eng. mranalog_at_home.com (home)
Press Start Inc. http://www.pressstart.com
Sunnyvale,CA

Curator
Analog Computer Museum and History Center
http://www.best.com/~dcoward/analog
====================================================
Received on Wed May 17 2000 - 12:01:02 BST

This archive was generated by hypermail 2.3.0 : Fri Oct 10 2014 - 23:33:09 BST