Date : Mon, 27 Jun 2005 16:53:11 +0100
From : Richard_Talbot-Watkins@...
Subject: Re: BASIC IV: Coded Line Number
Angus Duggan wrote:
> Because that would make scanning for end of line and end of program
harder;
> the MSB/LSB might conflict with other tokens. If I remember correctly,
it's
> designed so that none of the following three bytes are a legal end of
> program sequence.
Yep... even though Basic lines contain a 'line length' value, which allows
the interpreter to skip to the next line very quickly, without scanning,
sometimes it is necessary. Take the following line:
10 IF A%=0 GOTO 139 ELSE PRINT"Banana"
If A% is non-zero, the interpreter has to scan forward, looking for either:
* an ELSE clause
* the end of the line
Clearly if line number 139 was coded as a hi/lo pair, a bogus ELSE token
(139) would be found and it would all go wrong. Instead, by making the
line number look a bit like a variable name using the three-byte coding
method, the problem is eliminated.
Of course, during scanning forward, it could make a special case of the 8D
token and automatically skip the next two bytes. But the interpreter
(Basic II at least) doesn't make special cases of *anything* when scanning
the line as you can see by this example:
10 IF A%=0 PRINT"<139>Works" ELSE PRINT"Broken"
(<139> is the ASCII character 139 inserted directly into the string)
When run with A%=0, the message '<139>Works' is printed as expected.
When run with A%=1, we get the less useful 'Mistake at line 10'.
The interpreter is failing to notice that the ELSE token (139) is within
quotes and should therefore be scanned past. This is a BUG! (bit unfair
admittedly)
A truly evil way to obfuscate a Basic program would be to actually put
valid interpretable code inside that string after the <139>, ending it with
:<244> (the token for REM, to tell it to skip the rest of the line, or that
closing quote would be a syntax error). I've just tried this in Basic II
and it works perfectly, much to my horror!
Try it (type it exactly as shown):
>MODE 7
>NEW
>10A$="HELLO"
>20IFA%=0 PRINT"**A$:*" ELSE PRINT"GOODBYE"
>?(PAGE+&1A)=&8B
>?(PAGE+&1B)=&F1
>?(PAGE+&1F)=&F4
>
>LIST
10A$="HELLO"
20IFA%=0 PRINT" qA$:t" ELSE PRINT"GOO
DBYE"
>
>A%=0:RUN
qA$:t
>A%=1:RUN
HELLO
>
I have a feeling this was corrected in Basic IV... anyone care to try it?
**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
postmaster@...
This footnote also confirms that this email message has been checked
for all known viruses.
**********************************************************************
Sony Computer Entertainment Europe