Wednesday, November 30, 2011

HP 15C Programming Tutorial - Part 13: Indirect Addressing and Subroutines

Indexing and Subroutines

This is a continuation in a two-part series covering indirect addressing. While Part 12 dealt with memory registers, Part 13 will deal with subroutines.

To store a number in the index register, press [STO] [TAN] (I)

To call up subroutine label "I", press [GSB] [TAN] (I)

To branch to label "I", press [GTO] [TAN] (I).

When calling subroutines or branching to labels, only the integer portion of I is considered. For either one of these purposes, 0 ≤ I ≤ 24.


If RI contains... GTO I/GSB I calls label...
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 .0
11 .1
12 .2
13 .3
14 .4
15 .5
16 .6
17 .7
18 .8
19 .9
20 A
21 B
22 C
23 D
24 E


We will present two programs that make use of calling subroutines indirectly.

Random Voting Machine

This program uses the calculator's random function to count a specified number of votes between two candidates: Candidate 0 and Candidate 1. In order to get the calculator to round the random number to 0 or 1, we use the setting Fix 0, then execute the RND (round) function.

RND rounds the displayed number, using the display settings for the number of decimal points used.

Labels used: A (main), 0, 1, 2 (loop)

Storage Registers Used:
R0 = number of votes Candidate 0 receives
R1 = number of votes Candidate 1 receives
R2 = total number of votes

Program Listing:


Key Code Key
001 42 21 11 LBL A
002 44 2 STO 2
003 0 0 * Clear R0 and R1
004 44 0 STO 0
005 44 1 STO 1
006 42 7 0 FIX 0 * Fix decimal settings
007 42 21 2 LBL 2
008 42 36 RAN #
009 43 34 RND
010 44 25 STO I * Store result in the
index register
011 32 25 GSB I
012 42 5 2 DSE 2
013 22 2 GTO 2
014 45 0 RCL 0
015 31 R/S
016 45 1 RCL 1
017 43 32 RTN
018 42 21 0 LBL 0
019 1 1
020 44 40 0 STO+ 0
021 43 32 RTN
022 42 21 1 LBL 1
023 1 1
024 44 40 1 STO+ 1
025 43 32 RTN


Instructions:
1. Enter the number of votes to be counted.
2. Press [ f ] [ √ ] (A)
3. The number of votes for Candidate 0 is displayed. Press [R/S] to display the number of votes for Candidate 1.

Example:
A sample vote of 25 votes: 25 [ f ] [ √ ] (A)

You may get 13 votes for Candidate 0 and 12 votes for Candidate 1.

A sample vote of 2,500 votes yields 1,252 votes for Candidate 0 and 1,248 votes for Candidate 1. It took about 17 seconds for the program to run on the HP 15C Limited Edition. It may take considerably longer on the original HP 15C.

Fresnel Integrals

This program calculates the Fresnel Integrals:

Fresnel Sine Integral: S(x) = √(2/π) × ∫((sin u)^2, u, 0, x)

Fresnel Cosine Integral: C(x) = √(2/π) × ∫((cos u)^2, u, 0, x)

This program sets an index. If RI = 0, S(x) is calculated. If RI = 1, C(x) calculated. If RI > 1, the program flashes to communicate an error condition. RI is set before program execution.

Program Listing:


Key Codes Key
001 42 21 11 LBL A
002 45 25 RCL I
003 1 1
004 43 30 8 TEST 8 ( x < y )
005 22 3 GTO 3
006 43 8 RAD
007 33 R ↓
008 33 R ↓
009 0 0
010 34 x<>y
011 42 20 2 ∫ 2 * Integer - label 2
012 2 2
013 43 26 π
014 10 ÷
015 11 √
016 20 ×
017 43 32 RTN
018 42 21 2 LBL 2
019 32 25 GSB I
020 43 11 x^2
021 43 32 RTN
022 42 21 0 LBL 0
023 23 SIN
024 43 32 RTN
025 42 21 1 LBL 1
026 24 COS
027 43 32 RTN
028 42 21 3 LBL 3
029 43 4 9 SF 9
030 43 32 RTN


Caution: Because integration is involved, an additional 23 registers is required. The particular program takes up 28 registers (integration, program instructions)

Instructions:
1. To calculate the Fresnel Sine Integral, set register I to 0. To calculate the Fresnel Cosine Integral, set register I to 1.
2. Enter x.
3. Run program A. [ f ] [ √ ] (A).

Examples:

1. S(1.5) [Fresnel Sine Integral]

Key Strokes:
0 [STO] [TAN] ( I )
1.5 [ f ] [ √ ] (A)

Result: S(1.5) ≈ 0.5703

2. C(1.5) [Fresnel Cosine Integral]

Key Strokes:
1 [STO] [TAN] ( I )
1.5 [ f ] [ √ ] (A)

Result: C(1.5) ≈ 0.6266

3. Try to run program A with I = 2.

Key Strokes:
2 [STO] [TAN] ( I )
1.5 [ f ] [ √ ] (A)

The display flashes. Stop the flash by pressing the backspace key or clearing Flag 9.

This concludes Part 13. Until next time, happy programming,

Eddie




This tutorial is property of Edward Shore. © 2011

Spotlight: Akron Brass FireCalc Pocket Computer

Spotlight: Akron Brass FireCalc Pocket Computer Welcome to a special Monday Edition of Eddie’s Math and Calculator blog. Thi...