Micro challenge: improve this RPN code
12-18-2019, 04:15 PM
Post: #1
 David Hayden Senior Member Posts: 387 Joined: Dec 2013
Micro challenge: improve this RPN code
While waiting for my Woodstock LP, I've been playing with my 29C and writing my own versions of some of the programs in the solutions packs. One of them, Gaussian Quadrature on page 22 of the Math pack takes numbers "a" and "b" on the stack in Y and X, and stores (a+b)/2 and (a-b)/2 in two registers like so:
Code:
STO 7 STO 8 X<>Y ST+ 7 ST- 8 2 ST/ 7 ST/ 8
I look at my code and think "there must be a better way." Can you improve it?

The goal is for 29C code only. I won't list all the 29C instructions, but here are some important differences with earlier and later models:
Stack manipulation: X<>Y, RUP and RDN only. No direct stack STO/RCL
It has STO arithmetic, but not RCL arithmetic
It does have sigma+ and sigma-.
It doesn't matter which registers the results go in.

Looking forward to smacking my head and saying "why didn't I think of that!!"

Dave
12-18-2019, 04:53 PM
Post: #2
 Albert Chan Senior Member Posts: 1,785 Joined: Jul 2018
RE: Micro challenge: improve this RPN code
I don't know 29C, but seems you can save code by (a+b)/2 = b + (a-b)/2

Example, for HP12C, a=10, b=2

10 Enter 2

STO 1 ﻿ ﻿ ﻿ ﻿ ; M1=b
-
2
/
STO 2 ﻿ ﻿ ﻿ ﻿ ; M2 = (a−b)/2
STO+ 1 ﻿ ﻿ ﻿; M1 = (a+b)/2
12-18-2019, 07:40 PM
Post: #3
 rprosperi Super Moderator Posts: 5,190 Joined: Dec 2013
RE: Micro challenge: improve this RPN code
Albert "Identity Man" Chan strikes again!

And having shared that identify, we can all scratch our heads, again, and wonder... 'how does he keep all those memorized?'

--Bob Prosperi
12-18-2019, 07:45 PM
Post: #4
 hth Senior Member Posts: 410 Joined: Mar 2014
RE: Micro challenge: improve this RPN code
Using the rewrite idea from Albert:

Code:
- LASTX X<>Y 2 / + LASTX

Results are in X and Y.
12-18-2019, 08:58 PM
Post: #5
 David Hayden Senior Member Posts: 387 Joined: Dec 2013
RE: Micro challenge: improve this RPN code
SMACK!

Just as promised. The funny thing is that I kept looking to use a slightly different identy: (a-b)/2 = (a+b)/2 - b

Thanks,
Dave
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)