Tuesday, November 8, 2011

HP 15C Programming Tutorial - Part 4: Tests

Part 4 of the HP 15C tutorial is going to cover tests and branching. Mastering this is vital to good keystroke programming.

The GTO key

The GTO (Go To) key transfers program control to any label. The label can be located either earlier or later in the set of programming instructions.

The following diagram is example of how GTO works. The instruction in line 001 says "GTO A". Program execution jumps to where "LBL A" starts, which is in line 005.
001		GTO A	("Look for Label A")
002
003
004
005		LBL A	("Here is Label A - continue from here)


Comparison Tests Against Zero

The HP 15C has six comparison tests that compare the contents in Register X (the display) against 0. If the condition is true, the next command is executed. If it is false, the next command is skipped.

In practice, most comparison tests are followed by one or two GTO statements. One label is for instructions when the condition is true, and the other label is for instructions when the condition is false.

The following diagram is an example of how comparison tests work. Let's say the conditional test x=0 is the instruction on Line 001. If x=0, Line 002 is executed. If not, Line 002 is skipped and Line 003 is the next instruction executed.

001		x = 0		(Comparison test)
002		GTO A		(If x = 0, go to label A)
003		instruction	(skip to here if x ≠ 0)
.
.
.
011		LBL A		(continue here from Line 002)
.
.
.


The six comparison tests against zero are:

Test		Key Stroke Sequence
x = 0		[ g ] [ × ] ( x = 0 )
x ≠ 0		[ g ] [ - ] (TEST) [ 0 ] 
x > 0		[ g ] [ - ] (TEST) [ 1 ]
x < 0		[ g ] [ - ] (TEST) [ 2 ]
x ≥ 0		[ g ] [ - ] (TEST) [ 3 ]
x ≤ 0		[ g ] [ - ] (TEST) [ 4 ]


Comparison Tests Involving X and Y

The HP 15C has six comparison tests involving the contents of registers X and Y, which work in the same way as the comparison tests against zero. If the condition is true, the next command is executed, if not the next command is skipped.

Here is an example diagram:
001		x ≤ y		(Comparison test)
002		GTO A		(If X ≤ Y, go to label A)
003		instruction	(skip to here if X > Y)
.
.
.
011		LBL A		(continue here from Line 002)
.
.
.


The six comparison tests between X and Y are:

Test		Key Stroke Sequence
x = y		[ g ] [ - ] (TEST) [ 5 ]
x ≠ y		[ g ] [ - ] (TEST) [ 6 ] 
x > y		[ g ] [ - ] (TEST) [ 7 ]
x < y		[ g ] [ - ] (TEST) [ 8 ]
x ≥ y		[ g ] [ - ] (TEST) [ 9 ]
x ≤ y		[ g ] [ ÷ ] (x ≤ y)


Hint: Only two conditional tests are listed on the keyboard, x = 0 and x ≤ y. For the others, you will need to press [ g ] [ - ] (TEST) [ # ]. A reference of all codes and tests available are located on the back of the calculator.

Let's put some of these conditional tests to work in next two programs.

The Sign Function

The sign function (sometimes called signum) is defined as follows:

              {-1 if x < 0 
sign(x)= {0 if x = 0
              {1 if x > 0


Note: It is assumed that you are familiar with switching the calculator between Run and Program modes. Furthermore, I assume that you are familiar clearing the program memory. Please review Part 2 if you are not familiar with these procedures.

The Sign Function Program

The following program can be shown in the following diagram:

Start with X
	↓
Is X = 0?
Yes:  Display 0 and stop.
No:  Go on to the next step.
	↓ 
Is X < 0?
Yes:  Go to Label 0.
No:  Display 1 and stop, because X > 0.
	↓ 
LABEL 0:
Display -1 and stop.
	↓ 
	END


We will use label C in this program. The display is on the left column, the required calculator keystrokes are on the right.

Program Listing: The Sign Function

Display				Keystrokes
001 -	42	21	13	[ f ] [SST] (LBL) [10^x] (C)
002 -		43	20	[ g ] [ × ] ( x = 0 )
003 -		43	32	[ g ] [GSB] (RTN)
004 - 	43	30	2	[ g ] [ - ] (TEST) [ 2 ] ( x < 0 )
005 - 		22	0	[GTO] [ 0 ]
006 -			1	[ 1 ]
007 - 		43	32	[ g ] [GSB] (RTN)
008 -	42	21	0	[ f ] [SST] (LBL) [ 0 ]
009 -			1	[ 1 ]
010 -			16	[CHS]
011 -		43	32	[ g ] [GSB] (RTN)


Notes:
* Line 002 - first test, X = 0.
* Line 004 - second test X < 0.
* Lines 006 and 007, instructions when X > 0
* Lines 008 through 011, instructions when X < 0

Examples:
1. sign(-52):

-52 [ f ] [10^x] (C)

Result: -1

2. sign(0):

0 [ f ] [10^x] (C)

Result: 0

3. sign(36):

36 [ f ] [10^x] (C)

Result: 1
The Game of Bust

This program is the game of Bust. The player builds a total from values chosen randomly from 1 to 10. The goal is to get the total close to 21 without going over.

This program uses three labels: A, B, and 0.
* Label A: Initializes the program
* Label B: Draw a number and add it to the total.
* Label 0: Instructions when the player "busts" (goes over 21).

Program Diagram:
Label A:  Initialization
Store 0 in R0 (memory register 0)
	↓ 
Label B:  
Put 1 on the X stack
	↓ 
Obtain a random number
	↓ 
Multiply the random number by 10
	↓ 
Take the integer part of the result and add it to 1
	↓ 
Stop and display the number
	↓ 
Add the number to the total
	↓ 
Is the total > 21?
Yes: Go to Label 0
No: Display the totalHuman Decision:  Draw again?
Execute Label B.

Label 0:  (Total > 21)
Display and flash the total.

	END


Program Listing: The Game of Bust

Display				Keystrokes
001 -	42	21	11	[ f ] [SST] (LBL) [ √ ] (A)
002 -			0	[ 0 ]
003 -		44	0	[STO] [ 0 ]
004 -		43	32	[ g ] [GSB] (RTN)
005 -	42	21	12	[ f ] [SST] (LBL) [e^x] (B)
006 -			1	[ 1 ]
007 -			36	[ENTER]
008 -			1	[ 1 ]
009 -			0	[ 0 ]
010 -		42	36	[ f ] [ENTER] (RAN#)
011 -			20	[ × ]
012 -		43	44	[ g ] [STO] (INT)
013 -			40	[ + ]
014 - 			31	[R/S]
015 -	44	40	0	[STO] [ + ] [ 0 ]
016 -		45	0	[RCL] [ 0 ]
017 - 			2	[ 2 ]
018 -			1	[ 1 ]
019 -	43	30	8	[ g ] [ - ] (TEST) [ 8 ] (x<y)
020 -		22	0	[GTO] [ 0 ]
021 -			34	[x<>y]
022-			43	32	[ g ] [GSB] (RTN)
023 -	42	21	0	[ f ] [SST] (LBL) [ 0 ]
024 -			34	[x<>y]
025 -	43	4	9	[ g ] [ 4 ] (SF) [ 9 ]
026 -		43	32	[ g ] [GSB] (RTN)


Instructions:
1. Press [ f ] [ √ ] (A) to start a new game.
2. Press [ f ] [e^x] (B) to draw numbers. The random number is displayed.
3. Press [R/S] to display the total.
4. If the total is over 21, you have busted, and the display flashes. Press [ g ] [ 5 ] (CF) [ 9 ], [ ← ], or [ON] to stop the flashing.
5. If the total is not over 21, decide whether to stop. If you want to draw again, repeat instructions 2 and 3. ([ f ] [e^x] (B))

A example game:
Paul, Julie, and Chris are playing the game of Bust.

Paul
[ f ] [ √ ] (A)
[ f ] [e^x] (B); (Display: 5); [R/S]; (Total: 5)
[ f ] [e^x] (B); (Display: 1); [R/S]; (Total: 6)
[ f ] [e^x] (B); (Display: 5); [R/S]; (Total: 11)
[ f ] [e^x] (B); (Display: 1); [R/S]; (Total: 12)
[ f ] [e^x] (B); (Display: 9); [R/S]; (Total: 20, decides to stop)

Julie:
[ f ] [ √ ] (A)
[ f ] [e^x] (B); (Display: 6); [R/S]; (Total: 6)
[ f ] [e^x] (B); (Display: 4); [R/S]; (Total: 10)
[ f ] [e^x] (B); (Display: 10); [R/S]; (Total: 20, decides to stop)

Chris:
[ f ] [ √ ] (A)
[ f ] [e^x] (B); (Display: 6); [R/S]; (Total: 6)
[ f ] [e^x] (B); (Display: 5); [R/S]; (Total: 11)
[ f ] [e^x] (B); (Display: 3); [R/S]; (Total: 14)
[ f ] [e^x] (B); (Display: 8); [R/S]; (Total: 22 - flashing - busted - [ ← ])

Playoff:
Paul: [ f ] [ √ ] (A) [ f ] [e^x] (B) - Display: 3
Julie: [ f ] [ √ ] (A) [ f ] [e^x] (B) - Display: 8 - (Julie wins the playoff)
Next time, we will tackle the Quadratic Formula. Until then,

Eddie


This tutorial is property of Edward Shore. Mass reproduction and distribution requires express permission. © 2011

Fun with the HP 30b

Fun with the HP 30 b Introduction The following programs are for the HP 30b Business Professional. Did you know that the 30b...