HP Forums
(41C) How to edit & debug complex programs? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: (41C) How to edit & debug complex programs? (/thread-19806.html)



(41C) How to edit & debug complex programs? - acser - 04-14-2023 02:44 AM

I am getting started with HP 41C FOCAL programming. What really is limiting
for me that even with BST and STP in PRGM mode it’s really hard to look through the code.

I totally get the minimalistic environment by today’s standards, but was wondering
if people can share some best practices on how to develop, debug, and enter modified
FOCAL code into V41 on Windows or i41CX+ on iOS.

So far the method that worked for me is this:
1) enter the code into Notepad++ or a text editor on Windows where I have line
numbers.
2) manually enter or copy/paste the code into the emulator
3) run and test the program in the emulator
4) make changes to the code in the text editor on Windows
5) make the same change to the code in the emulator. Go to 3)
6) Save the working program into a raw file and archive.

Does anyone have a better/faster process/workflow?
Thanks in advance.,
A


RE: (41C) How to edit & debug complex programs? - floppy - 04-14-2023 07:23 AM

As reactivated beginner, I work similarly. See my comments.
I automated a bit with start icons on the PC Desktop for starting V41 (Emulator) / ILPER / PilBoxBridge.

1) enter the code into Notepad++ or a text editor on Windows where I have line
numbers.
-> I make a drawing on paper what I want to program (with a pen), then when it is mature, go to the PC
-> on a Debian (linux) PC 64bits editor "gedit", no line number, documented file where the stack X Y Z Register are described. See TXT attachment as example
-> I start the command in a prompt window, which will create a raw file > wine "/home/mimimi/.wine/drive_c/Program Files (x86)/HP-Emulators/HP41UC/hp41uc.exe" /t=ELPER.TXT /r /k
-> I click an icon on my PC which will launch the programs V41 and ILPER. A virtual drive (on an USB drive) HDRIVE1.DAT is connected to ILPER

2) manually enter or copy/paste the code into the emulator
-> Upload the previous created raw file into the launched V41 emulator

3) run and test the program in the emulator

4) make changes to the code in the text editor on Windows
-> make small changes direct in V41
-> reflect the changes in "gedit"
-> test the program again on V41 (create raw, upload raw..)

5) make the same change to the code in the emulator. Go to 3)
-> see 4

6) Save the working program into a raw file and archive.
-> Save the working program in V41 into the virtual drive on USB drive

-> 7) start another icon on my PC where the programs "PIlBox-Bridge" and "ILPER" are started. When I connect a real HP41 to an HP-IL to a PILBOX to the PC, then I can retrieve the programs from the virtual drive (on USB) into the HP41 machine (then transfer to HEPAX or XMemory if requested).

-> 8) same above can be done on a raspberry PI4 (ARM with raspbian Linux) with touchscreen.


RE: (41C) How to edit & debug complex programs? - Martin Hepperle - 04-14-2023 07:43 AM

When I was writing many larger programs in the 1980s, the plug-in printer was one of the most important accessories. In fact, I first bought the 82143 printer before I bought (and could afford) an HP-IL mass storage device.

Printing out the program listings gave me what Notepad++ gives you today: a global view of the code. And the option to draw flowchart lines into the listing, scribble annotations and so on.
When the program was finished I photocopied the printout for archiving.

Additionally, a list of registers and their assignment to variables was essential.
The old HP-41C User's Library programming forms have been designed specifically for this purpose.

Compared to today's programming style, which is much more "hack, execute and see which error the system throws", I spent more time in writing down algorithms on paper, drawing flow charts, checking the syntax etc. before entering the first program lines into the computer. The same was true for mainframe computers, because the interactive (or card punch) time was very limited, so preparation before entering programs was essential.

For a modern variant of such a documentation see the attached Word document.


RE: (41C) How to edit & debug complex programs? - Massimo Gnerucci - 04-14-2023 08:55 AM

(04-14-2023 07:43 AM)Martin Hepperle Wrote:  Compared to today's programming style, which is much more "hack, execute and see which error the system throws", I spent more time in writing down algorithms on paper, drawing flow charts, checking the syntax etc. before entering the first program lines into the computer. The same was true for mainframe computers, because the interactive (or card punch) time was very limited, so preparation before entering programs was essential.

Holy words!


RE: (41C) How to edit & debug complex programs? - KeithB - 04-14-2023 12:40 PM

(04-14-2023 08:55 AM)Massimo Gnerucci Wrote:  
(04-14-2023 07:43 AM)Martin Hepperle Wrote:  Compared to today's programming style, which is much more "hack, execute and see which error the system throws", I spent more time in writing down algorithms on paper, drawing flow charts, checking the syntax etc. before entering the first program lines into the computer. The same was true for mainframe computers, because the interactive (or card punch) time was very limited, so preparation before entering programs was essential.

Holy words!

Indeed, I have joked that my current programming style is "step-wise refinement by error message". 8^)


RE: (41C) How to edit & debug complex programs? - acser - 04-14-2023 07:48 PM

(04-14-2023 12:40 PM)KeithB Wrote:  
(04-14-2023 08:55 AM)Massimo Gnerucci Wrote:  Holy words!

Indeed, I have joked that my current programming style is "step-wise refinement by error message". 8^)


So true. The 'think first, code next' mentality seems to be largely gone.


RE: (41C) How to edit & debug complex programs? - acser - 04-14-2023 07:49 PM

(04-14-2023 07:43 AM)Martin Hepperle Wrote:  When I was writing many larger programs in the 1980s, the plug-in printer was one of the most important accessories. In fact, I first bought the 82143 printer before I bought (and could afford) an HP-IL mass storage device.

Printing out the program listings gave me what Notepad++ gives you today: a global view of the code. And the option to draw flowchart lines into the listing, scribble annotations and so on.
When the program was finished I photocopied the printout for archiving.

Additionally, a list of registers and their assignment to variables was essential.
The old HP-41C User's Library programming forms have been designed specifically for this purpose.

Compared to today's programming style, which is much more "hack, execute and see which error the system throws", I spent more time in writing down algorithms on paper, drawing flow charts, checking the syntax etc. before entering the first program lines into the computer. The same was true for mainframe computers, because the interactive (or card punch) time was very limited, so preparation before entering programs was essential.

For a modern variant of such a documentation see the attached Word document.

Thank you!


RE: (41C) How to edit & debug complex programs? - acser - 04-14-2023 07:50 PM

(04-14-2023 07:23 AM)floppy Wrote:  As reactivated beginner, I work similarly. See my comments.
I automated a bit with start icons on the PC Desktop for starting V41 (Emulator) / ILPER / PilBoxBridge.

1) enter the code into Notepad++ or a text editor on Windows where I have line
numbers.
-> I make a drawing on paper what I want to program (with a pen), then when it is mature, go to the PC
-> on a Debian (linux) PC 64bits editor "gedit", no line number, documented file where the stack X Y Z Register are described. See TXT attachment as example
-> I start the command in a prompt window, which will create a raw file > wine "/home/mimimi/.wine/drive_c/Program Files (x86)/HP-Emulators/HP41UC/hp41uc.exe" /t=ELPER.TXT /r /k
-> I click an icon on my PC which will launch the programs V41 and ILPER. A virtual drive (on an USB drive) HDRIVE1.DAT is connected to ILPER

2) manually enter or copy/paste the code into the emulator
-> Upload the previous created raw file into the launched V41 emulator

3) run and test the program in the emulator

4) make changes to the code in the text editor on Windows
-> make small changes direct in V41
-> reflect the changes in "gedit"
-> test the program again on V41 (create raw, upload raw..)

5) make the same change to the code in the emulator. Go to 3)
-> see 4

6) Save the working program into a raw file and archive.
-> Save the working program in V41 into the virtual drive on USB drive

-> 7) start another icon on my PC where the programs "PIlBox-Bridge" and "ILPER" are started. When I connect a real HP41 to an HP-IL to a PILBOX to the PC, then I can retrieve the programs from the virtual drive (on USB) into the HP41 machine (then transfer to HEPAX or XMemory if requested).

-> 8) same above can be done on a raspberry PI4 (ARM with raspbian Linux) with touchscreen.

Thank you.


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

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



RE: (41C) How to edit & debug complex programs? - Craig Bladow - 04-15-2023 03:17 AM

(04-14-2023 02:44 AM)acser Wrote:  I am getting started with HP 41C FOCAL programming. What really is limiting
for me that even with BST and STP in PRGM mode it’s really hard to look through the code.
...
Does anyone have a better/faster process/workflow?
...
This is one of the reasons I created CC41. Programs can be edited in your favorite text editor. You can put multiple commands on one one line and use indentation to provided some visual indication of sub-structure to the program. TRACE turns on a trace mode when running a program and the stack contents are printed out after each step. The next release will have the ability to 'watch' the contents of registers as a program is run, or single stepped through.

Cheers,
Craig


RE: (41C) How to edit & debug complex programs? - floppy - 04-15-2023 06:54 PM

(04-15-2023 03:17 AM)Craig Bladow Wrote:  
(04-14-2023 02:44 AM)acser Wrote:  I am getting started with HP 41C FOCAL programming. What really is limiting
for me that even with BST and STP in PRGM mode it’s really hard to look through the code.
...
Does anyone have a better/faster process/workflow?
...
This is one of the reasons I created CC41. Programs can be edited in your favorite text editor. You can put multiple commands on one one line and use indentation to provided some visual indication of sub-structure to the program. TRACE turns on a trace mode when running a program and the stack contents are printed out after each step. The next release will have the ability to 'watch' the contents of registers as a program is run, or single stepped through.

Cheers,
Craig
+1
Craig, can you post a video on youtube how it works?
On my ToDo list (100+ projects) I had a plugin for HP41 (Notepad, or others) but your work sound interesting.
Question: is it for programming direct in, then debugging, then creating raw files for transfer to virtual drives via V41?


RE: (41C) How to edit & debug complex programs? - acser - 04-15-2023 10:51 PM

(04-15-2023 03:17 AM)Craig Bladow Wrote:  
(04-14-2023 02:44 AM)acser Wrote:  I am getting started with HP 41C FOCAL programming. What really is limiting
for me that even with BST and STP in PRGM mode it’s really hard to look through the code.
...
Does anyone have a better/faster process/workflow?
...
This is one of the reasons I created CC41. Programs can be edited in your favorite text editor. You can put multiple commands on one one line and use indentation to provided some visual indication of sub-structure to the program. TRACE turns on a trace mode when running a program and the stack contents are printed out after each step. The next release will have the ability to 'watch' the contents of registers as a program is run, or single stepped through.

Cheers,
Craig

Just tried this, it works nicely, thank you. A difference from the HP41 is that PSE commands do not allow for X stack modification. Also, it would be nice to accept the sequence "!=" as the "<>" (not equal) sequence Smile (+41CX on iOS generates !=) Thank you. Smile


RE: (41C) How to edit & debug complex programs? - Craig Bladow - 04-16-2023 01:20 AM

(04-15-2023 06:54 PM)floppy Wrote:  +1
Craig, can you post a video on youtube how it works?
On my ToDo list (100+ projects) I had a plugin for HP41 (Notepad, or others) but your work sound interesting.
Question: is it for programming direct in, then debugging, then creating raw files for transfer to virtual drives via V41?

Thanks! I presented on CC41 at HHC 2021: https://youtu.be/CUvFmkcpVys

CC41 loads programs written in your favorite text editor. It converts those files into its own internal program format. CC41 does not save files in RAW format as there are other utilities that convert text files to RAW format.

Cheers


RE: (41C) How to edit & debug complex programs? - Craig Bladow - 04-16-2023 01:25 AM

(04-15-2023 10:51 PM)acser Wrote:  Just tried this, it works nicely, thank you. A difference from the HP41 is that PSE commands do not allow for X stack modification. Also, it would be nice to accept the sequence "!=" as the "<>" (not equal) sequence Smile (+41CX on iOS generates !=) Thank you. Smile

It will be in the next release as x!=y? as an alias for x<>y? and x!=0? for x<>0? Smile

I'll document the PSE behavior difference in the manual and put it on my to do list to see what can be done in the future.

Cheers


RE: (41C) How to edit & debug complex programs? - floppy - 04-16-2023 08:13 PM

(04-16-2023 01:20 AM)Craig Bladow Wrote:  Thanks! I presented on CC41 at HHC 2021: https://youtu.be/CUvFmkcpVys

CC41 loads programs written in your favorite text editor. It converts those files into its own internal program format. CC41 does not save files in RAW format as there are other utilities that convert text files to RAW format.

Cheers

Good. Thats a presentation.
Perhaps a short debugging / live programming event video now?
a) take a camera over your shoulder (or a screen capture)
b) explain your task with paper/pen (or write it on a whiteboard on the PC)
c) write RPN code
d) show an interactive use
.. like a salesman for programming IDE tools would do ;-)


RE: (41C) How to edit & debug complex programs? - acser - 04-18-2023 02:55 AM

(04-16-2023 01:25 AM)Craig Bladow Wrote:  
(04-15-2023 10:51 PM)acser Wrote:  Just tried this, it works nicely, thank you. A difference from the HP41 is that PSE commands do not allow for X stack modification. Also, it would be nice to accept the sequence "!=" as the "<>" (not equal) sequence Smile (+41CX on iOS generates !=) Thank you. Smile

It will be in the next release as x!=y? as an alias for x<>y? and x!=0? for x<>0? Smile

I'll document the PSE behavior difference in the manual and put it on my to do list to see what can be done in the future.

Cheers

Excellent, thank you. Appreciate your openness Smile


RE: (41C) How to edit & debug complex programs? - Bill Duncan - 04-18-2023 06:35 AM

I guess I'm old school. I just rough it out in a file with vim (on linux or macos), then punch it into the machine..

Some things I have to fudge, like the summation key.

eg.
https://www.hpmuseum.org/software/41/41tmgr15.htm


RE: (41C) How to edit & debug complex programs? - floppy - 04-18-2023 08:48 AM

HP41 IDE found here (no experience with this) http://www.flaxcottage.com/Calculators/hp41csoftware.asp


RE: (41C) How to edit & debug complex programs? - Craig Bladow - 05-06-2023 04:44 PM

(04-18-2023 02:55 AM)acser Wrote:  
(04-16-2023 01:25 AM)Craig Bladow Wrote:  It will be in the next release as x!=y? as an alias for x<>y? and x!=0? for x<>0? Smile

I'll document the PSE behavior difference in the manual and put it on my to do list to see what can be done in the future.

Cheers

Excellent, thank you. Appreciate your openness Smile

The above actions are incorporated in today's release of CC41.