Interesting BASIC quirk

Basic and Machine Language

Moderator: Moderators

Post Reply
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Interesting BASIC quirk

Post 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 :)
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Interesting BASIC quirk

Post by Bobbi »

Something else I didn't realise is SPC() and TAB() and handled only for PRINT, so A$ = SPC(10) is an error.
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Interesting BASIC quirk

Post 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.
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Interesting BASIC quirk

Post by Bobbi »

Oh my bad, forgot:

Code: Select all

20 END
My excuse is not enough coffee yet.
User avatar
Stormcrow
Vic 20 Enthusiast
Posts: 177
Joined: Mon Dec 24, 2012 9:46 pm
Website: http://trimboli.name
Location: Ronkonkoma, NY

Re: Interesting BASIC quirk

Post 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).
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Interesting BASIC quirk

Post 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.
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Interesting BASIC quirk

Post 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.
User avatar
Stormcrow
Vic 20 Enthusiast
Posts: 177
Joined: Mon Dec 24, 2012 9:46 pm
Website: http://trimboli.name
Location: Ronkonkoma, NY

Re: Interesting BASIC quirk

Post 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"
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Interesting BASIC quirk

Post 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:
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Interesting BASIC quirk

Post 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 1690 times
User avatar
srowe
Vic 20 Scientist
Posts: 1325
Joined: Mon Jun 16, 2014 3:19 pm

Re: Interesting BASIC quirk

Post by srowe »

LOL, must be revenge for the WAIT 6502 Easter egg

http://www.pagetable.com/?p=43
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Interesting BASIC quirk

Post by Bobbi »

Apparently so!
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Interesting BASIC quirk

Post 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.
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Interesting BASIC quirk

Post by Mike »

While we're at it - enter:

Code: Select all

PRINT 5+"A"+-5
... and see the BASIC crash! :mrgreen:
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Interesting BASIC quirk

Post by Bobbi »

That is a good one!
Post Reply