The IF statement....
Moderator: Moderators
The IF statement....
Hello everybody, I'm new to this so if I make any mistakes tell me.
I'm somewhat new to CBM-Basic and the Vic-20, I own a C64 -though I prefer programing on my Vic-. I have started programming and I've figured out how memory can be manipulated to change characters, display, sound, joysticks, etc. When I made my first program it had a-lot of IF statements; on the Vic-20 ,that slows it down tremendously (Pac-Man usually never eats anything before I give up), I have to use if's: how does the Vic know the object ran off the screen, or when they collide. I'm wondering if their is a way to use something else? I have self-taught myself everything and the Programmer's reference guide is my best buddy. I noticed cartridge games are pretty fast is it because of machine language? Anyone have suggestions or am I doomed to never finish the level.
Anyway, I think it's nice to see forums like this one around the web, even though the Vic-20 had a short-but-sweet run. If you've gotten this far in reading this I'm amazed .
(Sorry for my English)
I'm somewhat new to CBM-Basic and the Vic-20, I own a C64 -though I prefer programing on my Vic-. I have started programming and I've figured out how memory can be manipulated to change characters, display, sound, joysticks, etc. When I made my first program it had a-lot of IF statements; on the Vic-20 ,that slows it down tremendously (Pac-Man usually never eats anything before I give up), I have to use if's: how does the Vic know the object ran off the screen, or when they collide. I'm wondering if their is a way to use something else? I have self-taught myself everything and the Programmer's reference guide is my best buddy. I noticed cartridge games are pretty fast is it because of machine language? Anyone have suggestions or am I doomed to never finish the level.
Anyway, I think it's nice to see forums like this one around the web, even though the Vic-20 had a short-but-sweet run. If you've gotten this far in reading this I'm amazed .
(Sorry for my English)
-Floopy
-
- Vic 20 Afficionado
- Posts: 354
- Joined: Thu Oct 13, 2016 11:35 am
- Location: Toronto
- Occupation: Programmer
Re: The IF statement....
As you are discovering, BASIC is very slow for arcade style games. Most of the better ones are written in machine code.
Re: The IF statement....
I think I'll have to dive into machine language some time, I'm a little confused on how it works, but I'll figure something out someday
-Floopy
-
- Vic 20 Afficionado
- Posts: 354
- Joined: Thu Oct 13, 2016 11:35 am
- Location: Toronto
- Occupation: Programmer
Re: The IF statement....
You can find lots of help here for VIC-20 specific stuff. 6502.org is pretty good for general machine code wisdom.
Back in the day I found the Machine Code Monitor (VICMON) cartridge very educational, but these days it is easier to code assembly on a PC using a cross-assembler like xa65 and to debug in the VICE monitor window.
Back in the day I found the Machine Code Monitor (VICMON) cartridge very educational, but these days it is easier to code assembly on a PC using a cross-assembler like xa65 and to debug in the VICE monitor window.
Re: The IF statement....
Okay, thank you for your advice . I might post the program I made recently just to see general reaction.
-Floopy
-
- Vic 20 Afficionado
- Posts: 354
- Joined: Thu Oct 13, 2016 11:35 am
- Location: Toronto
- Occupation: Programmer
Re: The IF statement....
Sure why not? It's always good to have new stuff to play with on the VIC
Re: The IF statement....
I took me 2 years of extensive reading and coding attempts before I could finally understand ml well enough to write a functional program. It has been well worthh the effort.Floopy wrote:I think I'll have to dive into machine language some time, I'm a little confused on how it works, but I'll figure something out someday
R'zo
I do not believe in obsolete...
I do not believe in obsolete...
-
- Vic 20 Afficionado
- Posts: 352
- Joined: Tue Apr 14, 2009 8:15 am
- Website: http://wimbasic.webs.com
- Location: Netherlands
- Occupation: farmer
Re: The IF statement....
It is not so much that the IF statement slows things down, it's more the formula after the IF statement. There's a few things that you can do to speed that up.
Machinelanguage is one of them. In arcade games this helps dramatically. Arcade games are usuallly around integers and byte, whereas in BASIC floating point operations are key.
Should you still prefer BASIC (because of the ease of programming), then a clever order of variable initialization can help. In order to locate a variable, the first one that was defined, is found the quickest.
As far as IFs go:
Testing against zero is not necessary. works the same as The last one is obviously faster.
When you are writing: you'd better rewrite this as with <cond1> being the easiest to evaluate (least number of variables and least complex arithmetic).
Finally, sometimes you can replace IFs by or
Also, the way GOTO is implemented can be of significant impact, especially when it is a long program. in a lengthy program works slower than For an idle loop this is not essential, but it can be in other situations. Better replace these by FOR...NEXT, or split the program in modules.
Removing spaces in the code and combining multiple lines to one, helps making GOTO faster in this respect.
Thats about the best you can get, without making your program illegible.
Regards,
Wim.
Machinelanguage is one of them. In arcade games this helps dramatically. Arcade games are usuallly around integers and byte, whereas in BASIC floating point operations are key.
Should you still prefer BASIC (because of the ease of programming), then a clever order of variable initialization can help. In order to locate a variable, the first one that was defined, is found the quickest.
As far as IFs go:
Testing against zero is not necessary.
Code: Select all
IF B<>0 THEN
Code: Select all
IF B THEN
When you are writing:
Code: Select all
IF <cond1> AND <cond2> THEN...
Code: Select all
IF <cond1> THEN IF <cond2> THEN...
Finally, sometimes you can replace IFs by
Code: Select all
ON <index> GOTO line,line,...
Code: Select all
ON <index> GOSUB line,line,...
Code: Select all
4530 GET A$:IF A$="" THEN 4530
Code: Select all
10 GET A$:IF A$="" THEN 10
Removing spaces in the code and combining multiple lines to one, helps making GOTO faster in this respect.
Thats about the best you can get, without making your program illegible.
Regards,
Wim.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
- Mike
- Herr VC
- Posts: 4941
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
Re: The IF statement....
Floopy wrote:When I made my first program it had a-lot of IF statements; on the Vic-20 ,that slows it down tremendously (Pac-Man usually never eats anything before I give up), I have to use if's: how does the Vic know the object ran off the screen, or when they collide.
The use of BASIC is not necessarily an impediment when the program is well structured.Bobbi wrote:As you are discovering, BASIC is very slow for arcade style games. Most of the better ones are written in machine code.
Quite often beginners use working, but over-complicated constructions, because not all of the language is known. Also a lot of the BASIC commands and functions can be used in ways that only reveal with experience.
That being said, it's quite instructive to inspect a program similar to that one is about to write, and look what is done there, why, and how. And then improve upon it.
Some years ago I released a collection of 21 games for the unexpanded VIC-20, mostly type-ins, and also most of them in BASIC. There, you should spare a look at FRESSMANN. It comes in two parts on this disk - the first part redefines the characters, the second part is the main program. And it's *fast*, even though it's 100% written in BASIC.
-
- Vic 20 Afficionado
- Posts: 354
- Joined: Thu Oct 13, 2016 11:35 am
- Location: Toronto
- Occupation: Programmer
Re: The IF statement....
You are quite correct Mike!
I guess I could have said that there is 'nice' BASIC code and 'fast' BASIC code. Some of the tricks one has to do to make BASIC code run faster tend to result in less clarity when reading the code. When I go back and read 1980s computer magazines I am amazed at how convoluted some of the type-in programs are! That said, it is hard to write 'nice' easy to read code in the rather bare-bones CBM BASIC V2.
I enjoy BASIC for prototyping and for playing around interactively, but I tend to go for assembly (or cross-compiled C these days) when it comes to the actual implementation.
I guess I could have said that there is 'nice' BASIC code and 'fast' BASIC code. Some of the tricks one has to do to make BASIC code run faster tend to result in less clarity when reading the code. When I go back and read 1980s computer magazines I am amazed at how convoluted some of the type-in programs are! That said, it is hard to write 'nice' easy to read code in the rather bare-bones CBM BASIC V2.
I enjoy BASIC for prototyping and for playing around interactively, but I tend to go for assembly (or cross-compiled C these days) when it comes to the actual implementation.
Re: The IF statement....
If you want to start from the fundamentals, you may want to take a look at this excellent course...
www.nand2tetris.org
www.nand2tetris.org
Re: The IF statement....
Wow, never thought I would get so many answers.
Thank you everybody, my question has pretty much been answered. I will try different implantations and see what works best. I have a few articles in Compute! and Commodore magazines that explain how to start using ML. As for posting my program (it's not super exciting but it's my first real functional program) I need to find a way to port it to a .TAP file I'll figure something out.
Thank you all!
Thank you everybody, my question has pretty much been answered. I will try different implantations and see what works best. I have a few articles in Compute! and Commodore magazines that explain how to start using ML. As for posting my program (it's not super exciting but it's my first real functional program) I need to find a way to port it to a .TAP file I'll figure something out.
Thank you all!
-Floopy
- Kweepa
- Vic 20 Scientist
- Posts: 1302
- Joined: Fri Jan 04, 2008 5:11 pm
- Location: Austin, Texas
- Occupation: Game maker
Re: The IF statement....
If you are planning to learn 6502, I recommend this:
https://skilldrick.github.io/easy6502/
It's a crash course with an online assembler and simulator.
https://skilldrick.github.io/easy6502/
It's a crash course with an online assembler and simulator.
Re: The IF statement....
with a bit of planning, almost all IF statements can be replaced with (multiple) boolean statements.
eg.
if x = 1 then p=p+1
if x = 2 then p=p-1
can be achieved with
p=p+(x=2)-(x=1)
much faster....
eg.
if x = 1 then p=p+1
if x = 2 then p=p-1
can be achieved with
p=p+(x=2)-(x=1)
much faster....
Re: The IF statement....
And very pretty...
I taught myself a good bit ML programming in about 4 or 5 months, 3 or 4 hours of coding a night and asking a lot of questions on here and another forum dedicated to 6502 coding. I wrote my second version of Diamond Hunt then. I'm obviously not as skilled as some others here though. I'm just saying it is possible to learn it. If that's your route, I recommend CBM PRG Studio. It was very helpful.
I taught myself a good bit ML programming in about 4 or 5 months, 3 or 4 hours of coding a night and asking a lot of questions on here and another forum dedicated to 6502 coding. I wrote my second version of Diamond Hunt then. I'm obviously not as skilled as some others here though. I'm just saying it is possible to learn it. If that's your route, I recommend CBM PRG Studio. It was very helpful.
Rob