Indirection

Introduction

Most versions of BASIC allow access to the computer's memory with the PEEK function and the POKE command. Such access, which is limited to one byte at a time, is sufficient for setting and reading screen locations or 'flags', but it is difficult to use for building more complicated data structures. The indirection operators provided in BBC BASIC (Z80) enable you to read and write to memory in a far more flexible way. They provide a simple equivalent of PEEK and POKE, but they come into their own when used to pass data between CHAINed programs, build complicated data structures or for use with machine code programs.

BBC BASIC (Z80) can 'see' all the computer's memory and the indirection operators will accept 16-bit addresses in the range &0000 to &FFFF.

There are three indirection operators:

NameSymbolPurposeNo. of Bytes Affected
Query?Byte Indirection Operator1
Exclamation!Word Indirection Operator4
Dollar$String Indirection Operator1 to 256

Query

Byte Access

The query (?) operator accesses individual bytes of memory. ?M means 'the contents of' memory location 'M'. The first two examples below write &23 to memory location &4FA2, the second two examples set 'number' to the contents of that memory location and the third two examples print the contents of that memory location.

?&4FA2=&23

or

memory=&4FA2
?memory=&23
number=?&4FA2

or

memory=&4FA2
number=?memory
PRINT ?&4FA2

or

memory=&4FA2
PRINT ?memory

Thus, '?' provides a direct replacement for PEEK and POKE.

Query as a Byte Variable

A byte variable, '?count' for instance, may be used as the control variable in a FOR...NEXT loop and only one byte of memory will be used.

DIM count% 0
FOR ?count%=0 TO 20
  - - -

  - - -
NEXT

Exclamation

The query (?) indirection operator works on one byte of memory. The word indirection operator (!) works on 4 bytes (an integer word) of memory. Thus,

!M=&12345678 

would load

and

PRINT ~!M : REM (print !M in hex format) 

would give

12345678 

Dollar

The string indirection operator ($) writes a string followed by a carriage-return (&0D) into memory starting at the specified address. Do not confuse M$ with $M. The former is the familiar string variable whilst the latter means 'the string starting at memory location M'. For example,

$M="ABCDEF" 

would load the ASCII characters A to F into addresses M to M+5 and &0D into address M+6, and

PRINT $M 

would print

ABCDEF 

Use as Binary Operators

All the examples so far have used only one operand with the byte and word indirection operators. Provided the left-hand operand is a variable (such as 'memory') and not a constant, '?' and '!' can also be used as binary operators. (In other words, they can be used with two operands.) For instance, M?3 means 'the contents of memory location M plus 3' and M!3 means 'the contents of the 4 bytes starting at M plus 3'. In the following example, the contents of memory location &4000 plus 5 (&4005) is first set to &50 and then printed.

memory=&4000
memory?5=&50
PRINT memory?5

Thus,

The two examples below show how two operands can be used with the byte indirection operator (?) to examine the contents of memory. The first example displays the contents of 12 bytes of memory from location &4000. The second example displays the memory contents for a real numeric variable. (See the appendix entitled Format of Program and Variables in Memory).

10 memory=&4000
20 FOR offset=0 TO 12
30   PRINT ~memory+offset, ~memory?offset
40 NEXT

Line 30 prints the memory address and the contents in hexadecimal format.

 10 NUMBER=0
 20 DIM A% -1
 30 REPEAT
 40   INPUT"NUMBER PLEASE "NUMBER
 50   PRINT "& ";
 60   FOR I%=2 TO 5
 70     NUM$=STR$~(A%?-I%)
 80     IF LEN(NUM$)=1 NUM$="0"+NUM$
 90     PRINT NUM$;" ";
100   NEXT
110   N%=A%?-1
120   NUM$=STR$~(N%)
130   IF LEN(NUM$)=1 NUM$="0"+NUM$
140   PRINT " & "+NUM$''
150 UNTIL NUMBER=0

See the appendix entitled Format of Program and Variables in Memory for an explanation of this program.

Power of Indirection Operators

Indirection operators can be used to create special data structures, and as such they are an extremely powerful feature. For example, a structure consisting of a 10 character string, an 8 bit number and a reference to a similar structure can be constructed.

If M is the address of the start of the structure then:

Linked lists and tree structures can easily be created and manipulated in memory using this facility.