Re: STO consumes stack? Message #2 Posted by Allen on 24 Aug 2008, 5:40 p.m., in response to message #1 by MikeO
The only reason I can think to use a STO in the middle of a program is to save a small snapshot of one of the stack registers for later recall or testing.
I can't speak categorically, but there are over a dozen Stack operations on the Charlemagne Series (not including the SysRPL variants). With RPL's extreme stack agility and a near limitless stack depth, there is a much smaller motivation to store intermediate values during calculation.
You can see a good example of a moderately complex program in both RPL and RPN: in Ulam Spiral Mini Challenge
RPL Version
Note the use of Stack Commands DUP, SWAP. Counters are handled internally by loop commands. Input variable stored with ">" command. Program outputs kept and output to stack.
Bytes: 255
Checksum:#64353d
Variables used: 1 Local, 0 Global
Flags used: 01
<< 1 CF > N ; set up FLAG1 as odd/even increment for Floor/Ceil loop
<< 2 2 ; This skips 1 and 2 as input ( see assumptions)
DO DUP SQRT 1 ; Main loop to determine the corners (7,10,13,17,21...)
IF FS? ; Test for first or second corner between
THEN CIEL 1 CF ; perfect squares.
ELSE FLOOR 1 SF ; Corner numbers are alternate
END ; cv2=cv1+floor(sqrt(cv1)) OR
+ ; cv2=cv1+ ceil(sqrt(cv1))
SWAP 1 + SWAP ; increment corner counter
UNTIL DUP N >= ; Stop if you are at the corner or side
END ; gives side number(s) and corner value
IF DUP N == ; if the input IS the corner number
THEN SWAP 2 * 3 + ; use corner formula
DUP 2 + >LIST ; {2*s+3 2*s+5}
ELSE SWAP 4  ; else use side formula:
{2 2} * {3 11}
ADD ; {32*(s4) 11+2*(s4)}
END
{1 1} + ; All solutions have N+1 and N1
DUP DUP / N * ADD ; Adding N to all list values {1 1 s1 s2)
SWAP DROP SORT ; Clean stack and sort
>>
>>
Unoptimized RPN solution
Note use of STO as counter for variables 01, 02, and to Keep intermediate values of 07 and 08 (program outputs).
bytes: 76
Flags: 01 Used for corners of form x^2+1
Registers: 00 I nput number
01 C orner counter value
02 N ext corner value
07 L ow solution difference
08 H igh solution difference
00 { 76Byte Prgm }
01>LBL 00 ; program label, Program init.
02 CF 01 ; use flag 01 again for alternating loop
03 STO 00 ;
04 2
05 STO 01
06 STO 02 ; initial conditions C=2 and N=2
07>LBL 01 ; Loop to find the next highest corner value
08 1 ; and corner count
09 STO+ 01 ; increment corner value in loop
10 RCL 02
11 SQRT
12 IP
13 STO+ 02 ; add floor(sqrt(N)) to N
14 CLX ; place 0 on stack w/o push
15 FS? 01
16 1
17 STO+ 02 ; make ceil(sqrt(N)) if near x^2
18 FC?C 01
19 SF 01 ; Toggle Flag 01
20 RCL 02
21 RCL 00
22 X>Y? ; Test for the side,
23 GTO 01 ; repeat addition if too low
24 X=Y? ; Test for the corner
25 GTO 02 ; If yes, use corner algorithm
26 RCL 01 ; Otherwise use side algorithm
27 4
28 
29 2
30 *
31 3
32 +
33 +/
34 STO 07 ; L= 32*(C4)
35 +/
36 8
37 +
38 STO 08 ; (new) H=L+8
39 GTO 03 ; Report results
40>LBL 02 ; Corner algorithm
41 RCL 01
42 2
43 *
44 3
45 +
46 STO 07 ;L=3+2*c
47 2
48 +
49 STO 08 ;H=L+2
50>LBL 03 ;Report results
51 RCL 07
52 RCL 00
53 + ;Y=I+L
54 RCL 08
55 RCL 00
56 + ;X=I+H
57 .END.
