01.03.2014 Views

Section 2 - Commodore Computers

Section 2 - Commodore Computers

Section 2 - Commodore Computers

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

May. Mav. 1Q82.lssue 1982. Issue 24 2d COMPUTEI COMPUTE! 139<br />

100A=A+I<br />

=<br />

200 IF A>25 THEN Z% = 0<br />

200IFA>25THENZ% = 0<br />

you can save four out of the five byte overhead of<br />

the second line by coding:<br />

100 A=A+ A = A+1:IFA>25 I:IF A>25 THEN Z% = 0<br />

However, no suggestions are free of charge<br />

However, no suggestions are free of charge<br />

and there is also something to watch out for in this<br />

instance. You may freely combine statements for<br />

up to eighty characters, but it is possible that one of<br />

the statements you are combining might be the<br />

object of a GOTO or GOSUB statement, in which<br />

object of a GOTO or GOSUB statement, in which<br />

case you will receive the ""UNDEFINED STATE­<br />

MENT" error message. In the example immediately<br />

above, if there were another statenlent statement in the program<br />

which was "GOTO 200", line number 200<br />

would not be in the program after combiningg the<br />

two lines and you would get the error message. If<br />

youu had a statement that was "GOTO 100", that<br />

would not cause any problems.<br />

Another item to watch for when combining<br />

statements is not to combine a line with a preceding<br />

line that contains an IF statement. The statement<br />

shown above is alright. However, if the two lines<br />

were reversed:<br />

were reversed:<br />

100IFA>25THENZ% = 0<br />

200 A=A+ A = A+11<br />

you would not be able to combine the lines as:<br />

you would not be able to combine the lines as:<br />

100IFA>25THENZ% = O:A 0:A = A+lA + 1<br />

without altering the meaning of the statement. In<br />

this instance, the addition statement would only be<br />

executed if A were greater than twenty-five which<br />

is not the intent of the original.<br />

The last item that returns a little usable memory<br />

at the expense of readability and documentation is<br />

the use of short variable names. Although variable<br />

names may be up to 255 characters, BASIC uses<br />

only the first two characters (pluss the $ and %<br />

suffixes for string and integer variables respectively).<br />

Each character in the variable name occupies<br />

a byte wherever it is used; therefore you should<br />

limit variable names to two characters and one<br />

character would be even more thrifty from a<br />

memory-use point of view. Limiting the names to<br />

two characters could have a side benefit inasmuch<br />

it may eliminate a potential source of programming<br />

error. If you had two variables, one named "TAPE"<br />

and the other named "TASTE," BASIC would<br />

only recognize "TA" as the name and would, in<br />

effect, be dealing with a single variable.<br />

Avoiding a technical discussion as to why it is<br />

so, it is usually more economical to use constants<br />

instead of variables whenever possible. A constant<br />

consists of data stored in the BASIC statement<br />

itself. Constants may require less memory than<br />

variables, especially in cases where the constant is a<br />

relatively short string. As the length of the string<br />

relatively short string. As the length of the string<br />

increases, the amount of savings diminishes because<br />

the repetition of the constant also occupies memory.<br />

The following lines each contain a constant:<br />

100 A = A + 1<br />

("1" is the constant)<br />

100 S$ = D$ + "SUFFIX"<br />

("SUFFIX" is the constant)<br />

100 PRINT "TOTAL= = ";X<br />

("TOT AL=" is the constant)<br />

("TOTAL =" is the constant)<br />

An illustration of the savings that cann be<br />

gained is:<br />

100 T$ = "THIS IS A TEST"<br />

200 PRINTT$<br />

T$<br />

300 PRINTT$<br />

T$<br />

occupies 72 bytes of memory, whereas<br />

100 PRINT ""THIS IS A TEST"<br />

200 PRINT ""THIS IS A TEST"<br />

only occupies 69 bytes of memory. That is not<br />

much of a savings because the string "THIS IS A<br />

TEST" is relatively long; if it were shorter, the<br />

TEST" is relatively long; if it were shorter, the<br />

savings would be more dramatic. The reason for<br />

this is that, when data is assigned to a variable, it<br />

this is that, when data is assigned to a variable, it<br />

requires two areas of memory, but a constant requires<br />

only one (in the instruction itself).<br />

itself}-<br />

BASIC is sometimes very shrewd as far as<br />

memory management is concerned. BASIC is<br />

smart enough to know when you have used a string<br />

and will reuse it rather than recreating it again in<br />

memory. Thus, if you have coded the statement:<br />

PRINT "THIS IS A TEST" and, elsewhere in your<br />

PRINT "THIS IS A TEST" and, elsewhere in your<br />

program you coded A$ = "THIS IS A TEST",<br />

although memory would be required to contain<br />

pointers for the variable "A$", the actual text string<br />

pointers for the variable "A$", the actual text string<br />

'TH "THIS IS A TEST" would not be recreated in<br />

memory (except in the instruction itself). The<br />

original text string would be pointed to by the<br />

variable. This is starting to border on the kind of<br />

technical information that this article has tried to<br />

avoid, but is interesting enough to pass on.<br />

Don't AvoId Avoid Integer Variables<br />

It seems that most people writing BASIC programs<br />

never bother with integer variables and, yet, that is<br />

where a significant ificant savings in memory can be attained.<br />

This is particularly true if the program<br />

contains arrays. Consider that, for each element in<br />

an array, the number of bytes occupied is as follows:<br />

A string array = 3 bytes plus the length of the<br />

string per element<br />

A floating point array = 5 bytes per element.<br />

An integer array = 2 bytes per element.<br />

The contents of-many arrays do not require<br />

the use of decimal points, but it is easier to code<br />

the use of decimal points, but it is easier to code<br />

"DIM A(15)" rather than "DIM A%(15)." A%(I5)." By using<br />

the integer form, you would save 45 bytes of mem-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!