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