Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:14 01 Aug 2025 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 : Access a STRING as an ARRAY of characters/bytes?

Author Message
MustardMan

Senior Member

Joined: 30/08/2019
Location: Australia
Posts: 175
Posted: 05:52am 20 Sep 2020
Copy link to clipboard 
Print this post

Hi,

Is it possible to access a string (without having to use PEEK) as an array of bytes as is done in C and in some BASIC implementations?

For example, by executing a$="bytes" : x$=a$(2) I would get x$="y"

I've tried different combinations but it seems it can't be done without going the PEEK way...


Cheers,
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 06:00am 20 Sep 2020
Copy link to clipboard 
Print this post

Use MID$()
VK7JH
MMedit
 
MustardMan

Senior Member

Joined: 30/08/2019
Location: Australia
Posts: 175
Posted: 07:09am 20 Sep 2020
Copy link to clipboard 
Print this post

Doh!

Of course. I'd only used it for grabbing a string (multiple characters) out of a string, but of course it can be used for single bytes.

Thank you.
 
Cyber

Senior Member

Joined: 13/01/2019
Location: Ukraine
Posts: 161
Posted: 02:58pm 20 Sep 2020
Copy link to clipboard 
Print this post

If speed is critical MID$ would be slower. By they way, how do you do this with PEEK?
 
Cyber

Senior Member

Joined: 13/01/2019
Location: Ukraine
Posts: 161
Posted: 03:13pm 20 Sep 2020
Copy link to clipboard 
Print this post

On the second thought you might never bump into lack of speed with CMM2. )
 
Womble

Senior Member

Joined: 09/07/2020
Location: United Kingdom
Posts: 267
Posted: 03:50pm 20 Sep 2020
Copy link to clipboard 
Print this post

  Cyber said  If speed is critical MID$ would be slower. By they way, how do you do this with PEEK?

This post "Twisty little passages".  There is some code from Peter that shows how to find the address of a variable:
  matherp said  FWIW

I would read the entire z file into an integer array using LONGSTRING APPEND.

Then access the individual bytes

addr%=peek(varaddr array%())

opcode%=peek(byte addr%+offset%)

Then rather than a huge "if then else" or "switch" statement I would binary chop the opcodes in some intelligent way, possibly in two chops, and then have separate "if then else" for each sub group. And of course prioritise the most common opcodes at the top of the clauses

Take a look at PEEK on page 100 of the manual.

Regarding speed optimisation this refers  
  matherp said  One big overhead in functions and subroutines is local variables as they get created and deleted each time the subroutine is called. Obviously one of the big benefits of subroutines and functions is precisely that they have local variables though  

Setting variables as STATIC may improve this (untested)  but you then need to be careful that you don't assume they are set to zero when used. Local strings and arrays are worst case as they have to get memory off the MMBasic heap as well as allocating the variable header.

Making sure variable names are unique in the first 4 characters will offer minor advantages.

Finally check the new MATH command for simple things like setting arrays, copying arrays and finding MINs and MAXs in arrays. This will operate much faster than for loops in Basic

There is a mine of information on optimising code for the CMM2 here, although sometimes you to have to dig deep to find it  

Hope this helps

Womble
Edited 2020-09-21 01:51 by Womble
 
MustardMan

Senior Member

Joined: 30/08/2019
Location: Australia
Posts: 175
Posted: 09:51pm 20 Sep 2020
Copy link to clipboard 
Print this post

In 'the olden days' (my beard is getting grayer as I speak...) variables in BASIC were nearly always one or two characters. In fact some variants of BASIC limited you to such.

These days it is extremely common to use verbose names, and this presents two problems, one more serious than the other.

The first is search speed, which is not such an issue if the names are different in the first few characters.

The second is the memory consumed as the entire variable name is stored. I know memory sizes have grown considerably, but so too has the complexity of programs, and I am starting to note excessive use of memory in my programs - particularly if a 'properly named' variable appears multiple times.

As an example, it is now common to write variables like:
AmbientTemperatureInDegreesCentigrade
and
AmbientTemperatureInDegreesKelvin

where once they would have been written:
TC
and
TK

Obviously searching will take longer (but the CPU is so much faster it probably doesn't matter), but the 'new' way of declaring variables starts to eat up a lot of program memory.
I would think this also un-necessary eats away RAM. Some more modern BASICs chop variables to 32 characters (which is still really long, especially compared to the way compilers do it), which is not really a problem on a PC with Gigs of RAM, but it is on a microcontroller with only a few K.

In the example above the variable would take up either 32 or 38 bytes to store the name plus 8 bytes to store the value (assuming a 64 bit integer) in RAM. However the bigger problem is storing the name multiple times in the program memory, and to my knowledge it is not shortened there, and will take up 38 flash memory words (?) each time. I am personally starting to run into program size issues even after 'crunching'.

Compiled languages have neither of these problems as the variables are only ever referred to in the compilation stage and from then on a compiler allocated memory location is used.
I wonder if the most excellent 'MMEditPlus' could be enhanced with the ability to optionally 'crunch' variable names down to two three or four characters? Jim?
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 10:28pm 20 Sep 2020
Copy link to clipboard 
Print this post

  MustardMan said  
I wonder if the most excellent 'MMEditPlus' could be enhanced with the ability to optionally 'crunch' variable names down to two three or four characters? Jim?


For a start, in MMBasic you are limited to 32 bytes for variable names.
With the CMM2, having the first 4 characters unique is a means of gaining some speed improvement.
It is common to use short loop counters etc.

If you are concerned, you can come up with meaningful short names. Much more meaningful than anything that MMEdit can come up with.

My biggest aversion is letting my program do a global search and replace on your hard earned masterpiece.
I put that in the same bucket as a global file delete without the recycle bin as a backup.

So, no, sorry, but you could write your own in Basic. I know someone on the forum did do it many years ago (search the forum for "crunch.bas")

Jim
VK7JH
MMedit
 
MustardMan

Senior Member

Joined: 30/08/2019
Location: Australia
Posts: 175
Posted: 03:15am 21 Sep 2020
Copy link to clipboard 
Print this post

Hi Jim,

You are spot on - I would certainly not want any of my "majestic" code being done over in such a fashion! I was thinking more that the 'source' gets transformed into an 'executable' like MMedit does when you crunch - downloading the 'executable' but leaving your 'source' completely intact.

I found the thread on 'crunch.bas' you mention and it would seem that you've already covered all this stuff a good five years back!

Shortening variable names (only in the final 'executable') is something it reputedly does so if I start to bump up against program size problems, I'll look to what this can do for me. It does say it doesn't 'run' on the uMite - do you know if there was ever a stand-alone executable made (like your MMedit)?

And concatenating multiple lines into colon separated lines sounds like a nightmare!


I was thinking back to the good-old-days (or was it bad-old-days!) where the renumber program would scan your code and check all the GOTOs and GOSUBs to replace the destinations, doing the tedious things that computers are so good at. Fortunately line numbers are long gone, but crunch.bas as well as you MMEdit/MMEditPlus do significantly more than just search for GOTOs and GOSUBs!

Cheers,
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 1003
Posted: 05:11am 21 Sep 2020
Copy link to clipboard 
Print this post

An add on for MMEdit I wrote and use all the time still. Replacing descriptive variable, function and sub names with short names at load time was my main driver at the time. Also simplifies handling library code by keeping it as part of the main program.


MMReplace

Regards
Gerry
Latest F4 Latest H7 FotS
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 02:59pm 21 Sep 2020
Copy link to clipboard 
Print this post

  Cyber said  If speed is critical MID$ would be slower. By they way, how do you do this with PEEK?


Poke Var a$,offset,byte

Offset 0 is the string size, 1 to 255 is the string body

=Peek Var a$,offset

I find the slicing functions are fairly fast, doing pretty much the above but in the assembled machine code.

One area I found was quicker was building up a string one byte at a time. POKE VAR was quite a bit faster than a$=a$+Chr$(x) - there's a deceptive amount of evaluation in that simple statement.

As all simple strings are 255 bytes long, you can poke into the variable space at offsets 1 to 255 with confidence then just tweak the byte at offset 0 to set the length of the string when you are finished. I used this method when retrieving bytes from a Winbond flash memory  that I want in a string. In the absence of SPI Read a$, I had to find a faster way to pull data into strings one byte at a time.

With concatenation, the string has to be in a presentable form at all times so there is a degree of "overhead" you can dispense with if you are being dirty. Not always the advantage you think but suck it and see.
Edited 2020-09-22 01:02 by CaptainBoing
 
MustardMan

Senior Member

Joined: 30/08/2019
Location: Australia
Posts: 175
Posted: 11:18pm 21 Sep 2020
Copy link to clipboard 
Print this post

@disco4now
Hi Gerry,

That looks like it will do exactly what I want, and it integrates with MMedit which is great!

A question (not covered in the pdf)... can I put a comment after the #REPLACE x y to put even more detail into a variable description?

eg:

#REPLACE AmbientTemperature AT   ' Exterior air temperature
#REPLACE CoolantTemperature CT   ' Coolant temperature after engine thermostat
#REPLACE RadiatorTemperature RT  ' Coolant temperature after exit from radiator


Cheers,
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 06:55am 22 Sep 2020
Copy link to clipboard 
Print this post

It is important to understand that all simple variables take 64 bytes of RAM regardless of name length so there is no RAM saving using short names. Performance advantage will be small - worst case 8 integer compares vs 1. The only real advantage is saving of program memory space but how many programs get close to the 512K limit? A lot of time could be wasted for little return if you are not careful
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 1003
Posted: 10:14am 22 Sep 2020
Copy link to clipboard 
Print this post

  MustardMan said  @disco4now

A question (not covered in the pdf)... can I put a comment after the #REPLACE x y to put even more detail into a variable description?

eg:

#REPLACE AmbientTemperature AT   ' Exterior air temperature
#REPLACE CoolantTemperature CT   ' Coolant temperature after engine thermostat
#REPLACE RadiatorTemperature RT  ' Coolant temperature after exit from radiator


Cheers,

A quick look at the source and I think its OK for comments, as long as there is a space after the short name.
The line is parsed by pulling fields 2 and 3 assuming a space is the delimiter.
Latest F4 Latest H7 FotS
 
Print this page


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

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025