Friday, November 30, 2018

Casio fx-CG50: Using Goto and Label to Simulate Subroutines

Casio fx-CG50: Using Goto and Label to Simulate Subroutines

If you have a subroutine, you can program the main algorithm and any subroutines in one program file.  In this method, a single subroutine can be executed up to 36 times.  The programming language for Casio allows up to 37 labels: 0 through 9, A through Z, and θ.  

The Technique

1.  Know how many times the subroutine is to be executed.  This is the key.  

2.  We will need to set aside a variable to keep count.

3.  Use a label for the subroutine portion of the program.  The subroutine will be kept at the bottom of the program file.

4.  Each time the subroutine is executed, increase the counter by 1 and execute a jump test by using ⇒.  

5.  Finish the main portion of the program with the Stop command. 

Sample:

[main program starts here]
0 → C  [designate a counter variable]
Goto 0   [go to the subroutine]
Lbl 1 [resume]
...
Goto 0 [run the subroutine the 2nd time]
Lbl 2 [resume]
...
Goto 0 [run the subroutine the 3rd time]
Lbl 3 [resume]
...
(and so on)
...
Stop  [end of main program]

Lbl 0 [start of subroutine]
...
If C = 1 ⇒ Goto 1
If C = 2 ⇒ Goto 2
If C = 3 ⇒ Goto 3
... (and so on)

You can make this a simple or complex as you want.  Below are two sample programs, LOOP1 and LOOP2 that illustrate this technique. 

Note:  

The programs listed here are text files generated from the Casio fx-CG50.  You can generate text files by selecting a program from the Program List ([MENU] [ log ] (B)), then pressing [ F6 ] ( > ), [ F3 ] (SAVE.AS).  

Y^<2> represents Y^2

(-)1 represents -1

Example Programs

LOOP1:  Calculates the following:

f(x)^2 + 2 * f(x) - 1

In this example f(x) = x^2 - 3*x + 1

'ProgramMode:RUN
"F(X): X"?->X
0->C
Goto 0
Lbl 1
Y^<2>->F
Goto 0
Lbl 2
2*Y+F-1->F
F
Stop
Lbl 0
X^<2>-3*X+1->Y
C+1->C
C=1=>Goto 1
C=2=>Goto 2


Now for a more detailed example.  

LOOP2:   This program splits two positive integers of equal length (example: 4326 and 1821, both have four digits each).  Carry over is included and the result is returned in a list of digits.  

Example:  4326  + 1481 = 5807  (displayed as {5, 8, 0, 7})

'ProgramMode:RUN
"SPLIT R+T"
"R"?->R
"T"?->T
0->C
R->X
Goto 0
Lbl 1
List 1->List 2
T->X
Goto 0
Lbl 2
List 1+List 2->List 2
For N->I To 2 Step (-)1
If List 2[I]>=10
Then 
List 2[I]-10->List 2[I]
List 2[I-1]+1->List 2[I-1]
IfEnd
Next
If List 2[1]>=10
Then 
List 2[1]-10->List 2[1]
Augment({1},List 2)->List 2
IfEnd
List 2
Stop


Lbl 0
Int (log X)+1->N
X/(10)(N-1)->S
Int S->I
Frac S->F
{I}->List 1
F*(10)(N-1)->X
For N-2->K To 0 Step (-)1
X/(10)K->S
Int S->I
Frac S->F
Augment(List 1,{I})->List 1
F*(10)K->X
Next
C+1->C
C=1=>Goto 1
C=2=>Goto 2

Have fun and happy upcoming December!

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

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...