Classic Computer Magazine Archive ANTIC VOL. 2, NO. 4 / JULY 1983

Conserve RAM

With BASIC's USR function

by JERRY WHITE

While exact figures are unavailable from Atari, it is estimated that more than half of all Atari Home Computer owners have 16K systems. The ATARI 400 is supplied with 16K of memory, but used with the cassette system, there is only 14K of available RAM. Until about a year ago, all ATARI 800 systems were sold with only 16K on board, but owners could add RAM cards to fill out their systems to a maximum of 48K. The ATARI 800 is now sold with the full 48K RAM on board.

Conserving memory is important not only because many programmers must squeeze their work into limited memory space, but also because good programmers are not wasteful programmers. Elegant algorithms and programming techniques which make the most of the computer's capabilities in the least amount of memory space are the signatures of good programmers.

Using the USR command in BASIC can help to conserve memory. The USR function (described on Page 36 of the ATARI BASIC REFERENCE MANUAL), allows a programmer to use machine language assembler routines in the middle of a BASIC program. Such routines require less memory "overhead" than their equivalent in a higher language. The following example will show you how to "hand-assemble" a simple space-saving machine language routine.

Many BASIC programs contain a statement line similar to:

100 GRAPHICS 0:POKE 82,2:POKE 83,39:SETCOLOR 2,15,0: SETCOLOR 4,15,0: SETCOLOR 1,0,13

The instructions in Line 100 require 127 bytes of RAM. If you changed the SETCOLOR commands to POKE commands, you could cut RAM usage to 103 bytes:

200 GRAPHICS 0: POKE 82,2:POKE 83,39:POKE 710,240:POKE 712,240:POKE 709,13

The value that is POKEd into the color registers is found by multiplying the desired hue by 16 then adding the luminance (a table of hues is found in the ATARI BASIC -REFERENCE MANUAL on Page 50).

In assembler, the equivalent command to the POKE command is a two-step sequence of "load accumulator" (LDA) and "store accumulator" (STA). The sample DATA2STR program below creates a machine language subroutine that executes these POKE commands. The Assembly Language code is indicated in the REM statements.

PROGRAM OPERATION

The new Line 100 will be added to this program when it RUNs. Line 100 will begin with the GRAPHICS 0 command, but the POKE commands for margin settings and screen colors will be replaced with a USR command and a string of machine language instructions. This new line will require but 77 bytes of RAM.

After typing DATA2STR into your computer, be sure to SAVE it. This is always important when you are working on BASIC programs which contain machine language subroutines. The computer can lock up if you have accidentally made a typo or missed a keystroke, and you may have to turn the computer off and restart. If you haven't SAVEd your program to disk or cassette, you will have to retype the whole thing!

When you successfully execute DATA2STR it will generate a new Line 100. For demonstration purposes, you will note that Line 100 ends with a STOP command. If you run this program a second time, your screen colors should be different, and the program will stop at Line 100. You may then type CONT (or CON.) and press RETURN to continue. The screen will briefly show its original colors, and Line 100 will be created once again.

AVOID NUMERIC CONSTANTS

Whether you use the USR function or not, one of the greatest consumers of memory in ATARI BASIC programs is the numeric constant. Every time you use a number that is not part of a character string, it costs 7 (yes, seven!) bytes of RAM. Thus many programmers use numeric variables for commonly-used numbers such as 0 and 1. Once you have defined these variables, each additional reference to that number variable will cost only one byte, no matter how long the variable name is. So a number such as "1" may be redefined N1=l or N2 = 2. If you like descriptive variables, then you might use ZERO=0, ONE=1, TWO=2, etc. Note, however, that while you can say ZERO=0 or TWO=2, you must say LET ONE=1. Without the command LET, the BASIC language will interpret your command as ONE.

Jerry White is a professional programmer whose products are available under several labels, such as Swiftware, Adventure international and Educational Software. He is a Technical Consultant and frequent contributor to ANTIC.