![]() |
Forum Index : Microcontroller and PC projects : Immediate IF?
Author | Message | ||||
PhilTilson Newbie ![]() Joined: 15/11/2015 Location: United KingdomPosts: 35 |
Quick question: Is there any type of an Immediate IF in MMbasic? Something of the form: a = IIF(b = 2, 5, 7) I don't think there is, but I thought I'd ask! I had a look through some older posts about 'new features' for MMbasic and some were rejected because they are not really BASIC-like - for example, a+=1. This I accept, but IIF is to be found in VB6, so it could be argued that it's valid! It's so much more compact than: IF b = 2 THEN a = 5 ELSE a = 7 END IF And one other question: given the limited program space in some processors, is there any chance we could have a BYTE data type - ie an 8-bit integer. There are many, many times when we want a FOR loop for just a few values and it seems horribly extravagant to use up a whole eight bytes to count from 1 to 3! Similarly, it seems awkward having to use a one-byte string to hold a flag - ie just 1 or 0 - rather than using an 8-bit integer. Just a couple of thoughts! |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
As I understand it, dealing with different data-types is one of the biggest headaches for an interpreter. There is limited space in the 170 (the most popular MM?) now so I suspect a change/addition for a BYTE variable (although I would love it) is highly unlikely because of this change and the complexity that goes with handling the permutations. Just tonight, I used FLOATs for a job not because I need them but because they are half the size of the QWORD integers. Also, MMBasic is quite mature and a fundamental change to something so "core" that has been stable for ages is not to be attempted lightly. I think that as far as the Mk2's go; it is what it is - and I say that without a hint of a shrug - I am still blown-away by the power of this little chip and the MMBasic environment flashed into it. Bags of space in the newer MMX, Pi and ST based versions, so who knows what may come there (maybe even IIF, although as an ex-pro programmer I think I used it about a half-dozen times in my VB days - I always felt it was a solution looking for a problem). BYTE would offer a speed advantage too. |
||||
PhilTilson Newbie ![]() Joined: 15/11/2015 Location: United KingdomPosts: 35 |
I quite understand your points. And don't think that I don't appreciate the amazing power - and reliability - of MMbasic and the MicroMite+. I am just completing a control system for a full house central heating and hot water system, complete with solar tubes and backup boiler, a dozen temperature sensors, four pumps, three-way valves etc. And all in a box about 18 x 15cm and controllable from my mobile phone from 1200 miles away! Interestingly, it uses an ESP8266 module for internet access. Previously I was programming these using ESPbasic, very clever in its own way, but written and 'maintained' by a somewhat unreliable author. It was rather flaky and forced me, in the end, to get stuck into C++ using the Arduino IDE. I still find many aspects of C++ abstruse in the extreme, but have managed to hack my way through it. Trouble is, once you hit your 70s, it takes a helluva lot longer for stuff to go in than it used to! |
||||
lizby Guru ![]() Joined: 17/05/2016 Location: United StatesPosts: 3378 |
The "in some processors" is relevant. I admit that when I first encountered it in mmbasic, the thought of using 4 (or 8) bytes to hold a value less than 256 (or, horrors, only 1 or 0) seemed fraught. But in practice, I've never run up against a space limit where that matters, and if you do, you can usually step up to a processor with more memory. So if you're the age that I am, as it seems, and can remember back when 8K was a BIG hunk of memory, well, we just need to get over it. Dog/tricks (speaking of myself only). PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
Chopperp![]() Guru ![]() Joined: 03/01/2018 Location: AustraliaPosts: 1097 |
You can use the BITS of a variable as flags. This is what I have done for the CMM. (I've only just realised I could have used more than 8 bits) Flags = 0 '&b00000000 'Flags from earlier versions DO NOT Remove 'b0, &b00000001 = Start_Flag Prog running 'b1, &b00000010 = Dostuff_flag 1 Second timer 'b2, &b00000100 = Rain_Flag Rain Gauge input 'b3, &b00001000 = Day Flag For Data Save 'b4, &b00010000 = Rain_Reset_Flag Rain reset 'b5, &b00100000 = No_SD_Flag SD card removed 'b6, &b01000000 = Data_Flag For Controller Data 'b7, &b10000000 = Switch Press Flag Switch(es) pressed. Here is a code snippet on their use. if Flags and &b00000010 then 'dostuff_flag = 1. Flag set every 1 sec by SETTICK 1 gosub Dostuff 'run every second Flags = Flags and &b11111101 'Reset dostuff Flag endif 'continue DO if Flags and &b00000100 then 'Rain_Flag = 1, b2. Rain has fallen gosub Rain_Update 'flag set by ISR, Rain_Gauge Flags = Flags and &b11111011 'reset Rainflag b2 ready for next lot endif if Flags and &b10000000 then Get_Switch 'Sw_Press_Flag = 1, b7 if Flags and &b01000000 then Com_data_check 'check the Controller data sent To set a flag, say b1, use flags = flags OR &b10 (or 2 in dec) Not sure if you actually save much memory as extra programming is needed to use this method. Brian ChopperP |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Jim VK7JH MMedit |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1642 |
If I recall properly. Someone asked about the possibility of Bytes and 16bit Words for variables and Geoff's answer was an emphatic NO. I'd like them too, a lookup table of a few hundred Bytes or Words seems a waste when using 64 bit integers. Not wishing to teach you how to suck eggs but if you put Jim's function in the library it will be part of MMBasic. Bill Keep safe. Live long and prosper. |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
If it helps, the library does have a set of functions for bit manipulation of a flag array held in a single integer - makes it easy to set a single bit as a flag. I use these all the time http://www.fruitoftheshed.com/MMBasic.Bit-Manipulation-Functions.ashx This compliments ChopperP's method above by making it easier to manipulate the individual bits - certainly makes the code easy to read with stuff like If FlagTest(TempThreshUpper) Then FlagSet WaterOn The point about large memory system (so who cares if you use a few bytes for a flag) is valid, but I come from the old school and it just grates for me to be wasteful... years gone by we traveled to new worlds in software and often it was only possible by frugal and clever use of memory... old habits and all that... |
||||
Chopperp![]() Guru ![]() Joined: 03/01/2018 Location: AustraliaPosts: 1097 |
Thanks for that. Will have to try it out for myself. I think the CMM only uses 32 bits so not quite so memory wasted & it doesn't have the CONST command so will still have to use bit numbers. (I like the way you reset the flags) Brian ChopperP |
||||
lizby Guru ![]() Joined: 17/05/2016 Location: United StatesPosts: 3378 |
Thanks for the link, and right, kudos for the clever RESET code. Yes, it's better for the soul of an old C programmer not to be "wasting" all those bits, however many may be available. And thanks, Jim, for the reminder that you can use the language to extend the language. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
Thanks guys but none deserved... it's an old assembly language trick... think I might even have picked it up from Rodnay Zaks EDIT: Hey wow... look what I found while mooching... http://www.z80.info/zip/zaks_book.pdf |
||||
PhilTilson Newbie ![]() Joined: 15/11/2015 Location: United KingdomPosts: 35 |
Many thanks to all who have commented. Yes Jim, that's a very neat way of achieving what I wanted. I wonder if it's worth writing it as a C function and saving it in the library... As for my other point, as many have surmised, I was in near the start! One of my first forays into programming was writing a paper tape input routine for a DEC PDP8. It had 4K of core memory (we've come full circle as, like the MicroMite, everything was still there when you switched it on again in the morning!) and I entered the machine code through a set of switches on the control panel, one 12-bit word at a time. But yes, it still hurts to have to use 64 bits to save a '1'! |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5091 |
Using 32 bit's to store a boolean seems overkill memory wise. But if you look at it from the execution speed in an interpreter environment, it is not. There is far more time involved in decoding the basic instruction that uses the boolean, that would justify creating a different variable type. Also, writing complex code to combine bits in a variable like: if Flags and &b00000010 then etc... Is perfectly OK, but it slows down execution. And increases your program size way more that the 31 bits saved in memory. I think Geoffs solution is the best. Limit the amount of variable types since the interpreter environment simply runs faster. Volhout PicomiteVGA PETSCII ROBOTS |
||||
CircuitGizmos![]() Guru ![]() Joined: 08/09/2011 Location: United StatesPosts: 1427 |
It doesn't have to be a C function to be saved in the library. BASIC can be saved in the library. Making it a c function may make it smaller/faster, but the execution speed benefit would likely only be noticed in large loops. Micromites and Maximites! - Beginning Maximite |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1642 |
Micromite uses 64-bit integers but I agree with Volhout: You can store a huge number or a single-bit flag in an integer. It doesn't matter as long as the memory is available. Memory size is fixed. Packing Bit and Bytes may save memory but at the cost of speed and complexity. My 8-Bit micro days are long gone and I've given up worrying about a few Bytes here and there. Bill Keep safe. Live long and prosper. |
||||
isochronic Guru ![]() Joined: 21/01/2012 Location: AustraliaPosts: 689 |
I trialled BYTE in my interpreter a while ago, but tripped up. In the primeval f77 gloom BYTE was a synonym for a 8bit integer, but integers are signed by default, leading to a glorious max of 127. Most use of an eight bit integer is for 0-255. So for completion I put in unsigned variables, hardly worth the effort. I'd definitely make it a synonym for an unsigned integer, if it isn't already. |
||||
Chopperp![]() Guru ![]() Joined: 03/01/2018 Location: AustraliaPosts: 1097 |
My CMM is virtually Maxed out as far as memory goes so it is a problem for me. I did initially think that using bits for flags would save memory but from what is said above, it probably would not save any but in fact use more and also slow things down in the process. So I went & changed my flags to an array, DIM Flags(10), which uses 44 bytes & changed the 30 odd flag operations to Flags(0), Flags(1) etc & just used simple testing & setting etc. I did find one or two logical errors in the process. Been running for an hour or so without any problems. Memory went from about 18% SPARE to 20% which is good although it does depend a bit on where in it's operation, the program is stopped as to the value memory readings. Brian ChopperP |
||||
lizby Guru ![]() Joined: 17/05/2016 Location: United StatesPosts: 3378 |
FORTRAN's unsigned 8-bit integer arrays saved my bacon in 1981-2 when I was able to hold a million-character text document in memory and plug in different values to create 50+ different documents based on how Democratic U.S. senators had voted on all issues in careers spanning up to 25 years. It was the most memory-intensive program run on the Senate's IBM 370 computer, and could run only in the middle of the night when nothing else was happening. It took several minutes to build the document in memory, but then only seconds to plug in the "Y", "N", or "A" vote indicators and spit out the 50 individualized reports. Back in the days when computer time was much more expensive than programmer time. Computer administration didn't like the program because FORTRAN was no longer "supported" by IBM, as if the bits in the compiler were going to decay and the program could no longer be reproduced. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
For this specific case you could use: a = 7 + (b=2) * 2 or a = 5 + (b!=2) * -2 It is a bit cryptic but it is fast and small. :) btw. I did not test it so if you get a result of 9 just change the + into a -. A little bit less cryptic but more easy to use with different values [code] a = (b=2) * -5 + (b!=2) * -7 [/code] Then (b=2) part will result in a -1 or 0 depending if it is true or false. Microblocks. Build with logic. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |