Addressing And Accessing Local Data, Recursive Programming - HP RPN SCIENTIFIC WP 34S Owner's Manual

Table of Contents

Advertisement

Which solution serves you best depends on your application. You may of course combine

options. Use
local numbered registers allocated.

Addressing and Accessing Local Data, Recursive Programming

Global data take relative addresses from 0 to 111 as described on p. 154. So, relative
addresses of local data begin with 112 and may go up to 255 if 144 local registers are
allocated. The first 16 local registers and all local flags may also be directly addressed
using a dot heading the number – the arguments go from .00 to .15, corresponding to
relative addresses from 112 to 127.
This scheme allows for indirectly addressing
 a global register via a global index register (e.g. STO
 a global register via a local index register
 a local register via a global index register
 a local register via a local index register
Subroutine calls: XEQ – executed in a program – just pushes the return address on the
SRS before it branches to the target. The subroutine called will keep having access to the
caller's local data as long as it does not execute LOCR itself. As soon as it does, the
pointer to the local data is newly set, and the subroutine called cannot access the caller's
local data anymore.
RTN or POPLR – executed in a program – check if the current SRS pointer points to a
local frame (as explained on p. 156). If true then the pointer is moved above that frame,
and the SRS is searched from this point upwards for another local frame. If such a frame is
found then its pointer is stored; otherwise the pointer to the active local frame is cleared.
RTN will branch to the return address found, while POPLR will just continue execution. So
the current local frame is dropped and the next higher (or older) frame is reactivated if one
exists.
Manually executing RTN, starting a new program with XEQ, SLV, etc., or program editing
will clear the SRS and remove all local registers and flags by clearing the pointer. All such
data are lost then!
Recursive programming: Using local registers allows for creating a subroutine that calls
itself recursively. Each invocation deals with its local data only. Of course the RPN stack is
global so be careful not to corrupt it.
Here is a recursive implementation of the factorial. It is an
only, since this routine will neither set the stack correctly nor will it work for input
greater than some hundred:
76
Only arguments up to 127 are storable in an op-code, hence the limit.
WP 34S Owner's Manual
to monitor the free space available and the amount of global and
76
Any registers beyond are only indirectly addressable.
(e.g. STO
(e.g. STO
(e.g. STO
Edition 3.1
r23
23 with
< 112),
r.15
.15 with
< 112),
≥ 112), and
r47
47 with
≥ 112).
r.06
.06 with
example
for demonstration
Page 158 of 211

Advertisement

Table of Contents
loading

Table of Contents