Post Reply 
(41C) How to edit & debug complex programs?
04-14-2023, 08:00 PM
Post: #9
RE: (41C) How to edit & debug complex programs?
Editor

I write most of the programs for the HP-42S in Visual Studio Code with the HP42S-free42 extension.
To run the program I copy and paste it into the Free42 simulator.
Adapting the program to other models is often an easy task once it runs.

Compiler

I would have recommended the Python to RPN converter but the link doesn't work for me any more.
My Python to FOCAL Compiler only works with Python 2.7 but you might still have that installed somewhere.

This program to calculate the Perimeter of an Ellipse can be translated to Python (say in the ellipse.py file):
Code:
from math import sqrt, pi

def agm2(b):
    s = (1 + b * b) / 2
    a = 1
    t = 1

    while 0 == 0:
        a1 = (a + b) / 2
        if a == a1:
            break
        c = (a - b) / 2
        b = sqrt(a * b)
        a = a1
        s = s - t * c * c
        t = t * 2
    return s / a

# define _PI  3.141592653589793238462643

def ellipse(a, b):
    # perimeter of an ellipse
    # a semi-major axis
    # b semi-minor axis
    # e = eccentricity = sqrt(1-(b/a)^2)
    # p = 4*a*E(e)

    # use AGM variant
    return 2 * pi * a * agm2(b / a)

The compiler.py program must be adjusted a bit:
Code:
import dis
from ellipse import *  # <---

function = {
    # built-in
    'abs' : 'ABS',
    'int' : 'INT',
(…)
    # custom
    'agm2' : 'XEQ "AGM2"',  # <---
(…)
for program in [agm2, ellipse]:  # <---
    translate(program.__code__)
    print
}

The result is far from perfect but can be used as a starting point:
Code:
LBL "AGM2"
STO 00 ; b
RDN
1
RCL 00 ; b
RCL 00 ; b
*
+
2
/
STO 01 ; s
RDN
1
STO 02 ; a
RDN
1
STO 03 ; t
RDN
LBL 00
0
0
X#Y?
GTO 02
RCL 02 ; a
RCL 00 ; b
+
2
/
STO 04 ; a1
RDN
RCL 02 ; a
RCL 04 ; a1
X#Y?
GTO 01
GTO 03 ; break
GTO 01
LBL 01
RCL 02 ; a
RCL 00 ; b
-
2
/
STO 05 ; c
RDN
RCL 02 ; a
RCL 00 ; b
*
SQRT
STO 00 ; b
RDN
RCL 04 ; a1
STO 02 ; a
RDN
RCL 01 ; s
RCL 03 ; t
RCL 05 ; c
*
RCL 05 ; c
*
-
STO 01 ; s
RDN
RCL 03 ; t
2
*
STO 03 ; t
RDN
GTO 00
LBL 02
LBL 03
RCL 01 ; s
RCL 02 ; a
/
RTN

LBL "ELLIPSE"
STO 01 ; b
RDN
STO 00 ; a
RDN
2
PI
*
RCL 00 ; a
*
RCL 01 ; b
RCL 00 ; a
/
XEQ "AGM2"
*
RTN

You may notice a few issues:
  • due to the lack of local variables registers 00 and 01 are overwritten
  • calling AGM2 destroys the stack
  • some nonsensical code snippets

We can easily rewrite ELLIPSE to avoid this:
Code:
00 { 27-Byte Prgm }
01▸LBL "ELLIPSE"
02 X<>Y
03 STO 06
04 ÷
05 XEQ "AGM2"
06 2
07 ×
08 PI
09 ×
10 RCL 06
11 ×
12 END

With these changes the program gives the correct result.

Example

4 ENTER 3
XEQ "ELLIPSE"

22.1034921607

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


Messages In This Thread
RE: (41C) How to edit & debug complex programs? - Thomas Klemm - 04-14-2023 08:00 PM



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