CONTENTS | INDEX | PREV | NEXT
Stacks of Registers
All registers have a stack associated with them. This facilitates macro-
programming, especially with larger programs. When writing a macro that
needs a scratch-register, the calling program need not know what the
scratch-register is. When executing the macro, the first action is to
push all internally used registers on stack. The register is then
automatically cleared (numeric: 0). The last action would be to restore
(or: pop) the saved registers. The principle is much the same as for local
(or: stack) variables in a higher-level programming language.
7.0
Arrays
The stacks explained above are not always ideal for all programming tasks.
In many cases, the structure of a simple array is preferable. With three tricks,
the stacks are made to be usable as arrays as well:
1 - New notation for register retrieval
When the stack on a register is to be looked at as an array, it needs to
be possible to examine every item in the stack. Normally, only the top of
the stack can be accessed; QA = returns the value of the top element of
register A, no matter how many items deep the stack actually is.
Introducing a new notation for the same statement shows how the stack
can be seen as an array: QA[0] =.
As long as the stack is large enough, QA[X] can be used to retrieve
element X from the stack - effectively turning it into an array.
X is the subscript of the array.
2 - Array creation and disposal
When in need of an array of 100 elements, it is not logical to have to
perform 100 push operations. That is why the push (and pop) commands are
callable with an argument, defining how many pushes or pops to do in one
go.
3 - Array size retrieval
The array (or stack) size is always programmatically available via the
:L-command. With one caveat: the size returned assumes that no element
0 exists. There are arguments for and against this reasoning, but
this is simply how it is. The best way to think of it, is that :L returns
the number of pops that can be done. In other words: :LA ^A ESC will
remove all elements from the stack associated with register A.
As arrays and stacks are really just different ways of looking at the same
data-structure, the different notations can be used interchangeably and at
will.
The amount of overhead in memory for (every element of) an array is quite
large. It is more important than ever, when using arrays, to clean up as
much as possible after use.