A basic BASIC question.

Basic and Machine Language

Moderator: Moderators

User avatar
JohnnyRockets
Vic 20 Enthusiast
Posts: 178
Joined: Wed Jun 13, 2012 5:42 pm
Location: Michigan, USA
Occupation: IT Manager

A basic BASIC question.

Post by JohnnyRockets »

Hi!

Considering the following program listing from the VIC-20 Commodore Programmer's Reference Manual:

Here is an inefficient program that doesn't use GOSUB:
100 PRINT "THIS PROGRAM PRINTS
Thanks!

JR


><>
Boray
Musical Smurf
Posts: 4064
Joined: Mon May 03, 2004 10:47 am

Post by Boray »

Every basic command word, such as GOSUB or PRINT takes ONE byte in memory, so maybe the second program really takes less memory. You could make it use even less memory by putting more than one command on the same line using a : ... other than that, using Gosub in that example would clearly be better if you decide to change the delay loop. Then you only need to change it in one place.
PRG Starter - a VICE helper / Vic Software (Boray Gammon, SD2IEC music player, Vic Disk Menu, Tribbles, Mega Omega, How Many 8K etc.)
User avatar
Witzo
Vic 20 Afficionado
Posts: 381
Joined: Thu Dec 01, 2011 9:14 am
Location: The Hague

Post by Witzo »

The GOSUBS that jump to the loop actually each take up less memory than the wait loop.

Also, basic is placed in memory as 'tokens', one byte (thanks Boray) codes that represent each basic keyword.

Enter both and type
?FRE(0)
after entering each to see how many bytes in memory are left.
Last edited by Witzo on Mon Nov 05, 2012 4:03 pm, edited 1 time in total.
User avatar
Pedro Lambrini
Vic 20 Scientist
Posts: 1132
Joined: Mon Dec 01, 2008 11:36 am

Post by Pedro Lambrini »

Why is it a gosub and not a goto? That's something I've never really understood...
"...That of the Eastern tribe being like a multitude of colours as if a rainbow had settled upon its brow..." Daniels 1:3
User avatar
Witzo
Vic 20 Afficionado
Posts: 381
Joined: Thu Dec 01, 2011 9:14 am
Location: The Hague

Post by Witzo »

GOSUB remembers where to go back after a RETURN.
GOTO can't be followed by a RETURN. Going back would require a new GOTO pointing to the line after the first GOTO.
In the second example, after jumping to the wait loop, you don't know where to return to to print the next line. Was it the first, second or third line that was just printed? Conveniently, GOSUB has noted in memory where the next line was.
User avatar
Pedro Lambrini
Vic 20 Scientist
Posts: 1132
Joined: Mon Dec 01, 2008 11:36 am

Post by Pedro Lambrini »

Aah, thank you for your clear and concise answer. I can't tell you how silly I feel now! Still, you learn something new every day and today I have you to thank for that. :)
"...That of the Eastern tribe being like a multitude of colours as if a rainbow had settled upon its brow..." Daniels 1:3
User avatar
Witzo
Vic 20 Afficionado
Posts: 381
Joined: Thu Dec 01, 2011 9:14 am
Location: The Hague

Post by Witzo »

Glad to be of service!
Usually it's me asking for advice from the local thaumaturgs.
Boray
Musical Smurf
Posts: 4064
Joined: Mon May 03, 2004 10:47 am

Post by Boray »

Here is my text on basic optimization:
http://user.tninet.se/~pug510w/datormuseum/optim.html

Maybe of more help for non-beginners though...
PRG Starter - a VICE helper / Vic Software (Boray Gammon, SD2IEC music player, Vic Disk Menu, Tribbles, Mega Omega, How Many 8K etc.)
User avatar
Witzo
Vic 20 Afficionado
Posts: 381
Joined: Thu Dec 01, 2011 9:14 am
Location: The Hague

Post by Witzo »

Boray wrote:Here is my text on basic optimization:
http://user.tninet.se/~pug510w/datormuseum/optim.html

Maybe of more help for non-beginners though...
Awesome. I never knew any of those things except for squeezing as much as possible on a line.
What is the maximum line length? Two screen lines?
Boray
Musical Smurf
Posts: 4064
Joined: Mon May 03, 2004 10:47 am

Post by Boray »

It's four lines on a vic-20 and two lines on a c64, but you can make them even longer if you use command word abbreviations when writing the line. But then it will be hard to edit them afterwards. I think the very maximum is 255 or 256 characters.
PRG Starter - a VICE helper / Vic Software (Boray Gammon, SD2IEC music player, Vic Disk Menu, Tribbles, Mega Omega, How Many 8K etc.)
User avatar
Witzo
Vic 20 Afficionado
Posts: 381
Joined: Thu Dec 01, 2011 9:14 am
Location: The Hague

Post by Witzo »

Boray wrote:It's four lines on a vic-20 and two lines on a c64, but you can make them even longer if you use command word abbreviations when writing the line. But then it will be hard to edit them afterwards. I think the very maximum is 255 or 256 characters.
Yes, it comes back to me now.
The two-character one-normal-one-shifted abbreviations that expanded beyond the fourth line on screen.
wimoos
Vic 20 Afficionado
Posts: 350
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Post by wimoos »

The lines in the program in memory (with the tokenized keywords) have a maximum length of 255 bytes. Typing a line will never reach that length. POKEing around will, but that's extremely cumbersome.

Using shifted characters will indeed help. Funny thing is that when you shift the last character of a keyword, it will not be recognized ! That is because in the reference table of keywords in ROM, the last characters are already "shifted" as to signify the end of the keyword. Comparing the typed-in keyword with the keyword in memory stops when a difference of $80 is found. When both last characters are shifted, the difference is 0, which explains this odd behaviour.

Regards,

Wim
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
JohnnyRockets
Vic 20 Enthusiast
Posts: 178
Joined: Wed Jun 13, 2012 5:42 pm
Location: Michigan, USA
Occupation: IT Manager

Post by JohnnyRockets »

Witzo wrote:The GOSUBS that jump to the loop actually each take up less memory than the wait loop.

Also, basic is placed in memory as 'tokens', one byte (thanks Boray) codes that represent each basic keyword.

Enter both and type
?FRE(0)
after entering each to see how many bytes in memory are left.
Thanks, and very interesting! :D

The difference was:

3376 free without the gosubs

3390 free with the gosubs

= 14 byte difference


What is taking up those 14 bytes? Or is that mystery going to be found by examining the machine code?
Thanks!

JR


><>
User avatar
Witzo
Vic 20 Afficionado
Posts: 381
Joined: Thu Dec 01, 2011 9:14 am
Location: The Hague

Post by Witzo »

My guess it's the copies of the wait loop, FOR L = 1 TO 500 : NEXT; 3 tokens FOR-TO-NEXT, several numbers, several times over.
The gosubs are each only one token for GOSUB and the target line number.
User avatar
JohnnyRockets
Vic 20 Enthusiast
Posts: 178
Joined: Wed Jun 13, 2012 5:42 pm
Location: Michigan, USA
Occupation: IT Manager

Post by JohnnyRockets »

Witzo wrote:My guess it's the copies of the wait loop, FOR L = 1 TO 500 : NEXT; 3 tokens FOR-TO-NEXT, several numbers, several times over.
The gosubs are each only one token for GOSUB and the target line number.
Makes good sense! I appreciate it!
Thanks!

JR


><>
Post Reply