![]() |
Forum Index : Microcontroller and PC projects : CMM2: Questions about BASIC and CMM2 performance
Author | Message | ||||
chris Regular Member ![]() Joined: 24/08/2020 Location: United KingdomPosts: 56 |
Hello, I'm waiting for my CMM2 to arrive, and been reading the documentation and checking out the forum. I just had a general question about GOTOs and the DATA statement. I have mostly been coding with C and Java style languages for the past 20 years, and I mostly forgot about the GOTO statement. Looking at MMBasic, it seems that it is possible to code without GOTO (although I'm not 100% sure about this). I have some questions that maybe people in this forum can answer: (1) Is it possible to code entirely without gotos? (2) Is there still a good reason to use gotos (outside of legacy code compatibility)? (3) If I goto some label outside of a subroutine when I'm in a subroutine, what happens to the stack? (4) I see that in the DATA statements, that expressions are allowed. Are these expressions evaluated at the time of the READ, and presumably, if so, am I correct in assuming that the calculated values from these data expressions could change if some dependant variable or function result changes between READs? (5) What is the reason to use DATA statements rather than just creating a fixed array and placing elements into the array? If I want to set up lookup tables for example, is it better practise to use DATA or just creating and injecting values into an array? (6) I read that CMM2 executes about 270k of instructions per second. I guess that's an average across representative set of instructions. Assuming I'm peeking and poking one byte at a time, how many instructions per second do you think would be possible? Chris |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5090 |
welcome chris, As to your questions: 1/ it is definitely possible to code without goto's. MMBasic is a structured basic, the GOTO is only there as a heritage. 2/ goto's can make ones life easier, if used well. 3/ you point to one of the reasons why using goto's are not a good idea. 4/ - 5/ - 6/ these questions I cannot answer. I can only add that the MMBasic is an interpreter, meaning it reads the text in the program, decodes it, and executes it. You can immagine that reading and decoding require more CPU cycles than a peek or poke. And I think that is true for the majority of the instructions (even the math). Only the IO operations (video / audio / SPI etc...) will be different in that perspective. So the 270k instructions will most likely be true for simple peeks and pokes. PicomiteVGA PETSCII ROBOTS |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
I use micromites but MMBasic is MMBasic ( ![]() 4/ really? I wasn't aware. I was under the impression the DATA values are static, I have never used/encountered this, even in other Basics - maybe I have been ignorant all this time, which is entirely possible... but then I have not used every version so... it sounds like it may be slow to READ data if each element has to be evaluated - compared o a straight lookup. I will do some code to check. 5/ True there is more than one way to store fixed data in your prog but I think DATA scores for compactness & clarity. If you allocate more than a few values straight into an array at DIM time, it's a bit waffly. I READ data into an array from DATA statements all the time. Data in DATA statements is accessible only serially, you can redefine the pointer at any time with RESTORE (I always thought it would be nice to have multiple pointers to allow multiple data streams) h EDIT: 'tis true... Dim Integer a,b,c,d Restore Freddy Read a Read b Read c Read d ? a,b,c,d Freddy: Data 1,2+2,a+b, Val("34") > RUN 1 4 5 34 > not sure how I feel about that. Could provide some interesting methods of processing data/results, but the evaluation has to impact the speed of use. Thinking about it, I guess it means you only have to have a single method to get a value into play from your prog, otherwise you'd need two; one for normal stuff and another specifically to take values from DATA statements with no processing. Interesting. I am sure i could think of something desperately clever to make use of this ![]() This reminds me of INPUT on the ZX81 which also permitted expressions (including variables). Edited 2020-08-26 21:32 by CaptainBoing |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4311 |
I believe populating arrays from DATA statements involves two copies of the DATA existing in-memory, one in the text of the BASIC program and the other in the populated arrays. So if you are in a restricted memory environment (i.e. not the CMM2) but you do have a file system (presumably that excludes some of the Micromites?) it may be advisable to read the data from a file. Regards, Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
so I get to thinking... it makes sense that elements of DATA are evaluated, otherwise how could we have stuff like &B1111011 etc...? Then I think... Have I been missing this all the time? By going back to the earliest proper programming job I had (embedded CPCs in radar systems) which uses Mallard Basic (and later the "same" on PCs) I have salved my worried brow that I was as totally ignorant as I had recently come to believe. ![]() thank god for that! |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
I just tried it on a ZX Spectrum emulator, since I remembered binary numbers in data statements - DATA elements are evaluated there too. Tested on a BBC emulator - DATA is evaluated there too. I was a bit puzzled on how they distinguished strings for expressions, so I did another test in the BBC emulator. It seems data elements aren't evaluated if they're read as strings (on the BBC, not sure if this is the same for other 80's BASIC) e.g. 4 G=12345 5 F$="QWERTY" 7 READ B 10 READ A$ 19 PRINT B 20 PRINT A$ 90 DATA G 100 DATA F$ prints 12345, but prints "F$", not "QWERTY" (update: the BBC Micro user guide says DATA is supposed to have constants) Edited 2020-08-29 20:10 by capsikin |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
The reason why MMBasic allows expressions was simply because it was so easy to implement and there is no speed or compatibility impact if the elements are simple constants. In MMBasic the READ command uses the type of the variable in its argument list to determine if it is reading a string or a number. BTW, this was also discussed in this post. Geoff Geoff Graham - http://geoffg.net |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
Some questions that may not have been fully answered: It will get screwed up and at some point your program will fall over with some error. Given that MMBasic runs on a microcontroller (some with limited memory) it was not worth checking or allowing for this type of rare action. Yes and yes. PEEK and POKE are quite efficient so you would probably get near that speed (ie, about 4µs per command). Geoff Geoff Graham - http://geoffg.net |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
I want to clarify because I haven't seen this explicitly stated, but have seen it kind of implied, and it fits with a test I did. I believe you can't use expressions in data read as a string, is this right? And this only applies to reading numbers, not strings? So if you do: DIM tt AS STRING tt = "I want this string" DATA tt READ s$ s$ will be "tt", and not "I want this string"? |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
confirmed on my desk uM... > list Dim tt As STRING tt = "I want this string" Data tt Read s$ Print s$ > run tt > but then this also... > list Dim tt As string tt = "I want this string" Data ""+tt Read s$ Print s$ > run > ? tt I want this string > |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
Interesting, a bit more complicated than I realised. Possibly not something for programs to depend on. I'm guessing the initial quote or quoted string, in combination with the READ variable being a string, is used to decide to treat the data as a string expression. I tested a similar thing on a BBC micro emulator. 10 DATA "HEY"+"TH" 20 READ A$ Only "HEY" is read. but on the ZX Spectrum, "HEYTH" is read. Then I tried unquoted strings. They work on the BBC, at least the simple one I tried. On the Spectrum you can't have unquoted strings in DATA, it tries to look them up as a variable name. |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5090 |
Variables in data statements.... interesting. That would allow for self modifying code. AI maybe.. I have to let this sink in. Maybe it is not so different from using variables in general...;) PicomiteVGA PETSCII ROBOTS |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
I came to that conclusion also. I was thinking about READing into a set of DATA to perform an implicit eval of some kind based on a decision, but then I thought, why not just encode that eval as I normally would have and have the code less cryptic. No matter how I try, whenever I think I have found a useful application for this, I always manage to talk my way out of it with more conventional approaches. Many ways to skin a <insert item of choice here> For now, I am putting it down as a curiosity - a nice insight into the interior workings (which is never a bad thing). Maybe one day... ![]() |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
It was really only intended to make the programmers life a little easier. Eg: CONST nbr = 23 DATA nbr, nbr * 2, nbr * 4, nbr * 8, ... etc Geoff Geoff Graham - http://geoffg.net |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |