Swapping Registers on HP calculators
|
03-27-2024, 12:50 PM
(This post was last modified: 03-27-2024 12:54 PM by n1msr.)
Post: #1
|
|||
|
|||
Swapping Registers on HP calculators
I recently upgraded some older HP calcs (HP-65, HP-67, HP-41CX with USB battery) and used this opportunity to compare speed and implementation of a program to enumerate the permutations of up to 9 items (using digits 1 to 9, since the early machines have no alpha characters). I lazily took an algorithnm that had been implemented in Python. It requires two one-dimensional arrays (actually lists in Python). The algorithm needs to be able to swap two items in one of the lists, and this made me wonder if I am missing a tip/trick to doing this fast and/or using the fewest steps possible with the stack and some of the calculator capabilities. For example, on the HP-41CX the stack levels can be addressed programmatially, meaning you don't have to store one of the swapped values in a "temp" register. Because of stack lift (or not) with certain functions, using stack manipulation and not storing the "temp" value in a register requires care.
On the HP-67 my implementation finally worked - helped by Tony Nixon's 67 CPU upgrade running in turbo mode, coupled with his emulator that allowed me to see multiple registers at once (I needed both Primary and Secondary registers on the 67), plus the stack / flags as they changed. The 41CX is OK also, but no emulator this time, so harder/slower to debug The DM41X OK too (same as 41CX), and much faster. Now I am using the 15c+ (the 15c LE prototype from 2010) and I am trying to use its matrix capability to hold the two arrays (each one dimensional) and need to be able to swap array elements in one of the arrays (list a[] in the Python below). When I look at the number of program steps I use, it seems inefficient. I cannot use the auto-incrementing matrix indices in this implementation - I must be able to swap two non-sucessive elements. (PS is it possible to turn on/off USER mode on the 15c programmatically? - I haven't found a way.) For now I am just interested to know if there are known register swapping technioques from the relatively early models. I have copied the Python algorithm below in case anyone really is curious. For brevity I have not copied any programmed calculator steps. I have highlighted the Python line that does the swap (line 12). a = ['a', 'b', 'c', 'd'] # items to be permutated N = len(a) p = [num for num in range(N+1)] i = 1 print(p,a) while i < N: p[i] -= 1 if i % 2 == 1: j = p[i] else: j = 0 a[j], a[i] = a[i], a[j] <-- SWAP the ith and jth elements print(p,a) # output current permutation i = 1 while p[i] == 0: p[i] = i i += 1 Thank you, Mark |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)