3-bit flag

Discussion around the Swiss Micros DM42 calculator.
firai
Posts: 28
Joined: Sun Sep 27, 2020 11:38 am
Location: NYC/HK

3-bit flag

I'm probably getting too carried away with developing the menu system for this new program that I'm working on, but I want one of the sub-menus to show a • after the choice name if the user selected an option before and selected this particular option out of 4 (might be more for another building code). Does anyone have suggestions for smarter ways to go about this than my attempts below?

One of the ways I thought of doing this is with 3 flags:

Code: Select all

64 "d"
65 FC? 01
66 GTO d
67 FS? 02
68 FC? 03
69 GTO d
70 ├"•"
71▸LBL d
72 KEY 4 XEQ 16
...
93▸LBL 16
94 SF 01
95 SF 02
96 SF 03
97 RTN
But the code gets quite long and is marginally readable. Here's a slightly more readable variation with flags but with one more line per option:

Code: Select all

64 "d"
65 FC? 01
66 GTO d
67 FS? 02
68 GTO d
69 FS? 03
70 GTO d
71 ├"•"
72▸LBL d
73 KEY 4 XEQ 16
74 MENU
Using an actual variable to store the value in decimal form would be another option, but it would disrupt the stack unless I add even more code to restore the original values. It also doesn't seem to save as many lines of code as I thought it would, once I add the roll-downs after testing and setting the value.

Code: Select all

64 "d"
65 RCL a
66 4
67 X=Y?
68 ├"•"
69 R↓
70 R↓
71 KEY 4 XEQ 16
...
92▸LBL 16
93 4
94 STO a
95 R↓
96 RTN
Any ideas?

Menu.png (370 Bytes) Viewed 592 times
Sam
whuyse
Posts: 114
Joined: Thu Dec 21, 2017 1:23 pm

Re: 3-bit flag

I hope I have understood what it is you're trying to do..
If there are only 4 choices, and you want to show the dot for only one, why do you need three flags? Two will do?
Another way to avoid stack disruption is the use of ISG and DSE.
Suppose your choices are 0,1,2,3 - designate 0 to be the one that causes the dot to be shown.

Code: Select all

 "d"
DSE a
nop (any one-byte instruction as it is never executed)
ISG a
+"•"
KEY 4 XEQ 16
The value of a is unchanged by the succession of DSE and ISG.

Cheers, Werner
42S #3249S01123
DM42 #00345
DM41X #01215
firai
Posts: 28
Joined: Sun Sep 27, 2020 11:38 am
Location: NYC/HK

Re: 3-bit flag

Thanks for your response. The reason why I used 3 flags was because I wanted to show the dot only if the user has selected an option after starting the program. That is, there's a state "0" for the case where the user hasn't selected anything yet, resulting in a total of 5 possible states when there are 4 choices.

With the DSE+ISG technique, it seems like I can only store 2 possible states with each variable (≤0 and >0). Is my understanding correct? So I would still need to test and set 3 variables to allow 5 possible states, correct? Using this method of course would allow me to use the LSTO statement to declare "local flags", as your previous post mentioned.
Sam
whuyse
Posts: 114
Joined: Thu Dec 21, 2017 1:23 pm

Re: 3-bit flag

-- UPDATE -- : the LBL 10 was misplaced of course, plus some initialisations added

okay, I think I understood now: you can only choose 1 option, A,B,C or D. And when you just entered, none have been chosen.
Choosing another option will erase the previously chosen one.

So, two options
1.with a variable a, initialised to 0

Code: Select all

 0
STO a

LBL 10
RCL a
"A"
DSE ST X
X#0? @ nop
X=0?
+"."
KEY 1 XEQ 01
"B"
DSE ST X
X#0?
X=0?
+"."
KEY 2 XEQ 02
"C"
DSE ST X
X#0?
X=0?
+"."
KEY 3 XEQ 03
"D"
DSE ST X
X#0?
X=0?
+"."
KEY 4 XEQ 04
Rv
STOP
GTO 10

LBL 01
1
STO a
Rv
RTN
LBL 02
2
STO a
Rv
RTN
LBL 03
3
STO a
Rv
RTN
LBL 04
4
STO a
Rv
RTN
2. with 5 (!) flags, all clear at the beginning:

Code: Select all

 CLMENU
XEQ 00
CF 00

LBL 10
"A"
FS? 01
+"."
KEY 1 XEQ 01
"B"
FS? 02
+"."
KEY 2 XEQ 02
"C"
FS? 03
+"."
KEY 3 XEQ 03
"D"
FS? 04
+"."
KEY 4 XEQ 04
STOP
GTO 10

LBL 01
XEQ 00
SF 01
RTN
LBL 02
XEQ 00
SF 02
RTN
LBL 03
XEQ 00
SF 03
RTN
LBL 04
XEQ 00
SF 04
RTN
LBL 00
SF 00
CF 01
CF 02
CF 03
CF 04
RTN
In the latter case, perhaps flag 0 is not needed?
Cheers, Werner
42S #3249S01123
DM42 #00345
DM41X #01215
Dave Britten
Posts: 127
Joined: Wed Jun 14, 2017 9:27 pm

Re: 3-bit flag

I would probably take the lazy way out: use 4 flags. Unless your program has maxed out the number of available flags, this sounds like premature optimization.

Have a LBL you can XEQ that simply clears all 4 flags and returns. Call this at the start of the program, and at the beginning of each of the four KEY GTO routines (followed by setting the flag the user has selected).

Code: Select all

LBL "MYPROG"
XEQ 15
LBL 00
"A"
FS? 01
|-"*"
KEY 1 GTO 01
"B"
FS? 02
|-"*"
KEY 2 GTO 02
...
STOP
GTO 00
...
LBL 01
XEQ 15
SF 01
GTO 00
...
LBL 15
CF 01
CF 02
CF 03
CF 04
RTN

firai
Posts: 28
Joined: Sun Sep 27, 2020 11:38 am
Location: NYC/HK

Re: 3-bit flag

Thanks Werner and Dave! I was indeed trying to compact the storage too much, resulting in more memory required for the extra instructions than the one flag (that I didn't save because the flag exists anyway). Using 4 flags seems like the way to go.

Werner, flag 00 is represented by flag clear for all for the other 4 flags and indeed not necessary. The sequential DSE is quite clever; I'm going to keep that in my library of code snippets. Might come in handy in the future.
Sam