Contents / Reference section / Previous chapter / Next chapter / Index


39 Indirection Operators

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=&2000

?M means "the contents of" memory location M, so to set the contents of &2000 to 40 we write

?M=40

and to print the contents of memory location &2000 we write

PRINT ?M

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

!M=&12345678

would set location

&2000=&78

&2001=&56

&2002=&34

&2003=&12

and PRINT ~!M (print in hex, pling M) would give

12345678

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=&2000 then

$M="ABCDEF" will place the string ABCDEF and a carriage return (&0D) in memory from location &2000 on.

Similarly

PRINT $M will print

ABCDEF

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

M=&2000

?M=&45

Y=?M

PRINT ?M

!M=&8A124603

Y=!M

PRINT!M

$M="HELLO ZAPHOD"

B$=$M

PRINT $M

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

30 NEXT

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

>M=&2000

>FOR X=0 TO 12:P. "M+X, M?X:N.

2000 FF
2001 FF
2002 FF
2003 FF
2004 FF

2005 FF
2006 FF
2007 FF
2008 FF
2009 FF
200A FF
200B FF
200C FF

Use the byte indirection operator to change one byte

>M?3=&33

>FOR X=0 TO 12:P.~M+X, ~M?X:N.

2000 FF
2001 FF
2002 FF
2003 33
2004 FF
2005 FF
2006 FF
2007 FF
2008 FF
2009 FF
200A FF
200B FF
200C FF

Use the word indirection operator to change 4 bytes

>M!2=&12345678

>FOR X=0 TO 12:P. ~M+X, ~M?X:N.

2000 FF
2001 FF
2002 78
2003 56
2004 34
2005 12
2006 FF
2007 FF
2008 FF
2009 FF
200A FF
200B FF
200C FF

Use the string indirection operator to insert a string into a known place in memory

>$M="ABCDEFG"

>FOR X=0 TO 12:P. ~M+X, ~M?X:N.

2000 41
2001 42
2002 43
2003 44
2004 45
2005 46
2006 47
2007 D
2008 FF
2009 FF
200A FF
200B FF
200C FF

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.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001