The Museum of HP Calculators

HP Forum Archive 13

[ Return to Index | Top of Index ]

Sorting on a 12C
Message #1 Posted by Katie on 26 Oct 2003, 11:11 p.m.

In playing around with the good old 12C I realized that it has indirect register access. You can use the CFj and RCL CFj functions to store and recall indirectly through the n register. Furthermore it does a pre-increment (for CFj) and post-decrement (for RCL CFj) before the store and recall, respectively.

So just to see if this was at all useful I wrote a simple (bubble) sort program in only 21 bytes! It's easy to use, just fill up registers 0 - n with your data and call the program with n in the x register.

Here it is:

01      STO i                   i = maximum register
02      1
03      STO n           A:      n = 1
04      RCL CFj         B:      y = A(n)
05      RCL CFj                 x = A(n-1)
06      x<=y                    if (x <= y)
07      x<->y                     swap x,y
08      CFj                     A(n-1) = x
09      Rv
10      CFj                     A(n) = y
11      RCL i
12      RCL n
13      1
14      +                       n = n+1
15      x<=y                    if (n <= i)
16      GTO 03                    goto B
17      2
18      -                       i = i-1
19      x=0                     if (i = 0)
20      GTO 00                    return
21      GTO 01                  else
                                  goto A

Edited: 27 Oct 2003, 10:47 a.m. after one or more responses were posted

      
Re: Sorting on a 12C
Message #2 Posted by tony on 26 Oct 2003, 11:22 p.m.,
in response to message #1 by Katie

BRILLIANT!!!!!!!!!!!!!!!!!! I never knew about this indirect addressing!! Thanks for posting this masterpiece Katie.

      
Re: Sorting on a 12C
Message #3 Posted by Namir Shammas on 27 Oct 2003, 7:01 a.m.,
in response to message #1 by Katie

Katie,

You are sharp!!!!!! That sorting routine is smart!!!!

Namir

      
Re: Sorting on a 12C
Message #4 Posted by Patrick on 28 Oct 2003, 3:35 a.m.,
in response to message #1 by Katie

Very nice, Katie. It only took... what? ... 21 years for someone to realize this? He, he, he...

            
Re: Sorting on a 12C
Message #5 Posted by Valentin Albillo on 29 Oct 2003, 10:53 a.m.,
in response to message #4 by Patrick

Patrick wrote:

"Very nice, Katie. It only took... what? ... 21 years for someone to realize this?"

Not to belittle Katie's amazing sort in 21 steps, but seems to me you're implying that the possibility of using CFj in the HP-12C to achive indirect addressing of registers is a new discovery, unrealized for 21 years.

If that's what you mean, that's not the case. My "Serendipitous Solver" article, published in Datafile V21N2, (March/April 2002), 18 months ago, did include a program to evaluate and find roots of arbitrary polynomials in the HP-12C which already uses precisely this technique to indirectly access the storage registers where the coefficientes will be stored (see lines 05 and 12, as well as the description of the uses of CFj and CF0 in the article). I've seen no previous examples of this technique being used elsewhere, but it's quite possible that I wasn't the first to use it for practical purposes. In any case, it doesn't qualify as a new discovery.

All the same, kudos to Katie for its magnificent sort program. It would have made a superbly good challenge for all of us HP-12C lovers ("Given a set of n values stored in registers R0 to Rn, try and write a sorting routine in 21 bytes or less") :-) !!

Best regards from V.

                  
Re: Sorting on a 12C
Message #6 Posted by Katie on 29 Oct 2003, 6:22 p.m.,
in response to message #5 by Valentin Albillo

Valentin,

I hadn't seen your polynomial solver before, it's a wonderful use of indirect storage on the 12C! I did some looking around on the web when I first realized the usefulness of the CFj functions but couldn't find any reference to using them for indirect access. I was pretty surprised since there are so many clever people out there playing around with these things. I'll bet that someone, somewhere has been using this "feature" for 20 years and has even written exactly the same sort program that I did. I've come to realize that nothing in computer science is really unique. (My philosophy after doing this stuff for far too long.)

-Katie

      
Re: Sorting on a 12C
Message #7 Posted by David Smith on 28 Oct 2003, 3:53 p.m.,
in response to message #1 by Katie

Hi Katie,

Now go and implement my favorite sort... heapsort.

            
Re: Sorting on a 12C
Message #8 Posted by Katie on 29 Oct 2003, 12:21 a.m.,
in response to message #7 by David Smith

David,

You might be able to write a heap sort on the 12C (or more likely the 12C platinum) if it had just a bit more register memory. Amazingly, you can structure register memory as a linked list on the 12C using the byte of memory where the cash flow counts are stored (Nj) and, of course, you can access this indirectly. There's a complication with this, however. When you use CFj it automatically sets Nj to 1 overwriting what you ave stored there, bummer.

-Katie


[ Return to Index | Top of Index ]

Go back to the main exhibit hall