HP 41 FORTH, final thoughts Message #7 Posted by Egan Ford on 11 Sept 2008, 1:45 a.m., in response to message #6 by Egan Ford
I finally had some time late tonight to explore the FORTH41 ROM a bit more. First of all I must state that I'm impressed with the effort. Kudos to Serge Vaudenay for this achievement.
What is FORTH41?
Information about this module is very sparse, the only proper documentation was written about 6 months ago and is difficult to obtain. This is what I was able to find:
 "FORTH FOR THE HP41", Datafile V3N45 Page 29. This article was published in 1984. I've posted a scan of this page here: http://sense.net/~egan/forth41/forth41_v3n45_p29.pdf.
 Emmanuel Compes (the author of the aforementioned FORTH41 manual) has a bit more information here: http://www.emmanuel.hp41.eu/database/forth41.html.
 According to sources in the previous two points, FORTH41 is based on the Forth79 standard. You can obtain a copy of the standard here: http://www.dartmouth.edu/~lmaurer/forth/Forth79.pdf.
Observations
 While FORTH41 is based on Forth79, it is not complete, however there are substitutes for what is missing. E.g. no 2/ or 2* for right and left shifts, no problem, just use 2 / and 2 *.
 Performance is poor. I created two similar programs (one Forth, one RPN); given X, get the next prime number. I chose this because it is purely integer work (Forth does not have native FP support), and I had an ML version to compare with as well. NOTE: Neither program is well optimized, the purpose was to have the same method running under two different environments.
 Programming Forth on the 41 is not as cumbersome as I thought it would have been.
Benchmark Results
To benchmark I set X to 1000 and generated the next 5 primes. The ML program was hands down the fastest. The RPN version was about 10x faster than the Forth version. BTW, Forth on the 71B is measurably faster than BASIC (about 3x).
Summary
Despite lesser performance and an incomplete implementation, it was still the most fun I've had with my 41 in a while.
If you are interested in learning Forth, the book, "Starting FORTH", is freely available from here: http://home.iae.nl/users/mhx/sf.html.
NOTE: All of this testing was done with i41CXpthe iPhone 41C emulator.
NOTE: I have V41 working now. FORTH41 does not play well with the CX (uses pages 4 and 5). I've been lucky with EMU41 and i41CXp. For V41 just use the CV ROM image with FORTH41.
Forth Code
DECIMAL
VARIABLE R
VARIABLE M
VARIABLE S
: ISQRT
M ! 0 R ! 16384 DUP S !
BEGIN 0> WHILE
M @ S @ R @ OR < IF
R @ 2 / R !
ELSE
S @ R @ OR NEGATE M +!
R @ 2 / S @ OR R !
THEN
S @ 4 / DUP S !
REPEAT
R @
;
: NP
1 + DUP
2 MOD 0 = IF 1 + THEN
BEGIN
DUP ISQRT 1 + 3 DO
DUP I MOD 0 = IF
1 LEAVE THEN
2 +LOOP
DUP 1 = IF
DROP 2 + 0
ELSE DUP THEN
UNTIL
;
: BM
1000 NP NP NP NP NP
;
NOTE: If you think this looks a bit like RPL, it does. RPL was heavily influenced by Forth. Replace ! with STO and @ with RCL, quote your variables, and you are almost there.
RPN Code
01 LBL "NP"
02 1
03 +
04 STO 00
05 2
06 MOD
07 X#0?
08 GTO 01
09 1
10 ST+ 00
11 LBL 01
12 RCL 00
13 SQRT
14 INT
15 1
16 +
17 STO 01
18 3
19 STO 02
20 LBL 02
21 RCL 00
22 RCL 02
23 MOD
24 X=0?
25 GTO 03
26 2
27 ST+ 02
28 RCL 01
29 RCL 02
30 X<=Y?
31 GTO 02
32 RCL 00
33 RTN
34 LBL 03
35 2
36 ST+ 00
37 GTO 01
38 LBL "BM"
39 1000
40 XEQ "NP"
41 XEQ "NP"
42 XEQ "NP"
43 XEQ "NP"
44 XEQ "NP"
45 END
Edited: 11 Sept 2008, 12:02 p.m.
