Post Reply 
My 1st HP-41 program. Suggestions?
10-19-2023, 12:49 AM
Post: #1
My 1st HP-41 program. Suggestions?
After having spent some time typing in simple programs suggested from "HP-41C
STANDARD APPLICATIONS" as suggested by Sylvain I decided to write a guess the number program as I have done this on almost every new system I have programmed since the mid 1980s.

The random number generator is from the Games book. I did add a seed function that uses TIME. The method of displaying prompts was copied from the Guess the word program in the Standard Apps manual. I'm curious how others might optimize this program.

I just implemented one small change as noted in the listing below. The RTN and END were removed from SEED so it drops through to RNDM. This also allows removing one line from GNUM as well.

I tried to keep the random number in Y by using strategically placed RDROPs. This works but seems messy. It is also not ideal on the DM41X as you can see the random number. I guess the number could be stored in R02 and an indirect Y used instead.

Code:

// To seed the RNG
LBL "SEED"
TIME
FRC
STO 00
RTN             ; Can leave out to drop through to RNDM
                   ; Also remove END                                      

// Method #1
LBL "RNDM"
RCL 00
9821
*
.211327
+
FRC
STO 00
RTN

Code:

//Guess the number game
LBL "GNUM"
0               ; Init # of guesses
STO 01          ; REG01 stores # of guesses
XEQ "SEED"      ; Seed RNDM then XEQ RNDM
//XEQ RNDM        ; Random # < 1, can leave out w/modified SEED
10
*
1
+               ; convert to value 1-10
INT
LBL 01
RCL 01
1
+
STO 01          ; Increment the guess counter
RDROP           ; Keep Rand # in Y
"Guess 1-10"
PROMPT          ; Type in # then press R/S 
X=Y?
GTO 03          ; Correct guess handler
X>Y?
GTO 02          ; X>Y handler
"TOO SMALL"     ; **X<Y handler
AVIEW
PSE 
PSE 
RDROP           ; Keep Rand # in Y
GTO 01
LBL 02          ; **X>Y handler
"TOO BIG"
AVIEW
PSE 
PSE 
RDROP           ; Keep Rand # in Y
GTO 01
LBL 03          ; **Correct handler
RCL 01
ARCL X
"|- GUESSES"
AVIEW 
RTN
Find all posts by this user
Quote this message in a reply
10-19-2023, 01:27 AM
Post: #2
RE: My 1st HP-41 program. Suggestions?
What is RDROP?

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
10-19-2023, 01:32 AM
Post: #3
RE: My 1st HP-41 program. Suggestions?
(10-19-2023 01:27 AM)Joe Horn Wrote:  What is RDROP?

Roll down.

V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
10-19-2023, 03:53 AM
Post: #4
RE: My 1st HP-41 program. Suggestions?
(10-19-2023 12:49 AM)Jeff_Birt Wrote:  My 1st HP-41 program. Suggestions?

There are a number of optimizations which could be made but for a first program it's quite acceptable. The largest optimization would be this:

The 15 lines in your original code highlighted in bold red below:
    [...]
    "Guess 1-10"
    PROMPT ; Type in # then press R/S
    X=Y?
    GTO 03 ; Correct guess handler
    X>Y?
    GTO 02 ; X>Y handler
    "TOO SMALL" ; **X<Y handler
    AVIEW
    PSE
    PSE
    RDROP ; Keep Rand # in Y
    GTO 01
    LBL 02 ; **X>Y handler
    "TOO BIG"
    AVIEW
    PSE
    PSE
    RDROP ; Keep Rand # in Y
    GTO 01

    LBL 03 ; **Correct handler
    [...]

... can be replaced by these 8 lines below:
    "TOO SMALL"
    X>Y?
    "TOO BIG"
    AVIEW
    PSE
    PSE
    RDROP
    GTO 01

for a net savings of 7 lines of code.

Hope it helps.
V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
10-19-2023, 12:12 PM
Post: #5
RE: My 1st HP-41 program. Suggestions?
(10-19-2023 01:27 AM)Joe Horn Wrote:  What is RDROP?

Sorry I'm mixing metaphors. When looking at the 'R down arrow' (Roll Down) key I was trying to think of how to express that in a text editor and the Forth word DROP came to mind and thus RDROP was born Smile
Find all posts by this user
Quote this message in a reply
10-19-2023, 12:17 PM
Post: #6
RE: My 1st HP-41 program. Suggestions?
(10-19-2023 03:53 AM)Valentin Albillo Wrote:  There are a number of optimizations which could be made but for a first program it's quite acceptable. The largest optimization would be this:

Hope it helps.
V.

Yes, thanks. This was the sort of feedback I was hoping for. Since ALPHA is not displayed until the AWIEW it does make sense to do things as you suggest.
Find all posts by this user
Quote this message in a reply
10-19-2023, 03:59 PM (This post was last modified: 10-19-2023 04:15 PM by Peet.)
Post: #7
RE: My 1st HP-41 program. Suggestions?
(10-19-2023 12:12 PM)Jeff_Birt Wrote:  When looking at the 'R down arrow' (Roll Down) key I was trying to think of how to express that in a text editor and the Forth word DROP came to mind and thus RDROP was born Smile

Press and hold the key to see the name RDN on the 41. Drop isn't the best choise because the stack rotates on RPN and not "drops" like RPL (DROP is more similar to CLX).

By the way, one of my first programs for the 41 over 40 years ago was also "guess a number" (HiLo), but my code was less optimized.

My calculators - former: CBM PR100, HP41CV, HP11C, HP28S - current: HP48G, HP35S, Prime, DM41X, DM42, HP12C
Find all posts by this user
Quote this message in a reply
10-20-2023, 12:00 AM
Post: #8
RE: My 1st HP-41 program. Suggestions?
(10-19-2023 12:17 PM)Jeff_Birt Wrote:  Yes, thanks. This was the sort of feedback I was hoping for.

You're welcome. Since you liked my suggestion, here's another one for you:

Your 6-line code in bold red below ...
    [...]
    INT
    LBL 01
    RCL 01
    1
    +
    STO 01 ; Increment the guess counter
    RDROP ; Keep Rand # in Y

    "Guess 1-10"

    [...]
... can be replaced by this 3-line code
    LBL 01
    ISG 01
    LBL 01
for a net savings of 3 lines (50%).

Regards.
V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
10-20-2023, 02:21 AM
Post: #9
RE: My 1st HP-41 program. Suggestions?
(10-20-2023 12:00 AM)Valentin Albillo Wrote:  [snip]

... can be replaced by this 3-line code
    LBL 01
    ISG 01
    LBL 01
for a net savings of 3 lines (50%).

Regards.
V.

Nice one Valentin! Double use of the same label is an effective NOP here, typically not the case.

At least this portion will not be further optimized... Smile

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
10-20-2023, 03:16 AM
Post: #10
RE: My 1st HP-41 program. Suggestions?
(10-20-2023 02:21 AM)rprosperi Wrote:  
(10-20-2023 12:00 AM)Valentin Albillo Wrote:  ... can be replaced by this 3-line code
    LBL 01
    ISG 01
    LBL 01
Nice one Valentin! Double use of the same label is an effective NOP here, typically not the case.
At least this portion will not be further optimized... Smile

Thanks for your appreciation, Bob. Re-using LBL 01 is ideal, as it's a 1-byte instruction which here it's just a never-executed placeholder and it can't be reached from anywhere in the program, whether directly or indirectly.

And yes, this portion can't be optimized any further ... Smile

Best regards.
V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
10-20-2023, 12:37 PM
Post: #11
RE: My 1st HP-41 program. Suggestions?
(10-20-2023 12:00 AM)Valentin Albillo Wrote:  ... can be replaced by this 3-line code
    LBL 01
    ISG 01
    LBL 01
for a net savings of 3 lines (50%).

Regards.
V.

Thanks. I had briefly looked at ISG (DSE) as they are the only built in INC/DEC type of commands. I was not sure how they would respond if the register was not set up entirely as you would for the proper use of the command. I seem to recall the manual said that if a step size was not specified, or zero was given than a step size of 1 would be used.

The reuse of LBL 01 is clever. I am not certain how the calculator parses a program. Given that this trick works though it must parse form the top and the first instance of LBL 01 is acted on. It leads me to wonder if the program is parsed top to bottom each time a local label is used or if the first time a label is encounter its address is stored somewhere and accessed by index. More reading to do.

Funny how much I have learned over the years from this simple little program.
Find all posts by this user
Quote this message in a reply
10-20-2023, 10:05 PM
Post: #12
RE: My 1st HP-41 program. Suggestions?
(10-20-2023 12:37 PM)Jeff_Birt Wrote:  The reuse of LBL 01 is clever. I am not certain how the calculator parses a program. Given that this trick works though it must parse form the top and the first instance of LBL 01 is acted on. It leads me to wonder if the program is parsed top to bottom each time a local label is used or if the first time a label is encounter its address is stored somewhere and accessed by index. More reading to do.

The calculator scans the source code, searching for the given local label, from the position of the program counter (containing a GTO ou EXQ instruction) down to the first occurrence of the label.

Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co
Find all posts by this user
Quote this message in a reply
10-21-2023, 12:51 AM
Post: #13
RE: My 1st HP-41 program. Suggestions?
Thanks. I thought of a rather contrived example to demonstrate how this works. The code has two global label entry points LBL 'E1' and LBL 'E2', each with an LBL 01 two lines later. XEQ 'E1' and XEQ 'E2' and see where it exits.

Code:

//Double lable trouble
LBL "E1"        ; First entry point
10              ; Entry marker
GTO 02          ; 
LBL 01          ; 1st LBL 01
11              ; Exit marker
RTN
LBL "E2"        ; Second entry point
20              ; Entry marker
GTO 02
LBL 01          ; 2nd LBL 01
22              ; Exit marker
RTN
LBL 02
GTO 01          ; Which LBL 01 will it goto?
Find all posts by this user
Quote this message in a reply
Post Reply 




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