Post Reply 
[Free42] RTN Stack Full
01-14-2024, 10:29 PM
Post: #1
[Free42] RTN Stack Full
I used pl0_hp42s.py to compile this recursive PL/0 program to calculate the factorial of a number \(n\):
Code:
VAR n,f;

PROCEDURE fact;
BEGIN
    IF n > 1 THEN
        BEGIN f := n*f; n := n-1; CALL fact
        END
END ;

BEGIN
    ?n; f := 1; CALL fact; !f
END .

This is the result:
Code:
GTO 00     # main
# Register 01: n
# Register 02: f
# Procedure 01: fact
LBL 01     # fact
1
RCL 01     # n
X≤Y?
GTO 02     # if true
RCL 01     # n
RCL 02     # f
×
STO 02     # f
RCL 01     # n
1
-
STO 01     # n
XEQ 01     # fact
LBL 02     # end if
RTN        # return fact
LBL 00     # main
INPUT 01   # n
1
STO 02     # f
XEQ 01     # fact
RCL 02     # f
PROMPT
END        # main

To my surprise it worked using Free42.

To analyse it further, I reduced the program to:
Code:
00 { 19-Byte Prgm }
01 GTO 00
02▸LBL 01
03 X=0?
04 GTO 02
05 1
06 -
07 XEQ 01
08▸LBL 02
09 RTN
10▸LBL 00
11 XEQ 01
12 PI
13 END

You can run it with any integer number from 0 to 1023 and will get \(\pi\).
However, with 1024 you will get an error message:

RTN Stack Full

In HISTORY I found the following remark:
Quote:
Code:
* Dynamically growing RTN stack. It is not unlimited; in order to prevent
  infinite recursion from eating up all memory, it maxes out at 1024 levels.
  The new stack behaves a bit different than the old version (and the real
  HP-42S): when an XEQ happens while the stack is full, the old version would
  silently discard the oldest RTN, while with the new version, this returns a
  "RTN Stack Full" error.

On a real HP-42S or an HP-41C the program would stop at line 09 when all return addresses are consumed.
Instead of \(\pi\) it would display \(0\).

In case of the fact program from above the following lines are not executed:
Code:
RCL 02     # f
PROMPT
END        # main

Therefore the result is not displayed.

Conclusion

For any meaningful program, recursion doesn't work on a real HP-42S.
However, if the target machine is Free42, it can be used within the limitation of 1024 stack levels.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
[Free42] RTN Stack Full - Thomas Klemm - 01-14-2024 10:29 PM
RE: [Free42] RTN Stack Full - Thomas Okken - 01-14-2024, 11:20 PM
RE: [Free42] RTN Stack Full - Thomas Klemm - 01-15-2024, 12:17 AM
RE: [Free42] RTN Stack Full - Thomas Okken - 01-15-2024, 02:14 AM



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