Showing posts with label storage arithmetic. Show all posts
Showing posts with label storage arithmetic. Show all posts

Saturday, January 10, 2026

RPN: A Comparison of Programming Methods: Stack vs. Storage Arithmetic with the HP 32SII

RPN: A Comparison of Programming Methods: Stack vs. Storage Arithmetic with the HP 32SII


News: The RPN series will continue for 2026: Every second Sunday!


Today we will compare two methods to tackle mathematical problems with the HP 32SII. The code presented here will also work with the HP 32S original and the Swiss Micros DM32.


Method 1 (left column): Use of the stack and when necessary, variables to store immediate results.


Method 2 (right column): Use of storage arithmetic to most, if not all, of the arithmetic operations.



What is Storage Arithmetic?


Storage arithmetic executes an arithmetic operation while simultaneously storing the result in the variable.


In all the examples, assume the variable Z starts with the value of 10.


STO+: Storage Addition

Adds the value in the display to the value stored in the variable and stores the result. Outside of RPN and Hewlett Packard calculators, storage addition is the most common storage arithmetic function. On four function calculators and adding machines, storage arithmetic is symbolized with M+. On Texas Instruments scientific calculators, such as the TI-30Xa and the classic TI-30 series, storage addition occurs with the SUM key.


2 STO+ Z adds 2 to Z. The value of Z is now 12.


In Python, the general syntax for storage addition:

var+=<expression>


STO-: Storage Subtraction


Subtracts the value in the display from the value stored in the variable and stores the result. On four function calculators and adding machines, storage arithmetic is symbolized with M-. If you have a TI-55 (1970s version), TI-57 (1970s version), TI-58, TI-59, or T-66, storage subtraction is executed by INV SUM.


2 STO- Z subtracts 2 from Z. The value of Z is now 8.


In Python, the general syntax for storage subtraction:

var-=<expression>



STO×: Storage Multiplication


Multiplies the value in the display from the value stored in the variable and stores the result. If you have a TI-55 (1970s version), TI-57 (1970s version), TI-58, TI-59, or T-66, storage subtraction is executed by Prod (or Prd).


2 STO× Z multiplies 2 to Z. The value of Z is now 20.


In Python, the general syntax for storage multiplication:

var*=<expression>



STO÷: Storage Multiplication


Multiplies the value in the display from the value stored in the variable and stores the result. If you have a TI-55 (1970s version), TI-57 (1970s version), TI-58, TI-59, or T-66, storage subtraction is executed by INV Prod (or Prd).


2 STO÷ Z divides Z by 2. The value of Z is now 5.


In Python, the general syntax for storage multiplication:

var/=<expression>


Example Problems


For fairness, both methods store the final result in Z. Only storage arithmetic is used because not all RPN calculators have recall arithmetic.


Problem 1: (1 + √5) ÷ 2 ≈ 1.61803398875


LBL A

5

SQRT

1

+

2

÷

STO Z

RTN


15.0 bytes

LBL B

1

STO Z

5

SQRT

STO+ Z

2

STO÷ Z

RCL Z

RTN


15.0 bytes


Problem 2: 4² + 3 × 4 – 5 = 4 × (4 + 3) – 5 = 23


LBL C

4

STO X

3

+

RCL X

×

5

-

STO Z

RTN


15.0 bytes

LBL D

4

STO X

STO Z

3

STO+ Z

4

STO× Z

5

STO- Z

RCL Z

RTN


18.0 bytes



Problem 3: √(8.5² + 9.6²) ≈ 12.8222462931


LBL E

8.5

x²

9.6

x²

+

SQRT

STO Z

RTN


29.5 bytes

LBL F

8.5

STO Z

STO× Z

9.6

STO Y

STO× Y

RCL Y

STO+ Z

RCL Z

SQRT

STO Z

RCL Z

RTN


37.0 bytes


Problem 4: (7 × 12) ÷ (7 + 12) ≈ 4.42105263158


LBL G

7

STO X

12

STO Y

×

RCL X

RCL Y

+

÷

STO Z

RTN


18.0 bytes

LBL H

7

STO X

STO Y

12

STO× X

STO+ Y

RCL X

RCL Y

÷

STO Z

RTN


19.5 bytes


Problem 5: 1 ÷ (1 ÷ 4.5 + 1 ÷ 8 + 1 ÷ 6.7) ≈ 2.01419624217


LBL I

4.5

1/x

8

1/x

6.7

1/x

+

+

1/x

STO Z

RTN


35.5 bytes

LBL J

4.5

1/x

STO Z

8

1/x

STO+ Z

6.7

1/x

STO+ Z

RCL Z

1/x

STO Z

RCL Z

RTN


38.5 bytes


Problem 6: 2 × (10.3 – 4.9) + 3 × (5.4 – 2) = 21


LBL K

10.3

4.9

-

2

×

5.4

2

-

3

×

+

STO Z

RTN


45.0 bytes

LBL L

10.3

STO Y

4.9

STO- Y

2

STO× Y

5.4

STO Z

2

STO- Z

3

STO× Z

RCL Y

STO+ Z

RCL Z

RTN


49.5 bytes



Findings


I like storage arithmetic. However from these results, storage arithmetic uses a bit more memory than merely using the stack. Storage arithmetic can be handy dandy technique in programming, not just in keystroke programming but other programming languages like Python.



Eddie


All original content copyright, © 2011-2026. 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.


The author does not use AI engines and never will.


Saturday, April 12, 2025

RPN with HP 15C & DM32: Stack Register Arithmetic

RPN with HP 15C & DM32: Stack Register Arithmetic


Got a treat for today folks. First...


14 YEARS!!


On the 16th, it will be 14 years since my blog started!!!! Thank you so much for your support – the blog is one of my joys of life.


Today is another installment of RPN with HP 15C & DM32, I hope you are enjoying this new monthly series, currently every second Saturday of the month.



Stack Register Arithmetic


As we know, nearly all Hewlett Packard (HP) and I think all Swiss Micros (SM) calculators that operate on Reverse Polish Notation (RPN) or Reverse Polish Lisp (RPL, think HP 48 and 50g), a feature that we can calculate an arithmetic operation (+, -, ÷, ×) directly on any number stored in any memory register. It’s a very handy feature indeed.


On the HP 41C, DM41, HP 42S, and DM42 series of calculators, that ability extend to the stack registers themselves (X, Y, Z, T, L (LastX)). To do this, press [ STO ], the required arithmetic operation, the decimal point [ . ], and the appropriate key.


Example:


The current stack is set as:


T: 11

Z: 16

Y: 10

X: 5


Note the 5 in the X stack. We can use the contents of the X stack to do operations on the other levels without “disturbing” the other levels.


Add 5 to stack Z without having to disturb the stack. ST+ Z (41), STO+ ST Z (42) returns:


T: 11

Z: 21

Y: 10

X: 5


Note that the entire stack except Z remains the same.


Multiply stack T by 5. 5 is in the X stack already. ST* T (41), STO× ST T (42) returns:


T: 55

Z: 21

Y: 10

X: 5


Pretty neat, right?


In summary:


STO+ SL: new SLV= old SLV + X

STO- SL: new SLV = old SLV – X

STO× SL: new SLV = old SLV × X

STO÷ SL: new SLV = old SLV ÷ X


where:

X = value in stack level X (display on one-line calculators)

SLV = stack value level (X, Y, Z, T, L)


The 15C and 32S series do not have a native way to do this, but today I will present a way to mimic these powerful stack storage arithmetic on levels X, Y, Z, and T. Most of them will not require the use of an outside memory register (i.e. R0 or R1 for the 15C series, or A or Z for the 32S series).


These techniques were tested on a Hewlett Packard HP 15C and Swiss Micros DM32. They probably would work on the HP 12C (or equivalent) as well, just substitute the roll up (R↑) with three roll down (R↓ R↓ R↓) commands when encountered.


FYI, the 42 series also has recall arithmetic on stack levels, which returns on stack X.



The Algorithms


For the following algorithms, let {OP} stand for the arithmetic operation (+, -, ×, ÷). Let the hash symbol, {#}, stack for a register of your choice (i.e. R0 for 15 or A for 32).


Storage Arithmetic on Stack X


R↑

STO {#}

R↓

ENTER

{OP}

RCL {#}

R↓




Stack Illustration with STO+ X (it will be similar with the rest of the arithmetic operations)


t

z

z

t

z

z

z

t

z

y

y

z

y

z

y

z

y

x

x

y

x

y

x + x

y

x

t

t

x

x

x + x

t

x + x

START

R↑

STO {#}

R↓

ENTER

+

RCL {#}

R↓


Shortcuts:

STO- X: Clx

STO× X: x^2 (specifically, the square function)


The next set will not require a separate memory register.


Example:


40

30

30

40

30

30

30

40

30

20

20

30

20

30

20

30

20

10

10

20

10

20

20

20

10

40

40

10

10

10 + 10 = 20

40

20

START

R↑

STO {#}

R↓

ENTER

+

RCL {#}

R↓





Storage Arithmetic on Stack Y


This is by far the easiest.


{OP}

LAST X


Stack Illustration with STO+ Y (it will be similar with the rest of the arithmetic operations)


t

t

t

z

t

z

y

z

y + x

x

y + x

x

START

+

LAST X



Example:


40

40

40

30

40

30

20

30

30

10

20 + 10 = 30

10

START

+

LAST X




Storage Arithmetic on Stack Z


x<>y

R↓

{OP}

LAST X

R↑

x<>y


Stack Illustration with STO+ Z (it will be similar with the rest of the arithmetic operations)


t

t

y

y

y

t

t

z

z

t

y

t

z +x

z + x

y

x

z

t

z + x

x

y

x

y

x

z + x

x

y

x

START

x<>y

R↓

+

LAST X

R↑

x<>y



Example:


40

40

20

20

20

40

40

30

30

40

20

40

40

40

20

10

30

40

40

10

20

10

20

10

30 + 10 = 40

10

20

10

START

x<>y

R↓

+

LAST X

R↑

x<>y




Storage Arithmetic on Stack T


R↑

x<>y

{OP}

LAST X

x<>y

R↓



Stack Illustration with STO+ T (it will be similar with the rest of the arithmetic operations)


t

z

z

z

z

z

t + x

z

y

y

z

y

y

z

y

x

t

y

t + x

x

y

x

t

x

t + x

x

t + x

x

START

R↑

x<>y

+

LAST X

x<>y

R↓


Example:


40

30

30

30

30

30

50

30

20

20

30

20

20

30

20

10

40

20

50

10

20

10

40

10

40 + 10 = 50

10

50

10

START

R↑

x<>y

+

LAST X

x<>y

R↓



Until next time,


Eddie


All original content copyright, © 2011-2025. 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.


Python in Numworks: Duplicating and Grayscale

Python in Numworks: Duplicating and Grayscale All three scripts presented today use the math, random, and the Numworks specific ...