Post Reply 
[TI-57] Calculate Square Root
10-24-2024, 07:47 AM (This post was last modified: 10-25-2024 09:31 AM by Thomas Klemm.)
Post: #1
[TI-57] Calculate Square Root
Based on a recent thread I took a closer look at the assembly code of the TI-57.
To analyze the code, I extended the RCL-57 emulator's command line tool to track the three registers A, B, and C.

This is the relevant assembly code:
Code:
0x0640: 0x13f4  CALL    0x03f4
0x0641: 0x05a1  LOAD    C,C-1,0000 0000 0000 00FF
0x0642: 0x1e4b  BRC     0x064b
0x0643: 0x05a1  LOAD    C,C-1,0000 0000 0000 00FF
0x0644: 0x1e47  BRC     0x0647
0x0645: 0x0520  LOAD    A,A+1,0000 0000 0000 00FF
0x0646: 0x1a41  BRNC    0x0641
0x0647: 0x0c96  TST     C[13:1]
0x0648: 0x1a4a  BRNC    0x064a
0x0649: 0x0520  LOAD    A,A+1,0000 0000 0000 00FF
0x064a: 0x026d  LOAD    NUL,B>>1,000F FFFF FFFF FF00
0x064b: 0x0c91  CLR     C[13:0]
0x064c: 0x0849  LOAD    B,B-B,0F00 0000 0000 0000
0x064d: 0x0507  MOVE    A,A,0000 0000 0000 00FF
0x064e: 0x0978  LOAD    B,B+R5,FFF0 0000 0000 0000
0x064f: 0x0501  LOAD    A,A-A,0000 0000 0000 00FF
0x0650: 0x0591  LOAD    C,C-C,0000 0000 0000 00FF
0x0651: 0x1a61  BRNC    0x0661
0x0652: 0x036d  LOAD    NUL,B>>1,000F FFFF FFFF FFFF
0x0653: 0x032d  LOAD    NUL,A>>1,000F FFFF FFFF FFFF
0x0654: 0x1a60  BRNC    0x0660
0x0655: 0x0381  LOAD    C,C-A,000F FFFF FFFF FFFF
0x0656: 0x0381  LOAD    C,C-A,000F FFFF FFFF FFFF
0x0657: 0x0308  LOAD    A,A+B,000F FFFF FFFF FFFF
0x0658: 0x0385  LOAD    NUL,C-A,000F FFFF FFFF FFFF
0x0659: 0x1a55  BRNC    0x0655
0x065a: 0x0309  LOAD    A,A-B,000F FFFF FFFF FFFF
0x065b: 0x0312  LOAD    C,A+C,000F FFFF FFFF FFFF
0x065c: 0x1a5d  BRNC    0x065d
0x065d: 0x00a5  LOAD    NUL,C-1,000F 0000 0000 0000
0x065e: 0x1a52  BRNC    0x0652
0x065f: 0x03a8  LOAD    C,C<<1,000F FFFF FFFF FFFF
0x0660: 0x036d  LOAD    NUL,B>>1,000F FFFF FFFF FFFF
0x0661: 0x0365  LOAD    NUL,B-1,000F FFFF FFFF FFFF
0x0662: 0x1a56  BRNC    0x0656
0x0663: 0x15b2  CALL    0x05b2

Here is a typical output:
Code:
(…)
0x0659: 0x1a55  BRNC    0x0655                          A=1000400000000000 B=0000100000000000 C=0002900000000000
0x0655: 0x0381  LOAD    C,C-A,000F FFFF FFFF FFFF       A=1000400000000000 B=0000100000000000 C=0002500000000000
0x0656: 0x0381  LOAD    C,C-A,000F FFFF FFFF FFFF       A=1000400000000000 B=0000100000000000 C=0002100000000000
0x0657: 0x0308  LOAD    A,A+B,000F FFFF FFFF FFFF       A=1000500000000000 B=0000100000000000 C=0002100000000000
0x0658: 0x0385  LOAD    NUL,C-A,000F FFFF FFFF FFFF     A=1000500000000000 B=0000100000000000 C=0002100000000000
0x0659: 0x1a55  BRNC    0x0655                          A=1000500000000000 B=0000100000000000 C=0002100000000000
0x0655: 0x0381  LOAD    C,C-A,000F FFFF FFFF FFFF       A=1000500000000000 B=0000100000000000 C=0001600000000000
0x0656: 0x0381  LOAD    C,C-A,000F FFFF FFFF FFFF       A=1000500000000000 B=0000100000000000 C=0001100000000000
0x0657: 0x0308  LOAD    A,A+B,000F FFFF FFFF FFFF       A=1000600000000000 B=0000100000000000 C=0001100000000000
0x0658: 0x0385  LOAD    NUL,C-A,000F FFFF FFFF FFFF     A=1000600000000000 B=0000100000000000 C=0001100000000000
0x0659: 0x1a55  BRNC    0x0655                          A=1000600000000000 B=0000100000000000 C=0001100000000000
0x0655: 0x0381  LOAD    C,C-A,000F FFFF FFFF FFFF       A=1000600000000000 B=0000100000000000 C=0000500000000000
0x0656: 0x0381  LOAD    C,C-A,000F FFFF FFFF FFFF       A=1000600000000000 B=0000100000000000 C=0009900000000000
0x0657: 0x0308  LOAD    A,A+B,000F FFFF FFFF FFFF       A=1000700000000000 B=0000100000000000 C=0009900000000000
0x0658: 0x0385  LOAD    NUL,C-A,000F FFFF FFFF FFFF     A=1000700000000000 B=0000100000000000 C=0009900000000000
0x0659: 0x1a55  BRNC    0x0655                          A=1000700000000000 B=0000100000000000 C=0009900000000000
0x065a: 0x0309  LOAD    A,A-B,000F FFFF FFFF FFFF       A=1000600000000000 B=0000100000000000 C=0009900000000000
0x065b: 0x0312  LOAD    C,A+C,000F FFFF FFFF FFFF       A=1000600000000000 B=0000100000000000 C=0000500000000000
0x065c: 0x1a5d  BRNC    0x065d                          A=1000600000000000 B=0000100000000000 C=0000500000000000
0x065d: 0x00a5  LOAD    NUL,C-1,000F 0000 0000 0000     A=1000600000000000 B=0000100000000000 C=0000500000000000
0x065e: 0x1a52  BRNC    0x0652                          A=1000600000000000 B=0000100000000000 C=0000500000000000
0x065f: 0x03a8  LOAD    C,C<<1,000F FFFF FFFF FFFF      A=1000600000000000 B=0000100000000000 C=0005000000000000
0x0660: 0x036d  LOAD    NUL,B>>1,000F FFFF FFFF FFFF    A=1000600000000000 B=0000010000000000 C=0005000000000000
0x0661: 0x0365  LOAD    NUL,B-1,000F FFFF FFFF FFFF     A=1000600000000000 B=0000010000000000 C=0005000000000000
0x0662: 0x1a56  BRNC    0x0656                          A=1000600000000000 B=0000010000000000 C=0005000000000000
0x0656: 0x0381  LOAD    C,C-A,000F FFFF FFFF FFFF       A=1000600000000000 B=0000010000000000 C=0004400000000000
0x0657: 0x0308  LOAD    A,A+B,000F FFFF FFFF FFFF       A=1000610000000000 B=0000010000000000 C=0004400000000000
0x0658: 0x0385  LOAD    NUL,C-A,000F FFFF FFFF FFFF     A=1000610000000000 B=0000010000000000 C=0004400000000000
0x0659: 0x1a55  BRNC    0x0655                          A=1000610000000000 B=0000010000000000 C=0004400000000000
(…)

However, we simply list the line 0x0659 to get the relevant data when calculating \(\sqrt{41}\):

A=1000100000000000 B=0000100000000000 C=0004100000000000
A=1000200000000000 B=0000100000000000 C=0003900000000000
A=1000300000000000 B=0000100000000000 C=0003500000000000
A=1000400000000000 B=0000100000000000 C=0002900000000000
A=1000500000000000 B=0000100000000000 C=0002100000000000
A=1000600000000000 B=0000100000000000 C=0001100000000000
A=1000700000000000 B=0000100000000000 C=0009900000000000
A=1000610000000000 B=0000010000000000 C=0004400000000000
A=1000620000000000 B=0000010000000000 C=0003180000000000
A=1000630000000000 B=0000010000000000 C=0001940000000000
A=1000640000000000 B=0000010000000000 C=0000680000000000
A=1000650000000000 B=0000010000000000 C=0009400000000000
A=1000641000000000 B=0000001000000000 C=0009760000000000
A=1000640100000000 B=0000000100000000 C=0003360000000000
A=1000640200000000 B=0000000100000000 C=0002079800000000
A=1000640300000000 B=0000000100000000 C=0000799400000000
A=1000640400000000 B=0000000100000000 C=0009518800000000
A=1000640310000000 B=0000000010000000 C=0000950700000000
A=1000640320000000 B=0000000010000000 C=0009670080000000
A=1000640311000000 B=0000000001000000 C=0002463590000000
A=1000640312000000 B=0000000001000000 C=0001182968000000
A=1000640313000000 B=0000000001000000 C=0009902344000000
A=1000640312100000 B=0000000000100000 C=0004786248000000
A=1000640312200000 B=0000000000100000 C=0003505623800000
A=1000640312300000 B=0000000000100000 C=0002224999400000
A=1000640312400000 B=0000000000100000 C=0000944374800000
A=1000640312500000 B=0000000000100000 C=0009663750000000
A=1000640312410000 B=0000000000010000 C=0002400311600000
A=1000640312420000 B=0000000000010000 C=0001119686780000
A=1000640312430000 B=0000000000010000 C=0009839061940000
A=1000640312421000 B=0000000000001000 C=0004153431180000
A=1000640312422000 B=0000000000001000 C=0002872806338000
A=1000640312423000 B=0000000000001000 C=0001592181494000
A=1000640312424000 B=0000000000001000 C=0000311556648000
A=1000640312423100 B=0000000000000100 C=0008878378287000
A=1000640312423200 B=0000000000000100 C=0007597753440800
A=1000640312423300 B=0000000000000100 C=0006317128594400
A=1000640312423400 B=0000000000000100 C=0005036503747800
A=1000640312423500 B=0000000000000100 C=0003755878901000
A=1000640312423600 B=0000000000000100 C=0002475254054000
A=1000640312423700 B=0000000000000100 C=0001194629206800
A=1000640312423800 B=0000000000000100 C=0009914004359400
A=1000640312423710 B=0000000000000010 C=0004902855407300
A=1000640312423720 B=0000000000000010 C=0003622230559880
A=1000640312423730 B=0000000000000010 C=0002341605712440
A=1000640312423740 B=0000000000000010 C=0001060980864980
A=1000640312423750 B=0000000000000010 C=0009780356017500
A=1000640312423741 B=0000000000000001 C=0003566371988660
A=1000640312423742 B=0000000000000001 C=0002285747141178
A=1000640312423743 B=0000000000000001 C=0001005122293694
A=1000640312423744 B=0000000000000001 C=0009724497446208

This is a largely literal translation of the code for the HP-42S:
Code:
00 { 39-Byte Prgm }     ; C=x
01 12                   ; n=12 C
02 STO 00               ; n C
03 SIGN                 ; B=1 C
04 X<>Y                 ; C B
05 0                    ; A=0 C B
06 GTO 01               ; X:A Y:C Z:B
07▸LBL 00               ; 
08 STO- ST Y            ; 0x0655: LOAD    C,C-A,000F FFFF FFFF FFFF
09▸LBL 01               ; 
10 STO- ST Y            ; 0x0656: LOAD    C,C-A,000F FFFF FFFF FFFF
11 RCL+ ST Z            ; 0x0657: LOAD    A,A+B,000F FFFF FFFF FFFF
12 X≤Y?                 ; 0x0658: LOAD    NUL,C-A,000F FFFF FFFF FFFF
13 GTO 00               ; 0x0659: BRNC    0x0655
14 RCL- ST Z            ; 0x065a: LOAD    A,A-B,000F FFFF FFFF FFFF
15 STO+ ST Y            ; 0x065b: LOAD    C,A+C,000F FFFF FFFF FFFF
16 10                   ; 
17 STO× ST Z            ; 0x065f: LOAD    C,C<<1,000F FFFF FFFF FFFF
18 STO÷ ST T            ; 0x0660: 0x036d  NUL,B>>1,000F FFFF FFFF FFFF
19 R↓                   ; 
20 DSE 00               ; 0x0661: LOAD    NUL,B-1,000F FFFF FFFF FFFF
21 GTO 01               ; 0x0662: BRNC    0x0656
22 END                  ;

Example

41
R/S

6.40312423743


For those who want to use this, I have added the relevant files:

Archive:  ti-57.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      364  10-24-2024 09:44   Makefile
     5085  10-24-2024 09:44   cli/app_ti57.c
    11280  10-24-2024 09:44   engine/ti57.c
    83798  10-24-2024 09:44   engine/asm.c
      122  10-24-2024 09:44   engine/asm.h
---------                     -------
   100649                     5 files


Attached File(s)
.zip  ti-57.zip (Size: 19.26 KB / Downloads: 4)
Find all posts by this user
Quote this message in a reply
10-24-2024, 06:24 PM
Post: #2
RE: [TI-57] Calculate Square Root
(10-24-2024 07:47 AM)Thomas Klemm Wrote:  Based on a recent thread I took a closer look at the assembly code of the TI-57.
To analyze the code, I extended the RCL-57 emulator's command line tool to track the three registers A, B, and C.

...

This is a largely literal translation of the code for the HP-42S:
...

Example

41
R/S

6.40312423743

...


Hi Thomas,

Thank you very much for sharing all of this with us!
I love it!

Keep yourself healthy!

Laurent
Find all posts by this user
Quote this message in a reply
10-25-2024, 09:09 AM
Post: #3
RE: [TI-57] Calculate Square Root
Explanation

When the odd numbers are added, we get the squares.
But we can split the odd number \(2k+1\) into \(k\) and \(k+1\).
Therefore, each \(k\) but the last one is added twice:

0²                             0
+0+1                           1
+1+2                           4
+2+3                           9
+3+4                          16
+4+5                          25
+5+6                          36
+6+7                          49

We stop when \(41\) is overshot and shift the number by one place:

60²                           3600
+60+61                        3721
+61+62                        3844
+62+63                        3969
+63+64                        4096
+64+65                        4225

640²                          409600
+640+641                      410881

6400²                         40960000
+6400+6401                    40972801
+6401+6402                    40985604
+6402+6403                    40998409
+6403+6404                    41011216

64030²                        4099840900
+64030+64031                  4099968961
+64031+64032                  4100097024

640310²                       409996896100
+640310+640311                409998176721
+640311+640312                409999457344
+640312+640313                410000737969

6403120²                      40999945734400
+6403120+6403121              40999958540641
+6403121+6403122              40999971346884
+6403122+6403123              40999984153129
+6403123+6403124              40999996959376
+6403124+6403125              41000009765625

64031240²                     4099999695937600
+64031240+64031241            4099999824000081
+64031241+64031242            4099999952062564
+64031242+64031243            4100000080125049

640312420²                    409999995206256400
+640312420+640312421          409999996486881241
+640312421+640312422          409999997767506084
+640312422+640312423          409999999048130929
+640312423+640312424          410000000328755776

6403124230²                   40999999904813092900
+6403124230+6403124231        40999999917619341361
+6403124231+6403124232        40999999930425589824
+6403124232+6403124233        40999999943231838289
+6403124233+6403124234        40999999956038086756
+6403124234+6403124235        40999999968844335225
+6403124235+6403124236        40999999981650583696
+6403124236+6403124237        40999999994456832169
+6403124237+6403124238        41000000007263080644

64031242370²                  4099999999445683216900
+64031242370+64031242371      4099999999573745701641
+64031242371+64031242372      4099999999701808186384
+64031242372+64031242373      4099999999829870671129
+64031242373+64031242374      4099999999957933155876
+64031242374+64031242375      4100000000085995640625

640312423740²                 409999999995793315587600
+640312423740+640312423741    409999999997073940435081
+640312423741+640312423742    409999999998354565282564
+640312423742+640312423743    409999999999635190130049
+640312423743+640312423744    410000000000915814977536

6403124237430²                40999999999963519013004900

But we can also split the summands into a constant and variable part.
The sum of the variable parts is just \(b^2\), where \(b\) denotes the last digit:

\(
\begin{align}
(60+4)^2 &= 60^2 \\
&+ 2 \cdot 60 \cdot 4 \\
&+ 4^2 \\
\end{align}
\)

\(
\begin{align}
(6400+3)^2 &= 6400^2 \\
&+ 2 \cdot 6400 \cdot 3 \\
&+ 3^2 \\
\end{align}
\)

Or then in general:

\(
\begin{align}
(a+b)^2 &= a^2 \\
&+ 2ab \\
&+ b^2 \\
\end{align}
\)

Formally:

\(
\begin{align}
\sum_{k=0}^{b-1} (a+k) + (a+k+1)
&= \sum_{k=0}^{b-1} 2a + 2k+1 \\
\\
&= \sum_{k=0}^{b-1} (2a + 1) + \sum_{k=0}^{b-1} 2k \\
\\
&= 2(a+1)b + 2 \frac{b(b-1)}{2} \\
\\
&= 2ab + b² \\
\end{align}
\)



I wonder whether they were forced to implement this slightly more complicated algorithm due to David S. Cochran's patent US3576983A:
Digital calculator system for computing square roots
Find all posts by this user
Quote this message in a reply
10-25-2024, 09:35 AM
Post: #4
RE: [TI-57] Calculate Square Root
(10-24-2024 06:24 PM)Nihotte(lma) Wrote:  Thank you very much for sharing all of this with us!

I would like to pass on the thanks to HrastProgrammer, pauln and whoever else contributed to the various TI-57 emulators.
Find all posts by this user
Quote this message in a reply
10-25-2024, 07:41 PM
Post: #5
RE: [TI-57] Calculate Square Root
Amazing work. Thank you for sharing!
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: