Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 12:02 26 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Short strings

Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 06:01pm 12 Oct 2012
Copy link to clipboard 
Print this post

Save space by using short strings.
MMBasic strings are a fixed 255 bytes long.
If you have a lot of short strings, you can save a lot of valuable memory by using short strings.
The following code creates an array of any ( < 255 ) length strings.
It would be a bit slow for intensive string manipulation but it can save heaps of space.

The code makes use of PEEK and POKE so it does have the potential to cause havoc.

function makeSmall( aa,bb)
First we work out the number of normal string elements needed to store out small array.
We then create the holding array and find its memory address.
The first 2 memory locations are used to store the new array dimensions.

function PutSmall(b, a$)
We pass the array element and the string for storing.
If the string is too long, it is truncated without any error message.
The function returns -1 if an error occurs or the length of the string.

function GetSmall$(b, a$)
Pass the array element and an optional error string
Return the string or the error string if array is out of bounds

'small strings by TassyJim
x=makeSmall( 39,40) ' 38+1 bytes long, 40 elements
print x;" string elements used"
memory
for n = 1 to 41
test$="Test string number "+str$(n)
x=PutSmall(n,test$)
print test$;" ";x
next n
print
print "Now we retrieve the strings"
for n = 1 to 41
test$=GetSmall$(n,"woops!")
print n;" ";test$
next n
memory
end

function makeSmall( aa,bb) 'aa= string length, bb= array elements
local ascii$,g,n,c
makeSmall=-1
dim smallstring$(int(aa*bb/256)+1) ' allocate the required memory as a normal array
for g=0 to 32 'look through a maximum of 32 variables
ascii$=""
for n = 0 to 32
c=peek(VARTBL,n+g*56)
if c=0 then exit for
ascii$=ascii$+chr$(c) ' retrieve variable name (end is chr$(0))
next n
SSl=peek(VARTBL,52+g*56)+peek(VARTBL,53+g*56)*256 ' string storage low word
SSh=peek(VARTBL,54+g*56)+peek(VARTBL,55+g*56)*256 ' string storage high word
if ascii$="SMALLSTRING$(" then
poke SSh, SSl, aa 'store the max string length
poke SSh, SSl+1, bb ' store the max number of elements
makeSmall=int(aa*bb/256)+1
exit for
endif
next g
end function 'returns -1 for error or number of normal string array elements used

function PutSmall(b, a$) ' element number, string to store
local n,aa,bb
aa=peek(SSh, SSl) ' retrieve the max string length
bb=peek(SSh, SSl+1) ' retrieve the max number of elements
n=-1
b=int(b)
if b>0 and b<=bb then
if len(a$)>=aa then
a$=left$(a$,aa-1) ' trim the string to max length
endif
poke SSh, SSl+b*aa, len(a$) ' store the string length
for n = 1 to len(a$)
c=asc(mid$(a$,n,1))
poke SSh, SSl+b*aa+n, c ' store the string
next n
endif
PutSmall=n ' returns length of string or -1 if subscript out of range
end function

function GetSmall$(b, a$) ' element number, error message if out of bounds
local n,aa,bb,s
aa=peek(SSh, SSl) ' retrieve the max string length
bb=peek(SSh, SSl+1) ' retrieve the max number of elements
b=int(b)
if b>0 and b<=bb then
a$=""
s = peek(SSh,SSl+aa*b) ' size of element
for n = 1 to s
k=peek(SSh,SSl+aa*b+n) ' build the string
a$=a$+chr$(k)
next n
endif
GetSmall$=a$ 'returns string or error message
end function


Tested on various hardware but not as part of a big program.
JimEdited by TassyJim 2012-10-14
VK7JH
MMedit   MMBasic Help
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1101
Posted: 01:16am 14 Oct 2012
Copy link to clipboard 
Print this post

Hi Jim, this is realy interesting as memory limits are a problem for me in a couple of programs I am working on. However, as an old techie (who has probably sniffed a few too many solder fumes over the years) I am struggling a bit with some of the concepts. If you have some time, could you perhaps expand a little on the program and how it works?

If a string has a default size of 255 bytes, does a string array have a default of 255 bytes for each element? That is, if I DIM a string array an_array_$(60), does this reserve 61 times 255 bytes of memory?
Also, I understood that arrays in Basic are referenced from 0 to the value in the dim statement less 1 ie. an_array(60) has elements 0 through 59 - is this correct?

As I understand your program, you create a single dimension string array with the number of elements equal to the number of small strings I want to handle, all of a short length which I can determine? I can then index into this array to store or recall the short strings?

I apologise for my lack of knowledge in the in depth handling of arrays and would greatly appreciate any comments/expansions you are able to make,

Regards, Doug.
... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 12:28pm 14 Oct 2012
Copy link to clipboard 
Print this post

Doug,
Strings use 256 bytes. The first byte is the length of the string and the remaining 255 bytes are where the actual string is stored. I used the same method so if you need strings 32 bytes long, you will have to specify 33 as the size.
In MMBasic you can specify the starting array number as zero or one. I have stayed with one because that is the way I have always done it.
I use the 'zero' location to store the new array size to save on 2 variables.

An example that might be relevant to you.
I have a list of 652 Aussie towns.
The longest name in the list is "Kingston South East" which is 19 characters long.
An array of 652 X 20 bytes is needed. 652*20/256 = 51
My method creates an array with 51 elements which uses about 13k
Full length strings would need 163k of memory
I also have a list of 65k place names but that's too much for the Maximite to chew on!

I deliberately kept to single dimension arrays but it is easy to use the short array as 2 dimensions. Most times a single dim array for the string and another standard numeric array for the rest of the data. In the Towns example this numeric array will store the latitude and longitude of the towns.

It is thanks to Geoff implementing PEEK and POKE and giving us the location of the variable table that I was able to do this.

I hope that helps.
Jim
VK7JH
MMedit   MMBasic Help
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 04:11pm 14 Oct 2012
Copy link to clipboard 
Print this post

  TassyJim said   Save space by using short strings.
MMBasic strings are a fixed 255 bytes long.
If you have a lot of short strings, you can save a lot of valuable memory by using short strings.
Jim


Hi Jim,
This seems to be a good candidate for the MMBasic library?
Greg
 
djuqa

Guru

Joined: 23/11/2011
Location: Australia
Posts: 447
Posted: 04:18pm 14 Oct 2012
Copy link to clipboard 
Print this post

  TassyJim said   Save space by using short strings.
MMBasic strings are a fixed 255 bytes long.
If you have a lot of short strings, you can save a lot of valuable memory by using short strings.

Jim
You can also save a lot of memory often by re-thinking the algorithm and minimising the usage of strings (especially string arrays).
VK4MU MicroController Units

 
shoebuckle
Senior Member

Joined: 21/01/2012
Location: Australia
Posts: 189
Posted: 12:37pm 15 Oct 2012
Copy link to clipboard 
Print this post

  paceman said  
Hi Jim,
This seems to be a good candidate for the MMBasic library?
Greg


I agree. Why not send a copy to mmlib@geoffg.net and I will add it to the library.

On second thoughts, Jim, if you are happy for it to be included, I can extract the code from this thread and add your explanation in a readme file.

Cheers,
Hugh
p.s. Unless someone else has library update access, it will need to wait until Geoff gets back from holiday.Edited by shoebuckle 2012-10-16
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 04:37pm 15 Oct 2012
Copy link to clipboard 
Print this post

  shoebuckle said  
  paceman said  
Hi Jim,
This seems to be a good candidate for the MMBasic library?
Greg


I agree. Why not send a copy to mmlib@geoffg.net and I will add it to the library.

On second thoughts, Jim, if you are happy for it to be included, I can extract the code from this thread and add your explanation in a readme file.

Cheers,
Hugh
p.s. Unless someone else has library update access, it will need to wait until Geoff gets back from holiday.


I am certainly happy for it to be added to the library.
It might be worth Geoff checking that it is not likely to cause grief.
Playing with PEEK and POKE should not be taken lightly.

Jim

VK7JH
MMedit   MMBasic Help
 
shoebuckle
Senior Member

Joined: 21/01/2012
Location: Australia
Posts: 189
Posted: 05:10pm 15 Oct 2012
Copy link to clipboard 
Print this post

  TassyJim said  I am certainly happy for it to be added to the library.
It might be worth Geoff checking that it is not likely to cause grief.
Playing with PEEK and POKE should not be taken lightly.

Jim


Thanks Jim,
I will check with Geoff before committing it to the library. If you make any mods, please let me know so I can include them.
Cheers,
Hugh
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024