Page 1 of 1

Interesting BASIC quirk

Posted: Fri Nov 04, 2016 9:51 am
by Bobbi
I was reading the ROM disassembly and trying to understand how BASIC uses the stack for implementing GOSUB and FOR / NEXT.

Here is an interesting quirk caused by the rather naive way that RETURN scans the stack:

Code: Select all

100 GOSUB 1000
1000 FORI=1TO10
1100 RETURN
Gives: ?RETURN WITHOUT GOSUB ERROR IN 1100

I am guessing this is well-known, but I was happy to see that the actual behaviour matched my reading of the code :)

Re: Interesting BASIC quirk

Posted: Fri Nov 04, 2016 10:05 am
by Bobbi
Something else I didn't realise is SPC() and TAB() and handled only for PRINT, so A$ = SPC(10) is an error.

Re: Interesting BASIC quirk

Posted: Fri Nov 04, 2016 11:23 am
by Mike
Naive? :lol:

That's one of the best things *correctly* handled by the stack: a pending FOR loop in a subroutine is discarded upon return from the subroutine!

What happens here? You enter the sub, open the FOR loop, return from the sub - purging the FOR loop! - and then *errorneously* re-enter the sub routine without GOSUB. No wonders you get a ?RETURN WITHOUT GOSUB error.

Actually quite sensible behaviour. When within a subroutine, you don't need to terminate all pending FOR loops to be "allowed" a RETURN. Comes handy for early exits from search processes.

You find a particularly nice example in my TRON game, where a loop in line 41 searches for an escape path for the enemy light cycle. If it succeeds, it does an early exit - otherwise it returns with the current direction unaltered, letting the enemy crash:

Code: Select all

17 IFPEEK(G+D(H))<>219ORRND(1)<.1THENI=C:GOSUB41
[...]
41 FORT=1TO4:I=(I+1)AND3:IFPEEK(G+D(I))=219THENH=I:RETURN
42 NEXT:RETURN
Here's the game. I put it into a collection of 21 games for the unexpanded VIC-20.

Re: Interesting BASIC quirk

Posted: Fri Nov 04, 2016 11:49 am
by Bobbi
Oh my bad, forgot:

Code: Select all

20 END
My excuse is not enough coffee yet.

Re: Interesting BASIC quirk

Posted: Fri Nov 04, 2016 2:56 pm
by Stormcrow
Bobbi wrote:Something else I didn't realise is SPC() and TAB() and handled only for PRINT, so A$ = SPC(10) is an error.
SPC() and TAB() only position the cursor; they don't produce characters (hence no $ on the function).

Re: Interesting BASIC quirk

Posted: Fri Nov 04, 2016 3:09 pm
by Bobbi
I guess for TAB that makes sense (since it uses the screen tab positions). SPC() could have been SPC$() quite easily though I guess.

Re: Interesting BASIC quirk

Posted: Fri Nov 04, 2016 3:18 pm
by Mike
Some other BASIC dialects support a SPACE$(n) or a STRING$(<string>,n) function, the latter which produces a string of n concatenated instances of <string>.

For a simple replacement of SPACE$(), a combination of a pre-initialised string SP$="<as many spaces as you think you'll need>" and LEFT$(SP$,n) is probably sufficient.
Bobbi wrote:SPC() could have been SPC$() quite easily though I guess.
SPC( and TAB( are format specifiers intended only for the use within PRINT, and for screen output. TAB( isn't even guaranteed to work on anything other than the screen, and SPC( is just implemented by outputting n spaces. Setting up a string for use within the program is slightly more complicated.

Re: Interesting BASIC quirk

Posted: Mon Nov 07, 2016 1:04 pm
by Stormcrow
Bobbi wrote:SPC() could have been SPC$() quite easily though I guess.
SPC$() would produce actual space characters if it existed. SPC() does not.

PRINT "123←←←" SPC(3) "456"

is different than

PRINT "123←←←" CHR$(32) CHR$(32) CHR$(32) "456"

Re: Interesting BASIC quirk

Posted: Mon Nov 07, 2016 1:14 pm
by Mike
You're right. :shock:

Try out this one for a good laugh what *actually* is PRINTed:

Code: Select all

PRINTCHR$(34)SPC(3)CHR$(34)
The closing angle brackets are the control characters for CRSR RIGHT. :lol:

Re: Interesting BASIC quirk

Posted: Mon Nov 07, 2016 1:50 pm
by Bobbi
I discovered this easter egg earlier today (courtesy of TPUG newletter.) I assume it is well known.
snapshot.png
snapshot.png (3.43 KiB) Viewed 1806 times

Re: Interesting BASIC quirk

Posted: Mon Nov 07, 2016 2:06 pm
by srowe
LOL, must be revenge for the WAIT 6502 Easter egg

http://www.pagetable.com/?p=43

Re: Interesting BASIC quirk

Posted: Mon Nov 07, 2016 2:08 pm
by Bobbi
Apparently so!

Re: Interesting BASIC quirk

Posted: Mon Nov 07, 2016 2:40 pm
by Mike
Nothing spectacular here. The seeds for RND() contain enough information, that one can print ANY 5 letter string in that way.

One only needs to put out a small search program - and preferably run it on a PC in VICE with "no speed limit" - to reproduce other strings.

Re: Interesting BASIC quirk

Posted: Mon Nov 07, 2016 2:43 pm
by Mike
While we're at it - enter:

Code: Select all

PRINT 5+"A"+-5
... and see the BASIC crash! :mrgreen:

Re: Interesting BASIC quirk

Posted: Mon Nov 07, 2016 3:06 pm
by Bobbi
That is a good one!