RISCOS.com

www.riscos.com Technical Support:
BBC BASIC Reference Manual

 


Accessing memory locations


Individual memory locations can be accessed from BASIC by using four indirection operators:

Symbol Purpose Number of bytes
? Byte indirection operators 1
! Integer indirection operator 4
| Floating point indirection operator 5 (BASIC V)
8 (BASIC VI)
$ String indirection operator 1 to 256

These operators can either be used to read the value(s) in one or more memory locations or to alter the value(s) there. You must be very careful that you only read from or write to memory locations which you have set aside specially. Using these operators on other areas of the memory can have undesirable effects.

Reserving a block of memory

You can reserve a block of memory using a special form of the DIM command. For example:

DIM pointer% 100

This reserves a block of (uninitialised) memory and sets the variable pointer% to the address of the first byte of the block. The bytes are at addresses pointer%+0 to pointer%+100, a total of 101 bytes. Note that the address assigned to pointer% will always be a multiple of four. This means that consecutive DIMs will not necessarily allocate contiguous blocks of memory.

Note also that this differs from the usual use of DIM to dimension an array in that the size is not contained in brackets, and the variable cannot be a string.

The '?' indirection operator

You can set the contents of the first byte of this block of memory to 63 by typing

?pointer% = 63

To check that this value has been inserted correctly, type

PRINT ?pointer%

The ? indirection operator affects only a single byte. Only the least significant byte of the number is stored. Thus, if you give it a value of n, where n > 256, only n AND &FF will be stored.

For example,

?pointer% = 356
PRINT ?pointer%

produces the result:

     100

because 356 AND &FF gives 100.

If you wish to set or examine the contents of the location which is five bytes after pointer%, you can do this by typing

?(pointer% + 5) = 25

Alternatively, a shorter form is available as follows:

pointer%?5 = 25

The following program prints out the contents of all the memory locations in the reserved block:

10 DIM block_of_memory% 100
20 FOR N% = 0 TO 100
30   PRINT "Contents of ";N%;" are ";block_of_memory%?N%
40 NEXT N%

The '!' indirection operator

BASIC integer variables are stored in four consecutive bytes of memory. The ! operator can be used to access these four bytes. For example, type

DIM pointer% 100
!pointer% = 356
PRINT !pointer%

The least significant byte of the integer is stored in the first memory location, and the most significant byte in the fourth location. This can be seen in the following example:

10 DIM pointer% 100
20 !pointer% = &12345678
30 PRINT ~pointer%?0
40 PRINT ~pointer%?1
50 PRINT ~pointer%?2
60 PRINT ~pointer%?3

This prints:

      78
      56
      34
      12

The '|' indirection operator

Floating point numbers, which are stored in five bytes (in BASIC V) or eight bytes (in BASIC VI), can be accessed using the unary operator |. For example:

10 DIM pointer% 100
20 |pointer% = 3.678
30 PRINT |pointer%

There is no dyadic form of |. You cannot say, for example, a|5=1.23.

Appendix A: Numeric implementation explains how floating point numbers are stored in BBC BASIC.

The '$' indirection operator

Strings can be placed directly in memory, each character's ASCII code being stored in one byte of memory. For example:

DIM pointer% 100
$pointer% = "STRING"
PRINT $pointer%

The $ indirection operator places a carriage return (ASCII 13) after the last character of the string. Thus, the example above uses seven bytes: six for the characters of the word STRING, plus one for the terminating carriage return. To see this, run the following program:

 10 DIM space% 10
 20 REM set all bytes to zero
 30 FOR N% = 0 TO 10
 40   space%?N% = 0
 50 NEXT N%
 60 REM Store the string
 70 $space% = "STRING"
 80 REM Print out the bytes
 90 FOR N% = 0 TO 10
100   PRINT space%?N% "  ";CHR$(space%?N%)
110 NEXT N%

As with |, there is no dyadic form of $. For example, although you may use $(string+1), the form string$1 is not allowed.

This edition Copyright © 3QD Developments Ltd 2015
Last Edit: Tue,03 Nov 2015