Page 1 of 3

The IF statement....

Posted: Mon Feb 27, 2017 7:55 pm
by Floopy
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 :D .
(Sorry for my English)

Re: The IF statement....

Posted: Mon Feb 27, 2017 8:04 pm
by Bobbi
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....

Posted: Mon Feb 27, 2017 8:09 pm
by Floopy
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 :?:

Re: The IF statement....

Posted: Mon Feb 27, 2017 8:12 pm
by Bobbi
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.

Re: The IF statement....

Posted: Mon Feb 27, 2017 8:19 pm
by Floopy
Okay, thank you for your advice :D . I might post the program I made recently just to see general reaction.

Re: The IF statement....

Posted: Mon Feb 27, 2017 8:31 pm
by Bobbi
Sure why not? It's always good to have new stuff to play with on the VIC :)

Re: The IF statement....

Posted: Mon Feb 27, 2017 11:12 pm
by R'zo
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 :?:
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.

Re: The IF statement....

Posted: Tue Feb 28, 2017 1:05 am
by wimoos
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.

Code: Select all

IF B<>0 THEN
works the same as

Code: Select all

IF B THEN
The last one is obviously faster.

When you are writing:

Code: Select all

IF <cond1> AND <cond2> THEN...
you'd better rewrite this as

Code: Select all

 IF <cond1> THEN IF <cond2> THEN...
with <cond1> being the easiest to evaluate (least number of variables and least complex arithmetic).

Finally, sometimes you can replace IFs by

Code: Select all

ON <index> GOTO line,line,...
or

Code: Select all

ON <index> GOSUB line,line,...
Also, the way GOTO is implemented can be of significant impact, especially when it is a long program.

Code: Select all

4530 GET A$:IF A$="" THEN 4530
in a lengthy program works slower than

Code: Select all

10 GET A$:IF A$="" THEN 10
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.

Re: The IF statement....

Posted: Tue Feb 28, 2017 2:27 am
by Mike
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.
Bobbi wrote:As you are discovering, BASIC is very slow for arcade style games. Most of the better ones are written in machine code.
The use of BASIC is not necessarily an impediment when the program is well structured.

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.

Re: The IF statement....

Posted: Tue Feb 28, 2017 6:56 am
by Bobbi
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.

Re: The IF statement....

Posted: Tue Feb 28, 2017 7:14 am
by beamrider
If you want to start from the fundamentals, you may want to take a look at this excellent course...

www.nand2tetris.org

Re: The IF statement....

Posted: Tue Feb 28, 2017 10:15 am
by Floopy
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!

Re: The IF statement....

Posted: Tue Feb 28, 2017 12:44 pm
by Kweepa
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.

Re: The IF statement....

Posted: Tue Jan 08, 2019 9:08 pm
by dr.geek
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....

Re: The IF statement....

Posted: Tue Jan 08, 2019 10:28 pm
by GreyGhost
And very pretty... :wink:

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.