04-26-2018, 03:20 PM
I've been subject to terrible headaches while searching for the best method to toggle a flag, ie to invert it.
In any conventional language that implements booleans, the code would be :
FLAG1 := NOT(FLAG1)
But in RPN, there is no way to do this easily, while it would be very convenient.
I tried the following four methods to toggle Flag1 (or Reg1 for the third one), each of them having pros and cons.
Method 1: sacrifice another flag
Desc: memorize the status of the first flag with a second one.
Pros: stack or Regs unchanged, only 6 steps
Cons: uses another flag
Method 2: sacrifice the stack
Desc: memorize the status of the flag on stack (X)
Pros: preserves other flags
Cons: modifies the stack, 7 steps
Method 3: sacrifice a register (and the stack)
Desc: use R1 to simulate a flag, with simple integers algebra
Pros: also works on HP12c, only 6 steps
Cons: modifies the stack, use a register, not a flag operation, assume to have R1 correctly initialized with 0 or 1
Method 4: sacrifice the labels
Desc: use GTO instructions to branch
Pros: preserves other flags, stack and registers, 5 steps on a 35s
Cons: uses conditional branching, perfect on a 35s but consuming LBLs on a 15c
What do you think?
I'm sure some of you have other methods, or a favorite one.
Thibault
In any conventional language that implements booleans, the code would be :
FLAG1 := NOT(FLAG1)
But in RPN, there is no way to do this easily, while it would be very convenient.
I tried the following four methods to toggle Flag1 (or Reg1 for the third one), each of them having pros and cons.
Method 1: sacrifice another flag
Desc: memorize the status of the first flag with a second one.
Pros: stack or Regs unchanged, only 6 steps
Cons: uses another flag
Code:
CF 2
FS? 1
SF 2
SF 1
FS? 2
CF 1
Method 2: sacrifice the stack
Desc: memorize the status of the flag on stack (X)
Pros: preserves other flags
Cons: modifies the stack, 7 steps
Code:
1
FS? 1
CLX
SF 1
X=0?
CF 1
Rv
Method 3: sacrifice a register (and the stack)
Desc: use R1 to simulate a flag, with simple integers algebra
Pros: also works on HP12c, only 6 steps
Cons: modifies the stack, use a register, not a flag operation, assume to have R1 correctly initialized with 0 or 1
Code:
RCL 1
CHS
1
+
STO 1
Rv
Method 4: sacrifice the labels
Desc: use GTO instructions to branch
Pros: preserves other flags, stack and registers, 5 steps on a 35s
Cons: uses conditional branching, perfect on a 35s but consuming LBLs on a 15c
Code:
; HP 15c style
FS? 1
GTO 0
SF 1
GTO 1
LBL 0
CF 1
LBL 1
; HP 35s style
A002 FS? 1
A003 GTO A006
A004 SF 1
A005 GTO A007
A006 CF 1
A007 ...
What do you think?
I'm sure some of you have other methods, or a favorite one.
Thibault