Saving space in PBASIC 2.5 code
I try to utilize the program space as much as possible, nearly all
banks in BS2p is full.
I wonder how several coding solution could affect the need of space.
E.g. is the use of case more space efficient the If....than
Is the length of variable names or gosub headings making any
difference?
What about the `End' statement? Do I need it, if the code never
retch the point, or will the compiler look for it?
Somebody figured out smart ways to save or utilize space on the BS2p?
Stein.
banks in BS2p is full.
I wonder how several coding solution could affect the need of space.
E.g. is the use of case more space efficient the If....than
Is the length of variable names or gosub headings making any
difference?
What about the `End' statement? Do I need it, if the code never
retch the point, or will the compiler look for it?
Somebody figured out smart ways to save or utilize space on the BS2p?
Stein.
Comments
effect on the size of your compiled program -- so be verbose with the names you
choose to make reading your program easier.
If you never reach and END statement, you can leave it out. The token for end
is $00 which is what empty EEPROM space is filled with. Just be careful that
your program can't "fall" into your subroutines -- you'll get unpredictable
results.
In PBASIC 2.5, you can save program space by wrapping DEBUG and SEROUT lines.
These functions take a bunch of code because of their complexity, so minimizing
them will save space. So instead of this:
DEBUG { some debug data }
DEBUG { some more debug data }
do this:
DEBUG { some debug data },
{ some more debug data }
Notice that the first DEBUG line ends with a comma -- this is what allows you to
extend it onto the next line. All so note that this technique works for any
comma-delimited list within PBASIC.
On really interesting note is that you can even embed comments in the list when
is spreads across lines:
DEBUG { something }, ' this is really good data
{ something else }, ' this is even better
{ altogether best } ' and this stuff just rocks!
Off the top of my head, these are the instructions that use comma-delimited
lists:
BRANCH
DATA - no space conservation with this one
DEBUG *
DEBUGIN - don't get crazy with long input lines
I2CIN
I2COUT
LCDIN
LCDOUT
LOOKUP
LOOKDOWN
ON..GOTO/GOSUB
OWIN
OWOUT
SERIN
SEROUT *
* = Best opportunity for program space saving.
-- Jon Williams
-- Applications Engineer, Parallax
-- Dallas Office
Original Message
From:
>banks in BS2p is full.
>
>I wonder how several coding solution could affect the need of space.
Hi Stein,
Do you have many DEBUG or serial commands that output strings of
data? You can save a huge amount of RAM by turning those strings
into a bunch of DATA statements, and then write a little routine that
sends them. The deal is that each character in a DEBUG or SEROUT (or
LCDOUT or I2COUT or OWOUT) takes 14 bits of EEPROM, but when you
store the same character as DATA it takes only 8 bits. The extra
subroutine does require some overhead, but the break even point in
saving bytes is surprisingly low. You could even move that string
data into an external memory (like your 45DB081!).
---inefficient method:
DEBUG "This is a long sentence to be followed by many more",CR
DEBUG "supercalaffagilisticexpialidocious redisestablishmentarianism",CR
---efficient method:
phrase1 DATA "This is a long sentence to be followed by many more",CR,0
phrase2 DATA "supercalafabilisticexpialidocious
redisestablishmentarinanism",CR,0
' null terminated strings
phrase2do=phrase1
GOSUB tell
phrase2do=phrase2
GOSUB tell
END
tell:
idx=0
DO
READ phrase2do+idx,char
IF char=0 THEN RETURN
DEBUG char
idx=idx+1
LOOP
The same thing goes for long lookup or lookdown tables. Storing them
as DATA can be much more efficient use of EEPROM when there are a lot
of values.
Use of subroutines instead of repeating the same code over and over
saves EEPROM. There are little things you can do to save a few bits.
For example, PAUSE 2048 takes a little less EEPROM space than PAUSE
2000, because of the particularly efficient way that the compiler
stores exact powers of two.
You asked specifically about PBASIC 2.5. You can save a few bytes
here and there by choosing the right form of a command. For example,
"DO : LOOP WHILE" takes fewer bytes than "DO WHILE : LOOP", so if it
doesn't make a difference in your program function, why not? More
info about the efficiency of different PBASIC 2.5 commands at this
URL:
http://www.emesystems.com/BS2pbasic25.htm
-- Tracy