A reversed numbering scheme of the HP48's RPN stack



#2

Hi all,

On HP48 calculators or similar, the stack level in which objects are pushed is the first. When you push another object, the object at first level will go to level two, while the currently pushed object will go to level one.
This is practical for everyday use of the calculator but not for
programming. Anyone who has written a RPL program for HP48 may know how cumbersome the stack is for storing things. You always have to wonder which level your data is; as this level change every time you push an objet to the stack or pop one, this quickly become unbearable.

I have just thought about numbering the stack levels "upside down", that is, the level in which you push object is the one with the highest number; the "oldest" level is numbered with one. As you may notice, pushing or DROPing objects does not change numbers of other objects.

As an illustration, I wrote a small utility which provides the RSIZE, RRCL and RSTO commands. These commands may recall you the SIZE, RCL and STO commands of the HP41 (I wrote them precisely because I like the RPN way of the HP41).

I called this small program REGS. Here are the source. The comments follow. On my HP50g, BYTES gives #D54Fh and 626.5:

%%HP: T(3)A(D)F(.);
DIR
RSIZE
\<< 1. RCHKNARGS \-> N
\<< N ~RSIZE -
IF DUP 0. \>=
THEN 1. SWAP
START 0.
DEPTH ~RSIZE - ROLLD
NEXT
ELSE NEG 1.
SWAP
START DEPTH N
- ROLL DROP
NEXT
END N '~RSIZE'
STO
\>>
\>>
RRCL
\<< 1. RCHKNARGS \-> N
\<<
IF N ~RSIZE \>= N
0. < OR
THEN # 203h
DOERR
ELSE DEPTH
~RTOP - N - PICK
END
\>>
\>>
RSTO
\<< 2. RCHKNARGS \-> N
\<<
IF N ~RSIZE \>= N
0. < OR
THEN # 203h
DOERR
ELSE DEPTH
~RTOP - N - \-> n
\<< n ROLL DROP
n 1. - ROLLD
\>>
END
\>>
\>>
RDEPTH
\<< DEPTH ~RTOP -
~RSIZE -
\>>
RCHKNARGS
\<<
IF RDEPTH 1. - >
THEN # 201h DOERR
END
\>>
~RSIZE 0.
~RTOP 0.
END

== Usage:

RRCL ( n --> obj )
Recall the content of the n-th register (betwen 0 and ~RSIZE-1).

RSTO ( obj n --> )
Put the specified object in the n-th register.

RSIZE ( n --> )
Set the size of the registers area to the integer n. This size is internally stored in the global variable '~RSIZE' (you may not modify it).

RDEPTH ( --> n )
Returns the number of available levels in the stack. The levels that are part of the registers area are considered as unavailable. (And you may not want to modify those without using RSIZE, RRCL or RSTO.)


== Notes:

The '~RTOP' global variable specifies the level from the last one at which the registers area starts. You usually want to let it to the value 0, so that the registers area is at the very top levels of the stack. If you want to set this variable, do it before using RSIZE.

Of course, these programs were written only to illustrate how easy the reversed numbering scheme of the stack is (and because the HP41's RPN way is nice). Indeed, note that the RRCL is as simple as « DEPTH ~RTOP - N - PICK », all other commands are for error checking.

If anyone like these commands, I may write and gather them in a SysRPL library (with other commands such as RSTO+, RSTO-, etc).

Regards,

Khanh-Dang


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP 50g - select characters on the stack, copy/paste Sean Freeman 7 431 11-20-2013, 07:11 AM
Last Post: Sean Freeman
  Prime: Placing more than 1 item on the RPN stack in a single program? John Colvin 4 300 11-19-2013, 08:59 AM
Last Post: Miguel Toro
  emu48 - copy stack doesn't work (as expected) Thomas Radtke 2 278 11-11-2013, 02:19 PM
Last Post: Thomas Radtke
  HP Prime Stack operations from within a program John Colvin 1 176 11-08-2013, 09:45 PM
Last Post: Helge Gabert
  [PRIME] RPN: another attempt at returning more than one value to the RPN stack Marcus von Cube, Germany 5 358 11-05-2013, 02:44 AM
Last Post: Marcus von Cube, Germany
  Prime: Anyway to refresh stack? kris223 5 294 10-16-2013, 05:09 PM
Last Post: kris223
  hp prime - sending program results to the stack giancarlo 6 344 10-15-2013, 02:00 AM
Last Post: Giancarlo
  Colour scheme? Geoff Quickfall 21 658 10-02-2013, 10:35 PM
Last Post: LHH
  HP Prime - RPN stack access from programs? Mike Mander (Canada) 10 393 09-30-2013, 11:20 AM
Last Post: steindid
  WP-34S: Stack after divide by 0 Marcel Samek 4 214 08-24-2013, 11:57 PM
Last Post: Paul Dale

Forum Jump: