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
Post Reply 


Messages In This Thread
(Free42) tips & tricks: local flags - Werner - 07-23-2020 10:21 AM



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