The Museum of HP Calculators

HP Forum Archive 19

[ Return to Index | Top of Index ]

example of 30b program
Message #1 Posted by Don Shepherd on 21 Jan 2010, 1:03 p.m.

Another poster mentioned writing a program to determine the solvability of the famous 15 puzzle, based upon the ordering of the number tiles initially and the position of the empty square. Essentially, the puzzle is solvable if the row number of the empty square + the number of "inversions" is even, otherwise it is not solvable. Inversions are the number of cases in which a number to the right of a given cell are less than that cell. For example, for cell values 9, 8, 7, 10, there are 3 inversions (8<9, 7<9, and 7<8).

The 30b code is below. Prior to running, you must fill the stats registers 0-15 with the arrangement of the number tiles, and the empty space has value 0. The program uses indirect addressing to get the values of the cells.

Note: I edited the code on 1/23/2010 to use the ISG looping instruction, which reduced the size of the code from 127 bytes to 119 bytes, which is not insignificant when you only have 290 bytes total. Note also that if R0 contains a fractional part, the fractional part is ignored when you use R0 for indirect addressing, which is great.

0
sto 0      determine the row# of the empty cell (=0)
lbl 00
rcl data   get cell(r0 index)
gf 01      found the empty cell
1
sto + 0
goto 00    keep looking
lbl 01
4
rcl + 0    row# of empty cell = IP((index+4)/4)
4
/
IP
sto 3      r3 contains the number of inversions, starts with row# of empty cell
.014
sto 1      loop 1 goes from 0 to 14
lbl 02
rcl 1
1.001
+
sto 2      loop 2 goes from (loop 1 index + 1) to 15, you're going to compare two cells to check for inversions
lbl 03
rcl 2
sto 0
rcl data   get cell value in loop 2
input      necessary because next command will pop the stack and you need this value in x
gf 04      if loop 2 value is 0, ignore it
rcl 1
sto 0      get loop 1 value now
roll down  need to get loop 2 value back in x
rcl data   get cell value in loop 1
?<
gf 04      no inversion, continue loops
1          yes, an inversion was detected, add 1 to inversion count
sto + 3
lbl 04     test to end loop 2
isg 2
goto 03
isg 1      test to end loop 1
goto 02
rcl 3      done looping, if #inversions is even, puzzle is solvable, else not
2
/
fp
gf 05
rcl 3      so inversions will be in x when you get the message (fyi)
msg Unsolv.  (limited to 8 characters)
stop
lbl 05
rcl 3
msg Solvable
stop

Edited: 23 Jan 2010, 6:40 a.m. after one or more responses were posted

      
Re: example of 30b program
Message #2 Posted by hugh steers on 21 Jan 2010, 5:19 p.m.,
in response to message #1 by Don Shepherd

Hi,

nice program.

how do you "rcl data". i'm writing a program right now, but i've run out of registers 0-9. is there a secret register i could use, like this "data"?

i need just one more.

            
Re: example of 30b program
Message #3 Posted by Don Shepherd on 21 Jan 2010, 6:02 p.m.,
in response to message #2 by hugh steers

Hugh, "data" is just the stats registers, of which there are 100 and indexed by register 0. In this case, I load my numbers into the stats registers before running the program, then RCL DATA gets me the contents of whichever stats register R0 points to. Or you can RCL CASHFL and use those if you want. 100 registers, shared between stats and cash flows.

            
Re: example of 30b program
Message #4 Posted by Gene Wright on 21 Jan 2010, 6:02 p.m.,
in response to message #2 by hugh steers

"Data" are the 100 cash flow / statistics registers.

You access these indirectly by storing the location index which goes from 0 to 99 in memory register 0 and then doing a STO DATA or RCL DATA.

"Data" starts at one end of the registers while Cashfl starts at the other.

Edited: 21 Jan 2010, 6:03 p.m.

                  
Re: example of 30b program
Message #5 Posted by hugh steers on 21 Jan 2010, 6:06 p.m.,
in response to message #4 by Gene Wright

Understood thanks.

i guess i should read the manual :-)

                        
Re: example of 30b program
Message #6 Posted by Katie Wasserman on 21 Jan 2010, 6:59 p.m.,
in response to message #5 by hugh steers

I added a 30b program to the many digits of pi article, if you want to look at another example of using the Data registers.

-Katie

                              
Re: example of 30b program
Message #7 Posted by hugh steers on 21 Jan 2010, 7:06 p.m.,
in response to message #6 by Katie Wasserman

Thanks. looks like a neat program. right now my unit is stuffed full of half-working efforts :-)

Actually, there are some serious drawbacks to the global nature of the labels. is that why your labels are chosen like you have. like 81, 82 etc for program 8 or similar?

                                    
Re: example of 30b program
Message #8 Posted by Gene Wright on 21 Jan 2010, 7:58 p.m.,
in response to message #7 by hugh steers

well, tradeoffs again

With 10 spots in the program catalog, imagine the complaints if each program were limited to 10 labels (10 x 10 = 100) since only 100 were available?

With 290 bytes available, I really don't think 100 labels all of which are global is that bad. :-)

                                          
Re: example of 30b program
Message #9 Posted by hugh steers on 21 Jan 2010, 8:05 p.m.,
in response to message #8 by Gene Wright

Indeed. that's why i'll have to stop calling them LBL 01, LBL 02 etc. its not the amount, but choosing separate namespaces.

i like the idea of using program-number+label as a convention as much as possible to avoid conflicts.

but like you say, it would be wrong to enforce this. because sometimes 10 isnt enough.

                                                
Re: example of 30b program
Message #10 Posted by Katie Wasserman on 22 Jan 2010, 1:32 a.m.,
in response to message #9 by hugh steers

Yes, I try to write 30b program using labels in the form: prog_number+sequence_number if possible. Also, I usually assign them to the shift-hold number key. Shift-hold 8, would run the program in location 8, for example.

As far as showing programs on this forum, I like Don's short-cut method, but it doens't capture all that you might be programming. Functions on the Math menu certainly make sense to abbreviate, but I'm less sure about things like clearing various memory elements using commands on the Reset menu and Memory menu. (Disturbingly you can clear parts of memory using the Memory menu which would normally only be used to read how much memory you've used, or to review the basic 0-9 registers contents.)

-Katie

                                                      
Re: example of 30b program
Message #11 Posted by bill platt on 22 Jan 2010, 7:52 a.m.,
in response to message #10 by Katie Wasserman

Where are you finding the 30b for sale?

Are they made in China?

Edited: 22 Jan 2010, 7:52 a.m.

                                                            
Re: example of 30b program
Message #12 Posted by hugh steers on 22 Jan 2010, 1:59 p.m.,
in response to message #11 by bill platt

They are made in china. the 30b keyboard is _much_ better than the 20b.

                                                                  
Re: example of 30b program
Message #13 Posted by bill platt on 22 Jan 2010, 2:04 p.m.,
in response to message #12 by hugh steers

I sooooo wish they'd not make these things in China.

What's wrong with Malaysia, or Indonesia, or heck, even Vietnam?

                        
Re: example of 30b program
Message #14 Posted by Walter B on 22 Jan 2010, 1:37 a.m.,
in response to message #5 by hugh steers

Hi gurus,

nice watching you running circles around us <envy>. Is the manual available anywhere? So plain folks are allowed reading it, too? d;-)

                              
Re: example of 30b program
Message #15 Posted by Don Shepherd on 22 Jan 2010, 6:29 p.m.,
in response to message #14 by Walter B

HP, are you listening? How about an online version of the 30b manual so people can read about its technical capabilities?


[ Return to Index | Top of Index ]

Go back to the main exhibit hall