Page 1 of 1

Range check function

PostPosted: Wed Dec 09, 2009 3:31 am
by hsilop
Hi, I am new here. My name is Steve and have been a fan of HP calculators since about 1975. My first calculator was the HP-25 and is still my favorite. I have been (slowly) collecting HP calculators over the years. My collection now consists of a 46, 25, 11C, 15C, 16C and most recently a 41CV. I have been after a 41 for many years and was able to score one in mint condition at a very reasonable price.

I just love this thing! Being well used to programming on the other HP models I found the addition of alphanumeric characters just makes this thing a whole new experience, I've only had it a few weeks, but I have been fiddling with it more-or-less no stop. It is such as awesome and versatile tool.

Along my journey I developed several programs that had a common requirement to see if a number entered was within a given range. Since the built-in conditional tests can only compare two numbers at a time, this presented a little programming exercise in itself. Then I though wouldn't it be cool to have a common function ( I'm an old C programmer ) that could do this and call it from each program. Thus saving memory and more importantly making the code simpler.

So I thought I would share this with the forum. I am sure the rest of you have done something similar. But you never know, one of you might find it useful. BTW - In the code below characters within double quotes are ALPHA characters. And <append> means shift-K in ALPHA mode. I have also developed a general purpose wrapper function that prompts for a number and tests it is valid between the given range X and Y before returning the input value in X. Note the code in GETX to restore the ALPHA register on exit is so that this subroutine can be called repeatedly without setting up the ALPHA register for each call if the prompts are the same. Plus I hate subroutines that have destructive side-effects.

Name: Y<=X<=Z

Stack on input:

    T:
    Z: Upper bound
    Y: Lower bound
    X: Number being tested


Stack on return:

    T:Lower bound
    Z:
    Y:Upper bound
    X:Number being tested


Flags:
    00: Cleared if X between Y and Z, otherwise set.

Description: Compares the number in the X register to the numbers in the Y and Z registers. If the number is between the two then flag 00 is cleared, otherwise flag 00 is set. On return the contents of the X register contains the number that was tested. If you want to restore the stack to the way it was before the call, insert R^ (Roll Up) followed by an X<>Y before the function return.

Code:

Code: Select all
01 LBL Y<=X<=Z
02 SF 00                Set the flag
03 X<>Y
04 X<=Y?                See if Y >= X
05 CF 00                Clear flag
06 RDN                  Roll down
07 X>Y?                 See if X > Y
08 SF 00                Too big - set flag
09 RTN                  Return


Name: GETX

Stack on input:

    T:
    Z:
    Y: Upper bound
    X: Lower bound
    ALPHA: Prompt prefix

Stack on return:

    T: Lower bound
    Z: Last invalid input ( if any )
    Y: Upper bound
    X: Valid number input
    ALPHA: Prompt prefix

Registers:
    18: Temporary hold for prompt prefix

Side effects:
    Changes the display mode to FIX 0

Description: Displays a prompt "PREFIX <X-Y>". Where the values of X and Y registers are the valid range of the input and PREFIX is the text in the ALPHA register. ( NOTE: The value in Y is assumed to be larger than or equal to the value in X )

Code: Select all
01 LBL GETX
02 FIX 0
03 ASTO 18          Save prefix
04 LBL 01
05 CLA
06 ARCL 18          Construct prompt
07 "<append> <"
08 ARCL X
09 "<append>-"
10 ARCL Y
11 "<append>">
12 PROMPT         Get user input
13 XEQ Y<=X<=Z    Call the function
14 FS? 00         If it's bad
15 GTO 02
16 CLA
17 ARCL 18        Restore ALPHA register
18 RTN
19 LBL 02
20 "OUT OF RANGE" Display error
21 AVIEW
22 PSE
23 X<>Y           Fixup stack
24 RCL T
25 GTO 01         Try again


Usage:
Code: Select all
3               Upper bound
ENTER^
1               Lower bound
"BLAH"   Your prompt prefix
XEQ GETX        Call the function

Re: Range check function

PostPosted: Wed Dec 09, 2009 12:45 pm
by jotne
Thanks for your contribution.

Re: Range check function

PostPosted: Thu Dec 10, 2009 10:50 am
by hsilop
Thanks. I've been having fun.

I've improved the GETX subroutine a little. I noticed if you hit R/S without entering anything the wheels fall off - badly! So I've added some code to prevent that. There isn't much you can't do on this thing!

Code: Select all
    01 LBL GETX
    02 FIX 0
    03 ASTO 18          Save prefix
    04 LBL 01
    05 CLA
    06 ARCL 18          Construct prompt
    07 "<append> <"
    08 ARCL X
    09 "<append>-"
    10 ARCL Y
    11 "<append>">
    12 CF 22
    13 PROMPT         Get user input
    14 FS? 22
    15 GTO 02
    16 "NULL INPUT"
    17 AVIEW
    18 PSE
    19 GTO 01
    20 XEQ Y<=X<=Z    Call the function
    21 FS? 00         If it's bad
    22 GTO 02
    23 CLA
    24 ARCL 18        Restore ALPHA register
    25 RTN
    26 LBL 02
    27 "OUT OF RANGE" Display error
    28 AVIEW
    29 PSE
    30 X<>Y           Fixup stack
    31 RCL T
    32 GTO 01         Try again


It's a bit quiet in here ...

Re: Range check function

PostPosted: Fri Dec 11, 2009 3:39 am
by Garth
As you get further into this venerable machine, you'll probably be getting more modules or at least one of the modern large memories for it that can store many ROM images and user files as well. The 41cx, as well as I'm sure the extended functions module for the 41cv, has a GETX that reads the next register of a data file from extended memory into the X register. Your enjoyment of the machine will further be increased when you get into synthetic programming. I personally have not gone to the extent of assembly-language programming yet.

Re: Range check function

PostPosted: Fri Dec 11, 2009 7:36 am
by hsilop
I don't think I'm quite ready for synthetic programming just yet. Maybe in a few more weeks.

I did some work in Intel assembler today ( first time in over 20 years! ) and it struck me how similar it felt to programming the 41CV. I had a ball! Haven't had so much fun at work for ages.