The IF statement....

Basic and Machine Language

Moderator: Moderators

User avatar
Floopy
Vic 20 Devotee
Posts: 221
Joined: Mon Feb 27, 2017 7:38 pm
Location: US

The IF statement....

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

Re: The IF statement....

Post by Bobbi »

As you are discovering, BASIC is very slow for arcade style games. Most of the better ones are written in machine code.
User avatar
Floopy
Vic 20 Devotee
Posts: 221
Joined: Mon Feb 27, 2017 7:38 pm
Location: US

Re: The IF statement....

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

Re: The IF statement....

Post 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.
User avatar
Floopy
Vic 20 Devotee
Posts: 221
Joined: Mon Feb 27, 2017 7:38 pm
Location: US

Re: The IF statement....

Post by Floopy »

Okay, thank you for your advice :D . I might post the program I made recently just to see general reaction.
-Floopy
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: The IF statement....

Post by Bobbi »

Sure why not? It's always good to have new stuff to play with on the VIC :)
User avatar
R'zo
Vic 20 Nerd
Posts: 514
Joined: Fri Jan 16, 2015 11:48 pm

Re: The IF statement....

Post 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.
R'zo
I do not believe in obsolete...
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: The IF statement....

Post 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.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: The IF statement....

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

Re: The IF statement....

Post 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.
User avatar
beamrider
Vic 20 Scientist
Posts: 1446
Joined: Sun Oct 17, 2010 2:28 pm
Location: UK

Re: The IF statement....

Post by beamrider »

If you want to start from the fundamentals, you may want to take a look at this excellent course...

www.nand2tetris.org
User avatar
Floopy
Vic 20 Devotee
Posts: 221
Joined: Mon Feb 27, 2017 7:38 pm
Location: US

Re: The IF statement....

Post 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!
-Floopy
User avatar
Kweepa
Vic 20 Scientist
Posts: 1314
Joined: Fri Jan 04, 2008 5:11 pm
Location: Austin, Texas
Occupation: Game maker

Re: The IF statement....

Post 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.
dr.geek
Vic 20 Drifter
Posts: 22
Joined: Tue Aug 28, 2018 5:05 am

Re: The IF statement....

Post 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....
User avatar
GreyGhost
Vic 20 Nerd
Posts: 525
Joined: Wed Oct 05, 2005 11:10 pm

Re: The IF statement....

Post 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.
Rob
Post Reply