Contents / Reference section / Previous chapter / Next chapter / Index
Indirection operators are not normally available on personal computers. They enable the user to read and write to memory in a far more flexible way than by using PEEK and POKE. They are intended for those using the computer's assembler or those wishing to create their own data structures. There are three indirection operators:
|Name||Purpose||No. of bytes affected|
|? query||byte indirection operator||1|
|! pling||word indirection operation||4|
|$ dollar||string indirection operator||1 to 256|
For the sake of illustration let us play with memory around location &2000 (that is 2000 hex - an appreciation of hex numbers is essential. If you don't understand hex then you will not need to use indirection operators). Let us set the variable M to &2000
?M means "the contents of" memory location M, so to set the contents of &2000 to 40 we write
and to print the contents of memory location &2000 we write
Those familiar with other dialects of BASIC will realise that
Y=PEEK(X) become Y=?X and
POKE X,Y becomes ?X=Y
The query operator acts on one byte of memory only. The word indirection operator (pling) acts on four successive bytes. Thus
would set location
and PRINT ~!M (print in hex, pling M) would give
Four bytes are used to store integers so the pling operator can be used to manipulate integer variables.
The last operator in this group is the string indirection operator called dollar. Do not confuse $M with M$ as they are quite different. M$ is the usual string variable. On the other hand $M can be used to read or write a string to memory starting at memory location M. Remembering that we have set
$M="ABCDEF" will place the string ABCDEF and a carriage return (&0D) in memory from location &2000 on.
PRINT $M will print
And one last twist to the use of these operators. We have seen how query, pling and dollar can be used as unary operators - that is with only one operand. We have used M as the operand in the example above - for example
but in addition both query and pling can be used as binary operators providing that the left hand operand is a variable (such as M9) and not a constant.
Thus M?3 means "the contents of (memory location M plus3)" in other words of location &2003.
There is a simple routine to examine the contents of memory for 12 bytes beyond &2000.
10 FOR X=0 to 12
20 PRINT ~M+X, ~M?X
Line 20 reads "Print in hex (M plus X) and in the next column, in hex, the contents of (M plus X)". It is easy to write this into one of the user defined function keys and keep it for debugging - like this
*KEY 0 FOR X=0 TO 12: P. ~M+X, M?X:NEXT ¦M
but don't forget that in MODE 7 it will be displayed as
*KEY 0 FOR X=0 TO 12: P. M+X,
M?X: NEXT ||M
just to complicate matters!
Here are some illustrations of some of the above.
Set up function key 0 and use it to examine memory beyond &2000.
>*KEY 0 FOR X=0 TO 12: P. ~M+X, ~M? X: N. ¦M
>FOR X=0 TO 12:P. "M+X, M?X:N.
Use the byte indirection operator to change one byte
>FOR X=0 TO 12:P.~M+X, ~M?X:N.
Use the word indirection operator to change 4 bytes
>FOR X=0 TO 12:P. ~M+X, ~M?X:N.
Use the string indirection operator to insert a string into a known place in memory
>FOR X=0 TO 12:P. ~M+X, ~M?X:N.
Note that interesting structures can be generated using ! and $. For example you may need a structure containing a 10 character string, a number and a reference to a similar structure. ! and $ together can do this. If M is the address of the start of the structure then
|$M||is the string|
|M!11||is the number|
|M!15||is the address of the next structure|
The tools are therefore provided to enable you to manipulate general tree structures and lists very easily in BASIC.