The IF statement....

Basic and Machine Language

Moderator: Moderators

User avatar
Floopy
Vic 20 Amateur
Posts: 63
Joined: Mon Feb 27, 2017 7:38 pm
Location: US
Occupation: Mechatronics

The IF statement....

Postby Floopy » Mon Feb 27, 2017 7:55 pm

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: 347
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: The IF statement....

Postby Bobbi » Mon Feb 27, 2017 8:04 pm

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 Amateur
Posts: 63
Joined: Mon Feb 27, 2017 7:38 pm
Location: US
Occupation: Mechatronics

Re: The IF statement....

Postby Floopy » Mon Feb 27, 2017 8:09 pm

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: 347
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: The IF statement....

Postby Bobbi » Mon Feb 27, 2017 8:12 pm

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 Amateur
Posts: 63
Joined: Mon Feb 27, 2017 7:38 pm
Location: US
Occupation: Mechatronics

Re: The IF statement....

Postby Floopy » Mon Feb 27, 2017 8:19 pm

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: 347
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: The IF statement....

Postby Bobbi » Mon Feb 27, 2017 8:31 pm

Sure why not? It's always good to have new stuff to play with on the VIC :)

User avatar
R'zo
Vic 20 Afficionado
Posts: 352
Joined: Fri Jan 16, 2015 11:48 pm

Re: The IF statement....

Postby R'zo » Mon Feb 27, 2017 11:12 pm

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 Devotee
Posts: 236
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: The IF statement....

Postby wimoos » Tue Feb 28, 2017 1:05 am

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.
PAL, two-prong VIC20 on 65C02 with 3k RAM expansion internal, 32k NOVRAM expansion external, DS1307 RTC and S-Video mod; 64HDD in FreeDOS on a thin-client; selfwritten 65asmgen; tasm; maintainer of WimBasic

User avatar
Mike
Herr VC
Posts: 2927
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: The IF statement....

Postby Mike » Tue Feb 28, 2017 2:27 am

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: 347
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: The IF statement....

Postby Bobbi » Tue Feb 28, 2017 6:56 am

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 Nerd
Posts: 803
Joined: Sun Oct 17, 2010 2:28 pm
Location: UK

Re: The IF statement....

Postby beamrider » Tue Feb 28, 2017 7:14 am

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 Amateur
Posts: 63
Joined: Mon Feb 27, 2017 7:38 pm
Location: US
Occupation: Mechatronics

Re: The IF statement....

Postby Floopy » Tue Feb 28, 2017 10:15 am

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: 1082
Joined: Fri Jan 04, 2008 5:11 pm
Location: Austin, Texas
Occupation: Game maker

Re: The IF statement....

Postby Kweepa » Tue Feb 28, 2017 12:44 pm

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.


Return to “Programming”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest