Jim Butterfield's "Machine Language for Commodore Machines" book states at page 92 in the chapter on BASIC Memory Layout that (emphasis mine):
- End-of-BASIC is signaled by three zero bytes somewhere after the
SOB
(Start-of-BASIC). If you commandNEW
in BASIC, you'll find the three bytes right at the start of BASIC [...].
I tried with the VICE C64 emulator, and found that there are only two zero bytes (not three) marking the end of BASIC after the SOB.
For example, that is verifiable from the BASIC prompt (SOB = 2049):
NEW
PRINT PEEK(2049);PEEK(2050);PEEK(2051)
Note that inspecting $2B
/$2C
shows the byte values $01
$08
, so SOB is $0801
, or 2049 decimal (which is also stated in the aforementioned book).
This behavior is confirmed also on a real C64C.
So, is this an error in Jim Butterfield's book?
Jim Butterfield's "Machine Language for Commodore Machines" book states at page 92 in the chapter on BASIC Memory Layout that (emphasis mine):
- End-of-BASIC is signaled by three zero bytes somewhere after the
SOB
(Start-of-BASIC). If you commandNEW
in BASIC, you'll find the three bytes right at the start of BASIC [...].
I tried with the VICE C64 emulator, and found that there are only two zero bytes (not three) marking the end of BASIC after the SOB.
For example, that is verifiable from the BASIC prompt (SOB = 2049):
NEW
PRINT PEEK(2049);PEEK(2050);PEEK(2051)
Note that inspecting $2B
/$2C
shows the byte values $01
$08
, so SOB is $0801
, or 2049 decimal (which is also stated in the aforementioned book).
This behavior is confirmed also on a real C64C.
So, is this an error in Jim Butterfield's book?
Share Improve this question edited Mar 8 at 16:40 Mr.C64 asked Mar 5 at 19:52 Mr.C64Mr.C64 43.1k15 gold badges95 silver badges168 bronze badges 3- This question seems more appropriate for Retrocomputing. – Barmar Commented Mar 7 at 22:07
- @Barmar: I have no problem moving the question to Retrocomputing; but why do we have a [c64] tag here on StackOverflow? Thanks. – Mr.C64 Commented Mar 7 at 23:45
- That's for questions about writing programs to run on a C64 (or emulator). Not questions about the design of the system itself. – Barmar Commented Mar 7 at 23:56
2 Answers
Reset to default 2The 1983 book "Machine Language for Beginners" by Richard Mansfield shows at page 106 a figure 8-1. A BASIC Program's Structure, similar to the following:
/-------------->---------------\ /-------------->---------------\
| | | |
| V | V
0, wPOINTER, wLINENUM, bytesLINE1, 0, wPOINTER, wLINENUM, bytesLINE2, 0, 0, 0
^ ^ ^ ^
| | | End of Prog
Start of BASIC End of Line End of Line
The Mansfield book pertains to Atari, VIC, Apple, Commodore 64, and PET/CBM computers. (Only the Atari uses a structure different than the one in this figure.)
Do notice that the main difference to Jim Butterfield's figure is that Jim places the Start of BASIC not on the leading zero but behind the leading zero.
When running NEW
, all the BASIC lines have to disappear:
====================================================================
0, wPOINTER, wLINENUM, bytesLINE1, 0, wPOINTER, wLINENUM, bytesLINE2, 0, 0, 0
^ ^ ^ ^
| | | End of Prog
Start of BASIC End of Line End of Line
leaving:
0, 0, 0
^ ^
| End of Prog
Start of BASIC
In short, it is 3 zero bytes, but for C64 the 3rd zero is before to where the 'Start-of-BASIC' at 002B-002C points. What is misleading in Jim Butterfield's figure is that Jim combined the one terminating zero for the last BASIC line together with the two zero bytes in the end. See https://www.lemon64/forum/viewtopic.php?t=61760.
A point of confusion common to Microsoft-derived BASIC interpreters of that era is that each "real" source line consists of a zero byte, followed by a line link and line number, followed by the source text, and the end of program appears as a zero byte followed by a line link to address zero, but the zero-page locations identifying the "start" of a program actually identify an address one byte higher than the zero byte that starts it.
I would guess that this quirk stems from a discovery that having program execution start at a zero byte immediately preceding a BASIC line allows the code for RUN and GOTO to be simplified by having execution start at the byte preceding the target line. Adding a zero byte between the address indicated by the start-of-BASIC-program pointer and the first line link would have broken compatibility with code that used the start-of-program address to find the first line link. Putting a zero before the link link, but having the start-of-program address point to the byte after it, avoided any compatibility problems that could have arisen.