![]() |
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: AustraliaPosts: 175 |
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: AustraliaPosts: 6283 |
Use MID$() VK7JH MMedit |
||||
MustardMan![]() Senior Member ![]() Joined: 30/08/2019 Location: AustraliaPosts: 175 |
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: UkrainePosts: 161 |
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: UkrainePosts: 161 |
On the second thought you might never bump into lack of speed with CMM2. ) |
||||
Womble![]() Senior Member ![]() Joined: 09/07/2020 Location: United KingdomPosts: 267 |
This post "Twisty little passages". There is some code from Peter that shows how to find the address of a variable: Take a look at PEEK on page 100 of the manual. Regarding speed optimisation this refers ![]() 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: AustraliaPosts: 175 |
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: AustraliaPosts: 6283 |
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: AustraliaPosts: 175 |
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: AustraliaPosts: 1003 |
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 KingdomPosts: 2170 |
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: AustraliaPosts: 175 |
@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 KingdomPosts: 10310 |
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: AustraliaPosts: 1003 |
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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |