HP 15c Manual
Have a look at the manual HP 15c Manual online for free. It’s possible to download the document as PDF or print. UserManuals.tech offer 1114 HP manuals and user’s guides for free. Share the user manual or guide on Facebook, Twitter or Google+.
101 Section 9 Subroutines When the same set of instructions needs to be used at more than one point in a program, memory space can be conserved by storing those instructions as a single subroutine. The Mechanics Go To Subroutine and Return The G (go to subroutine) instruction is executed in the same way as the t branch, with one major difference: it establishes a pending return condition. G label, like t label,* transfers program execution to the line with the corresponding label (A to E, 0 to 9 or .0 to .9). However, execution then continues until the first subsequent n instruction is encountered – at which point execution transfers back to the instruction immediately following the last G instruction, and continues on from there. Subroutine Execution ´bA ´b.1 G.1 |n |n END RETURN Execution transfers to line 000 and halts. Execution transfers back to original routine after G.1 * A G or t instruction followed by a letter label is an abbreviated key sequence (no ´ necessary). Abbreviated key sequences are explained on page 78.
102 Section 9: Subroutines Subroutine Limits A subroutine can call up another subroutine, and that subroutine can call up yet another subroutine. This ―subroutine nesting‖—the execution of a subroutine within a subroutine—is limited to stack of subroutines seven levels deep (this does not count the main program level). The operation of nested subroutines is as shown below: Main Program bA b1 b2 b3 b4 G1 G3 G2 G4 n n n n n End Examples Example: Write a program to calculate the slope of the secant line joining points (x1, y1) and (x2, y2) on the graph shown, where y = x2 - sin x (given x in radians). The secant slope is: The solution requires that the equation for y be evaluated twice—once for y1 and once for y2, given the data input for x1 and x2. Since the same calculation must be made for different values, it will save program space to call a subroutine to calculate y. The following program assumes that x1 has been entered into the Y-register and x2 into the X-register. 12 121222 12 12)sin ()sin (or ,xx xxxx xx yy
Section 9: Subroutines 103 MAIN PROGRAM |¥ ´ CLEAR M (Not programmable.) 000- 001- ´ b 9 Start main program. 002- | R Radians mode. 003- O 0 Stores x2 in R0. 004- ® Brings x1 into X; x2 into Y. 005- O - 0 (x2 - x1) in R0. 006- G .3 Transfer to subroutine ―.3‖ with x1. Return from subroutine ―.3‖. 007- “ - y1. 008- ® Brings x2 into X-register. 009- G .3 Transfer to subroutine with x2. Return from subroutine ―.3‖. 010- + y2 - y1. 011- l ÷ 0 Recalls (x2 – x1) from R0 and calculates (y2 - y1)/(x2 - x1). 012- | n Program end (return to line 000). SUBROUTINE 013- ´ b .3 Start subroutine .3. 014- | x x2. 015- | K Recall x. 016- [ sin x. 017- - x 2 – sin x, which equals y. 018- | n Return to origin in main program. Calculate the slope for the following values of x1 and x2: 0.52, 1.25; -1, 1; 0.81, 0.98. Remember to use G 9 (rather than ´ 9) when addressing a routine with a digit label. Answers: 1.1507; -0.8415; 1.1652.
104 Section 9: Subroutines Example: Nesting. The following subroutine, labeled ―.4‖, calculates the value of the expression as part of a larger calculation in a larger program. The subroutine calls upon another subroutine (a nested subroutine), labeled ―.5‖, to do the repetitive squaring. The program is executed after placing the variables t, z, y, and x into the T-, Z-, Y-, and X-registers. Keystrokes ´ b.4 Start of main subroutine. | x x2. G.5 Calculates y2 and x2 + y2. G.5 Calculates z2 and X2 + y2 + z2. G.5 Calculates t2 and x2 + y2 + z2 + t2. ¤ | n End of main subroutine; returns to main program. ´ b.5 Start of nested subroutine. ® | x Calculates a square and + adds it to current sum of squares. | n End of nested sub-routine; returns to main subroutine. If you run the subroutine (with its nested subroutine) alone using x = 4.3, y = 7.9, z = 1.3, and t = 8.0, the answer you get upon pressing G.4 is 12.1074. 2222tzyx 2 2 2 2t zyx
Section 9: Subroutines 105 Further Information The Subroutine Return The pending return condition means that the n instruction occurring subsequent to a G instruction causes a return to the line following the G rather than a return to line 000. This is what makes a subroutine useful and reuseable in different parts of a program: it will always return execution to where it branched from, even as that point changes. The only difference between using a G branch and a t branch is the transfer of execution after a n. Nested Subroutines If you attempt to call a subroutine that is nested more than seven levels deep, the calculator will halt and display Error 5 when it encounters the G instruction at the eighth level. Note that there is no limitation (other than memory size) on the number of nonnested subroutines or sets of nested subroutines that you may use.
106 Section 10 The Index Register and Loop Control The Index register (RI) is a powerful tool in advanced programming of the HP-15C. In addition to storage and recall of data the Index register can use an index number to: Count and control loops. Indirectly address storage registers, including those beyond R.9 (R19). Indirectly branch to program line numbers, as well as to labels. Indirectly control the display format. Indirectly control flag operations. The V and % Keys Direct Versus Indirect Data Storage With the Index Register The Index register is a data storage register that can be used directly, with V, or indirectly, with %.* The difference is important to note: V % The V function uses the number itself in the Index register. The % function uses the absolute value of the integer portion of the number in the Index register to address another data storage register. This is called indirect addressing. * Note that the matrix functions and complex functions use the V and % keys also, but for different purposes. Refer to sections 11 and 12 for their usage.
Section 10: The Index Register and Loop Control 107 Indirect Program Control With the Index Register The V key is used for all forms of indirect program control other than indirect register addressing. Hence, V (not %) is used for indirect program branching, indirect display format control, and indirect flag control. Program Loop Control Program loop counting and control can be carried out in the HP-15C by any storage register: R0 through R9, R.0 through R.9, or the Index register (V). Loop control can also be carried out indirectly with %. The Mechanics Both V and % can be used in abbreviated key sequences, omitting the preceding ´ prefix (as explained on page 78). Index Register Storage and Recall Direct. O V and l V. Storage and recall between the X- register and the Index register operate in the same manner as with other data storage registers (page 42). Indirect. O (or l) % stores into (or recalls from) the data storage register whose number is addressed by the integer portion of the value (0 to 65) in the Index register. See the table below and on the next page. Indirect Addressing If RI contains: % will address: t V or G V will transfer to:* ± 0 R0 ´ b 0 ⋮ ⋮ ⋮ 9 R9 ´ b 9 10 R.0 .0 11 R.1 .1 ⋮ ⋮ ⋮ 19 R.9 ´ b .9 20 R20 A *For RI 0 only. (Continued on next page.)
108 Section 10: The Index Register and Loop Control Indirect Addressing If RI contains: % will address: t V or GV will transfer to:* 21 R21 ´ b B 22 R22 C 23 R23 Á 24 R24 E ⋮ ⋮ — 65 R65 — *For RI 0 only. Index Register Arithmetic Direct. O or l { + , -, *, ÷ } V. Storage or recall arithmetic operates with the Index register in the same manner as upon other data storage registers (page 43). Indirect. O or l { + , -, *, ÷ } % carries out storage or recall arithmetic with the contents of the data storage register addressed by the integer portion of the number (0 to 65) in the Index register. See the above table. Exchanging the X-Register Direct. ´ X V exchanges contents between the X-register and the Index register. (Works the same as X n does with registers 0 through .9.) Indirect. ´ X % exchanges contents between the X-register and the data storage register addressed by the number (0 to 65) in the Index register. See the above table. Indirect Branching With V The V key—but not the % key—can be used for indirect branching (tV) and subroutine calls (GV). (Only the integer portion of the number in RI is used.) (% is only used for indirect addressing of storage registers).
Section 10: The Index Register and Loop Control 109 To Labels. If the RI value is positive, t V and G V will transfer execution to the label which corresponds to the number in the Index register (see the above table). For instance, if the Index register contains 20.00500, then a tV instruction will transfer program execution to ´b A. See the chart on page 107. To Line numbers. If the RI value is negative, tV causes branching to that line number (using the absolute value of the integer portion of the value in RI). For instance, if RI contains –20.00500, then a tV instruction will transfer program execution to program line 020. Indirect Flag Control With V F V, V, or ? V will set, clear, or test the flag (0 to 9) specified in RI (by the magnitude of the integer portion). Indirect Display Format Control With V ´ • V, ´ i V, and ´ ^ V will format the display in their customary manner (refer to pages 58–59), using the number in RI (integer part only) for n, which must be from 0 to 9.* Loop Control With Counters: I and e The I (increment and skip if greater than) and e (decrement and skip if less than or equal to) functions control loop execution by referencing and altering a loop control number in a given register. Program execution (skipping a line or not) then depends on that number. The key sequence is ´ { I, e } register number. This number is 0 to 9, .0 to .9, V ,or %. The Loop Control Number. The format of the loop control number is: nnnnn.xxxyy, where ±nnnnn is the current counter value, xxx is the test (goal) value, and yy Is the increment of decrement value * Except when using f (section 14)
110 Section 10: The Index Register and Loop Control For example, the number 0.05002 in a storage register represents: nnnnn x x x y y 0.0 5 0 0 2 Start count at zero. Count by twos. Count up to 50. I and e Operation. Each time a program encounters I or e it increments or decrements nnnnn (the integer portion of the loop control number), thereby keeping count of the loop iterations. It compares nnnnn to xxx, the prescribed test value, and exits the loop by skipping the next line if the loop counter (nnnnn) is either greater than (I) or less than or equal to (e) the test value (xxx). The amount that nnnnn is incremented or decremented is specified by yy. With these functions (as opposed to the other conditional tests), the rule is ―Skip if True‖. False (nnnnn xxx) True (nnnnn > xxx) instruction ´IV loop t. 1 instruction exit loop For I: given nnnnn.xxxyy, increment nnnnn to nnnnn + yy, compare it to xxx, and skip the next program line if the new value satisfies nnnnn > xxx. This allows you to exit a loop at this point when nnnnn becomes greater than xxx.