Post Reply 
(Free42) tips & tricks: local flags
07-23-2020, 10:21 AM (This post was last modified: 07-23-2020 10:24 AM by Werner.)
Post: #1
(Free42) tips & tricks: local flags
LSTO has given us local variables, but we do not have local flags.
We can use a local variable (say, "F") to store a flag, and at first glance, 0 for a clear flag and 1 for a set flag would seem logical. However, the values -1e99 and 1e99 (the exponents are not important as long as they are larger than 34) are more convenient, as then the testing of the 'flag' can be performed with DSE and ISG, which will not change the stored value, but will skip accordingly:

Flag Clear? -> ISG "F"
Flag Set? -> DSE "F"

to Set the flag (without disturbing the stack):

LSTO "F"
CLX
1e99
X<> "F"

to Clear the flag:

LSTO "F"
CLX
-1e99
X<> "F"

Bear in mind that you can turn these into subroutines, but you can't include the LSTO statement in the subroutine..

(actually, even -1e9 and 1e9 will do for the flag values.. you're not likely to test the flag a billion times.. ;-)

In case you need multiple flags, you may resort to saving the state of flags 0-7, using them for your own purpose, and restoring them. For that, X<>F (as in the '41 X-Functions) may be used:

Code:
00 { 40-Byte Prgm }
01▸LBL "X<>F"
02 256
03 ÷
04 -7
05 X<>Y
06 IP
07▸LBL 02
08 STO+ ST X
09 FS?C IND ST Y
10 ISG ST X
11 LBL 00
12 LASTX
13 STO+ ST X
14 IP
15 STO- ST L
16 X>0?
17 SF IND ST Z
18 R↓
19 ISG ST Y
20 GTO 02
21 END

Hope you like it,
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
07-24-2020, 05:18 AM
Post: #2
RE: (Free42) tips & tricks: local flags
(07-23-2020 10:21 AM)Werner Wrote:  In case you need multiple flags, you may resort to saving the state of flags 0-7, using them for your own purpose, and restoring them. For that, X<>F (as in the '41 X-Functions) may be used:

I'm adding X<>F in the next major release.

Maybe something like RCLFLAG and STOFLAG would be useful, too? The X-FCN version recalls and sets flags 0-43, that would have to be expanded to include at least flags 81-99, so the result would not fit in a six-character string...
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2020, 05:44 AM
Post: #3
RE: (Free42) tips & tricks: local flags
You need even more flags: 55-60, 66, 68-71, 72, 73, 74, well...
If you use a number instead of a string, all 100 flags will fit. 2^100 is only about 1e30.

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
07-24-2020, 05:50 AM
Post: #4
RE: (Free42) tips & tricks: local flags
In the binary version it wouldn't fit in a number.

I could use a two-element vector, but then you wouldn't be able to store it in a numbered register... but if you're going to store it using LSTO anyway, that wouldn't be a problem, I suppose.
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2020, 06:28 AM
Post: #5
RE: (Free42) tips & tricks: local flags
I keep forgetting about Free42 Binary, while I'm probably its biggest fan!
Now, I would say RCL/STOFLAGS is used more for mode-preserving, so perhaps it could leave out the User flags?
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
07-24-2020, 06:42 AM
Post: #6
RE: (Free42) tips & tricks: local flags
A pair of registers if numbered (n & n+1), a complex number if not? Yeah, a bit of a hack.

FWIW: 0-43, 81-99 would just fit into a numbered register but not remotely nicely.


Pauli
Find all posts by this user
Quote this message in a reply
07-24-2020, 07:10 AM
Post: #7
RE: (Free42) tips & tricks: local flags
(07-24-2020 06:28 AM)Werner Wrote:  I keep forgetting about Free42 Binary, while I'm probably its biggest fan!

Glad to read that, because the binary version is great!

When we refer to Free42, we very often think of the 34-digit accuracy of the BCD version, but the binary version (Windows platform) is so FAST! I'm using it a lot.

So, Thomas, don't ever give up the binary version. Actually I'm missing it on the Android platform, but I'm not an Android fan and I'm still enjoying the old Pocket PC versions.

J-F
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2020, 08:55 AM (This post was last modified: 07-24-2020 09:20 AM by Thomas Okken.)
Post: #8
RE: (Free42) tips & tricks: local flags
There are a few flags that really never need to be preserved because they are just status indicators, like 45 and 46 (solving, integrating), 49 (low battery), 53 (INPUT), and a few others as well. The remaining 96 or fewer flags that should be able to be saved and restored could then be split into two chunks of 48, which could be stored as a two-element vector containing two strings, or by using the X and Y registers simultaneously, or perhaps by having separate instructions for preserving the user flags and everything else, though then the 48/48 split wouldn't work. Hmm...

EDIT: 48 flags could also be stored in the mantissa of a binary floating-point number, so then the whole set could fit into a complex number, which would be LSTO-friendly and easy to split up for storage in numbered registers as well.

EDIT 2: Oh right, that's what Pauli just suggested.

(07-24-2020 07:10 AM)J-F Garnier Wrote:  So, Thomas, don't ever give up the binary version. Actually I'm missing it on the Android platform, but I'm not an Android fan and I'm still enjoying the old Pocket PC versions.

I have felt tempted to drop the binary version at times, but since building it requires next to no extra effort, I ended up keeping it anyway. But building it for Android and iOS, in a way that it can coexist with the decimal version or at least allow easy switching between the two, is a bit more difficult and I could never be bothered to sort that out. There doesn't appear to be much interest in it so I prefer to keep things simple there. Smile
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2020, 09:36 AM
Post: #9
RE: (Free42) tips & tricks: local flags
I count 47 'system mode' flags, but I think flag 75 can be left off, so we have 46:

11 auto execution
12 print double wide
13 print lower case
15-16 printer: 0)manual 1)normal 2)trace 3)trace w/stack print using the flags as a two-digit binary
21 printer enabled
22 numeric input available
23 alpha input available
24 ignore range errors
25 ignore any errors & clear
26 BEEP enabled
27 custom mode is active
28 radix mark: Clear is a comma, set is a period.
29 digit groupings shown: Clear - no Set - yes
31 YMD setting with F67
34-35 AGRAPH merging: 0) OR 1) duplicate "on" get turned off 2) overwrite 3) XOR
36-39 number of digits, 0-15 by using the four flags as a binary number.
40-41 display format: 0)sci 1)eng 2)fix 3)all by using the two flags as a two-digit binary
42-43 angle mode: 0)deg 1)rad 2)grad 3)rad by using the two flags as a two-digit binary
44 continuous on
55 printer exists
56 curve fit model: 1)linear 2)log 4)exponential 8)power
60 all \GS mode
65 matrix editor in use
66 auto grow matrices
67 YMD setting with F31
68 base: 0)decimal 1)binary 7)octal 15)hexadecimal
72 local label
73 polar mode
74 real result only
75 programmable menu selected
76 matrix wrap, edge to edge
77 matrix wrap, first to last
78 BSIGNED
79 BWRAP

And even so, flags 56-59 and 68-71 can be 'condensed' into 2 flags each.

Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
07-24-2020, 09:39 AM
Post: #10
RE: (Free42) tips & tricks: local flags
BTW Free42 doesn't do a 'trace with stack print' when both flag 15 and 16 are set - I have no idea (and no means to find out) if a 42S does that or not. The flags doc mentions this is a 'HP-IL' printer behaviour, which of course doesn't apply to a 42S.
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
07-24-2020, 10:19 AM
Post: #11
RE: (Free42) tips & tricks: local flags
(07-24-2020 08:55 AM)Thomas Okken Wrote:  ... or perhaps by having separate instructions for preserving the user flags and everything else, though then the 48/48 split wouldn't work.

A 64 bit binary float can safely hold 62 flags:
  • the mantissa holds 52 (the 53rd bit doesn't exist since it's always 1);
  • the sign bit holds 1 and
  • the exponent holds 9 (despite being 11 bits long, it shouldn't be all 0 or all 1 to avoid NaNs, infinities and subnormals).

It would probably be more user friendly to restrict it to the 52 mantissa bits (and possibly the sign). User friendly in the sense that changes could easily be made to the value.

All 64 bits could be used at a pinch if no operations were ever done. Perhaps using a different type field for this? I expect this would be a huge pain.


Pauli
Find all posts by this user
Quote this message in a reply
07-24-2020, 11:06 AM
Post: #12
RE: (Free42) tips & tricks: local flags
All 64 bits could be used; Free42 will display <Infinity>, <-Infinity>, or <Not a Number> when appropriate. Restricting the encoding to the 48 bits of a 6-character string or the 52 bits of a floating-point mantissa would just look a bit tidier.

Good question about the trace-with-stack mode. I don't have an 82240 but I could test this with Emu42. And even if the real 42S does not implement this behavior, there's no reason Free42 couldn't.
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2020, 11:14 AM
Post: #13
RE: (Free42) tips & tricks: local flags
(07-24-2020 10:19 AM)Paul Dale Wrote:  A 64 bit binary float can safely hold 62 flags:
  • the mantissa holds 52 (the 53rd bit doesn't exist since it's always 1);
  • the sign bit holds 1 and
  • the exponent holds 9 (despite being 11 bits long, it shouldn't be all 0 or all 1 to avoid NaNs, infinities and subnormals).

Avoiding Inf/NaN and subnormal/zero, exponent range = 2^11 - 2, thus able to hold 10 flags.

But, if we use binary float to hold flags, there is no reason to avoid them.
Find all posts by this user
Quote this message in a reply
07-24-2020, 11:28 AM
Post: #14
RE: (Free42) tips & tricks: local flags
(07-24-2020 11:14 AM)Albert Chan Wrote:  Avoiding Inf/NaN and subnormal/zero, exponent range = 2^11 - 2, thus able to hold 10 flags.

Good point.


Pauli
Find all posts by this user
Quote this message in a reply
07-24-2020, 11:35 AM
Post: #15
RE: (Free42) tips & tricks: local flags
(07-24-2020 11:14 AM)Albert Chan Wrote:  But, if we use binary float to hold flags, there is no reason to avoid them.

My concerns are about the possibility of the value changing behind the scenes and signalling NaNs. These shouldn't be a problem but a compiler being clever or a poor implementation might be.

They'd also make for a surprising display for the user.


Pauli
Find all posts by this user
Quote this message in a reply
07-24-2020, 12:53 PM
Post: #16
RE: (Free42) tips & tricks: local flags
(07-24-2020 11:06 AM)Thomas Okken Wrote:  Good question about the trace-with-stack mode. I don't have an 82240 but I could test this with Emu42. And even if the real 42S does not implement this behavior, there's no reason Free42 couldn't.

This is likely mentioned for compatibility w/41 programs which used this mode to print to the 82162A IL Printer, to act the same when printing to the 82240A/B IR Printer.

This mode was intended as a debugging aid, when there were few other choices, but it uses up paper at prodigious rates and ultimately not used often, though a few folks in the PPC Journal insisted it was "the only way to properly debug a program", apparently forgetting about paper and pencil...

I can check this later on DM42 if curious...

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
07-24-2020, 01:55 PM
Post: #17
RE: (Free42) tips & tricks: local flags
It's a lot more useful with a virtual paper trail for sure than burning up thermal paper (pun intended).
Find all posts by this user
Quote this message in a reply
07-24-2020, 03:34 PM
Post: #18
RE: (Free42) tips & tricks: local flags
It looks like the real HP-42S does not have this mode. I checked with Emu42 and the HP82240B Printer Simulator.

It sounds like implementing this in Free42 would require changing most or all occurrences of

Code:
if flag 15 set and flag 55 set
    PRX

to

Code:
if flag 15 set and flag 55 set
    if flag 16 set
        PRSTK
    else
        PRX

Is that what the real 41C with IL printer does? I have a 41CX but no printer so I can't check that either, and i41CX on my phone only emulates the 82143A where the printing mode is controlled by a hardware switch and flags 15 & 16 have no effect...

It would make sense for it to print the stack when STO ST operations are performed. Does the real thing do that?
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2020, 08:26 PM
Post: #19
RE: (Free42) tips & tricks: local flags
(07-24-2020 08:55 AM)Thomas Okken Wrote:  There doesn't appear to be much interest in it so I prefer to keep things simple there. Smile

Oh no! I'd be definetely interested, even if it only was to keep you busy?

Günter
Find all posts by this user
Quote this message in a reply
07-25-2020, 12:08 AM
Post: #20
RE: (Free42) tips & tricks: local flags
(07-24-2020 03:34 PM)Thomas Okken Wrote:  
Code:
if flag 15 set and flag 55 set
    if flag 16 set
        PRSTK
    else
        PRX

Sounds like it should be a function to me Smile


Pauli
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)