HP Forums

Full Version: (SR-52) Binary-to-Decimal conversion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
  Binary-to-decimal number conversion, often needed when working with digital equipment, can require laborious raising to powers and addition. This program, designed for an SR-52 calculator, provides a convenient method of converting 8-, 16-, 24-, and 32-bit words to their decimal equivalents.
  To use the program, binary words are entered into the calculator eight bits at a time, most significant bit first. (Since the SR-52 has a 10-digit mantissa, the program ignores the first two digits entered and only operates on the last eight binary digits.) The user then presses the SR-52's user-defined key A, and the program displays the decimal.
  Here's a typical 8 -bit conversion:

ignored
┌┐
1 1 1 1 1 0 1 0 1 0 ← 1) Enter binary number in display
      └────────┘
            8-bits         ← 2) Press A Key

                    2 3 4  ← 3) Calculator Displays decimal equivalent

  To obtain 16-, 24-, and 32-bit conversions, do the above operation for the first eight bits, but for each additional eight bits entered, press the user-defined key B, which will sum that 8-bit conversion with all previous conversions, to a maximum of 32 bits.
  The conversion algorithm is shown here in flowchart
[attachment=8020]
and SR-52 program-listing form.
 Location        Codes                    Keys
000 - 003  46 11 42 00    *LBL   A        STO    0
004 - 007  00 00 42 00    0        0        STO    0
008 - 011  03 42 00 04    3        STO    0        4
012 - 015  42 00 05 08    STO    0        5        8
016 - 019  44 00 05 01    SUM    0        5        1
020 - 023  94 44 00 04    +/-      SUM   0        4
024 - 027  46 79 01 44    *LBL   *6      1         SUM
028 - 031  00 04 43 00    0        4        RCL     0
032 - 035  00 55 01 00    0        ÷        1        0
036 - 039  95 42 00 01    =        STO    0        1
040 - 043  51 78 43 00    SBR    *5       RCL     0
044 - 047  04 75 43 00    4        -        RCL     0
048 - 051  05 95 42 00    5        =        STO    0
052 - 055  06 43 00 01    6        RCL     0        1
056 - 059  75 43 00 02    -        RCL     0        2
060 - 063  95 90 68 43    =        *if0    *8       RCL
064 - 067  00 06 90 67    0        6        *if0     *7
068 - 071  02 45 43 00    2        y"       RCL     0
072 - 075  04 95 44 00    4        =        SUM    0
076 - 079  03 43 00 02    3        RCL     0        2
080 - 083  42 00 00 41    STO     0       0        GTO
084 - 087  79 46 67 43    *6      *LBL    *7      RCL
088 - 091  00 03 81 46    0        3        HLT     *LBL
092 - 095  68 43 00 06    *8      RCL     0        6
096 - 099  90 67 43 00    *if0    *7       RCL     0
100 - 103  02 42 00 00    2        STO    0        0
104 - 107  41 79 46 78    GTO    *6      *LBL    *5
108 - 111  75 93 05 54    -        .         5        )
112 - 115  57 00 52 22    *fix     0        EE      INV
116 - 119  52 22 57 42    EE       INV     *fix    STO
120 - 123  00 02 56 46    0        2        *rtn    *LBL
124 - 127  12 42 00 00    B        STO    0        0
128 - 131  41 00 01 05    GTO    0        1        5
  It works by performing repeated divisions by 10, splitting the dividend into integral and fractional parts, and then looking to see if the fractional part is a 1 or 0. If it is a 1, the program raises 2 to appropriate power and sums it into register 03. After the program has looped through eight bits, it recalls register 03 and halts.

source: RCA Engineer, Engineering and Research Notes, Binary-to-decimal conversion program for a programmable calculator, 1977-08/09, pg.76, A.R. Campbell

BEST!
SlideRule
TI-57

Code:
00   32 0  :  STO 0
01   32 1  :  STO 1
02     08  :  8
03   32 2  :  STO 2
04     19  :  C.t
05   86 0  :  Lbl 0
06   33 1  :  RCL 1
07     45  :  ÷
08     01  :  1
09     00  :  0
10     85  :  =
11     49  :  Int
12     66  :  x=t
13   51 1  :  GTO 1
14   32 1  :  STO 1
15     55  :  ×
16   33 2  :  RCL 2
17   34 2  :  SUM 2
18     85  :  =
19  -34 0  :  INV SUM 0
20   51 0  :  GTO 0
21   86 1  :  Lbl 1
22   33 0  :  RCL 0
23     81  :  R/S

Example

1101
RST
R/S

13.

11011001
RST
R/S

217.


HP-25

Code:
01: 23 00    : STO 0
02: 23 01    : STO 1
03: 08       : 8
04: 23 02    : STO 2
05: 24 01    : RCL 1
06: 01       : 1
07: 00       : 0
08: 71       : /
09: 14 01    : f INT
10: 15 71    : g x=0
11: 13 18    : GTO 18
12: 23 01    : STO 1
13: 24 02    : RCL 2
14: 23 51 02 : STO + 2
15: 61       : *
16: 23 41 00 : STO - 0
17: 13 05    : GTO 05
18: 24 00    : RCL 0
19: 13 00    : GTO 00

Example

CLEAR PRGM

1101
R/S

13.00

11011001
R/S

217.00




(01-17-2020 04:49 PM)SlideRule Wrote: [ -> ]… provides a convenient method of converting 8-, 16-, 24-, and 32-bit words to their decimal equivalents.

I'm leaving that part as an exercise to the reader.
Great implementation for the TI-57 and HP-25.

It took me a while to understand where the 8 was coming from (hint: 10 - 2 = 8) and more generally why this method works at all.

Nit: in the TI-57 program, "0 x:t" can be replaced with "C.t" to save one step.
(06-17-2022 12:47 AM)pauln Wrote: [ -> ]It took me a while to understand where the 8 was coming from (hint: 10 - 2 = 8) and more generally why this method works at all.

I've explained it here in case of the decimal-to-binary conversion.
But it works similarly if 2 and 10 are swapped.

Here are some programs for the general case n-to-10 or 10-to-n:

(06-17-2022 12:47 AM)pauln Wrote: [ -> ]Nit: in the TI-57 program, "0 x:t" can be replaced with "C.t" to save one step.

Good catch. Thank you for the notification.
I have adjusted the listing accordingly.

The TI-57 was the first calculator I wrote programs for.
A colleague at school kindly loaned it to me for a while.
But in the end I got an HP-41CV and I have no regrets.
Here's a variant for the HP-25 that uses one more step but only one instead of three registers:
Code:
01: 23 00    : STO 0
02: 08       : 8
03: 21       : x<->y
04: 01       : 1
05: 00       : 0
06: 71       : /
07: 14 01    : f INT
08: 15 71    : g x=0
09: 13 19    : GTO 19
10: 21       : x<->y
11: 31       : ENTER
12: 31       : ENTER
13: 51       : +
14: 22       : Rv
15: 61       : *
16: 23 41 00 : STO - 0
17: 22       : Rv
18: 13 04    : GTO 04
19: 24 00    : RCL 0
20: 13 00    : GTO 00
Another way of looking at this method is to notice that it transforms the value in register 0 from:

\(a_0 + a_1 \cdot 10 + \cdots + a_n \cdot 10^n\)

into

\(a_0 + a_1 \cdot 2 + \cdots + a_n \cdot 2^n\)

To do so, it transforms \(10^k\) into \(2^k\) using the following general identity:

\(a^k - b^k = (a - b)(a^{k-1} + a^{k-2} \cdot b + \cdots + b^{k-1})\)

Applied to a = 10 and b = 2, we get:

\(10^k - 2^k = 8 \cdot (10^{k-1} + 10^{k-2} \cdot 2 + \cdots + 2^{k-1})\)

This explains the 8 at the beginning of the program as well as the values in register 2 (initially 8 and then 16, 32, 64, ...).
(06-17-2022 10:38 PM)pauln Wrote: [ -> ]I'm guessing this forum doesn't support Latex-like formatting for math expressions ?

A search in the Test forum with the keyword latex gives currently the following list:
Examples

\(a_0 + a_1 \cdot 10 + \cdots + a_n \cdot 10^n\)

\[a_0 + a_1 \cdot 2 + \cdots + a_n \cdot 2^n\]

\(
\begin{align}
a^k - b^k &= (a - b)\left(a^{k-1} + a^{k-2} \cdot b + \cdots + b^{k-1}\right) \\
\\
10^k - 2^k &= 8\left(10^{k-1} + 10^{k-2} \cdot 2 + \cdots + 2^{k-1}\right) \\
\end{align}
\)

Hint: You can see the code if you quote a post.
(06-18-2022 05:23 AM)Thomas Klemm Wrote: [ -> ]Hint: You can see the code if you quote a post.

Thanks! I fixed the original post.
Reference URL's