A basic BASIC question.
Moderator: Moderators
- JohnnyRockets
- Vic 20 Enthusiast
- Posts: 178
- Joined: Wed Jun 13, 2012 5:42 pm
- Location: Michigan, USA
- Occupation: IT Manager
A basic BASIC question.
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
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
><>
JR
><>
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.)
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.
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.
- Pedro Lambrini
- Vic 20 Scientist
- Posts: 1132
- Joined: Mon Dec 01, 2008 11:36 am
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.
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.
- Pedro Lambrini
- Vic 20 Scientist
- Posts: 1132
- Joined: Mon Dec 01, 2008 11:36 am
Here is my text on basic optimization:
http://user.tninet.se/~pug510w/datormuseum/optim.html
Maybe of more help for non-beginners though...
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.)
Awesome. I never knew any of those things except for squeezing as much as possible on a line.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...
What is the maximum line length? Two screen lines?
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.)
Yes, it comes back to me now.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.
The two-character one-normal-one-shifted abbreviations that expanded beyond the fourth line on screen.
-
- Vic 20 Afficionado
- Posts: 350
- Joined: Tue Apr 14, 2009 8:15 am
- Website: http://wimbasic.webs.com
- Location: Netherlands
- Occupation: farmer
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
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
- JohnnyRockets
- Vic 20 Enthusiast
- Posts: 178
- Joined: Wed Jun 13, 2012 5:42 pm
- Location: Michigan, USA
- Occupation: IT Manager
Thanks, and very interesting!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.
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
><>
JR
><>
- JohnnyRockets
- Vic 20 Enthusiast
- Posts: 178
- Joined: Wed Jun 13, 2012 5:42 pm
- Location: Michigan, USA
- Occupation: IT Manager