Intergers

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Intergers

Post by Jeff-20 »

I am not a math whiz. I recall reading somewhere that intergers (A%) use less memory than normal variables (A).

What is the difference in memory? Is that difference negated by the longer program?

For example, maybe it saves 3 bytes by not saving a decimal. What if I use the variable A% five times in a program? Wouldn't that cost me five extra bytes as opposed to using A just from the extra memory the %-sign uses in line memory?

I suppose if I run out of the 26 letter variables, A% is more efficient than A2 or AA. But, is the above true? I am confused.
High Scores, Links, and Jeff's Basic Games page.
User avatar
Mike
Herr VC
Posts: 4838
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

I am not a math whiz.
That isn't necessary here, anyway. :)

You must tell the "savings" in programm space apart from those in variable space. Same applies to execution time.

All references to an integer variable need the extra byte for the % sign within the BASIC program.

As long as you use a single integer there is *no* savings in variable space, as all variables - be it a real number, an integer number, a string descriptor[*], or a function descriptor - occupy 7 bytes. The data for an integer needs 2 bytes for the variable name, 2 bytes data, and 3 bytes are wasted.

Contrary to this, if you use an array of integers (say, DIM M%(2,2)) the array *will* need only 2 bytes per element. The savings are here.

Regards execution time: Internally, all calculations are done with real numbers (in CBM BASIC). If the interpreter needs to read an integer variable (single or in an array), it must be converted to real. Same applies to storing. So integers cost you additional execution time.

Greetings,

Michael

[*]: Strings of course need their own space at the end of BASIC memory.
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

Also remember that each element of the integer array can only hold values -32768 to 32767. If you need bigger values, use decimal variables or develop your own Basic that works with 24 bit integers (3 bytes). :lol:
Anders Carlsson

Image Image Image Image Image
Post Reply