The Museum of HP Calculators

HP Forum Archive 18

[ Return to Index | Top of Index ]

A reversed numbering scheme of the HP48's RPN stack
Message #1 Posted by Khanh-Dang Nguyen Thu-Lam on 13 Jan 2009, 11:52 a.m.

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


[ Return to Index | Top of Index ]

Go back to the main exhibit hall