$

From BeebWiki
Jump to: navigation, search

The dollar, $, is a symbol with multiple meanings on the BBC Micro series and in RISC OS:

  • it represents the root directory in a filing system;
  • it is used as a word separator in the names of RISC OS environment variables;
  • it may appear at the end of a BASIC variable name, to identify it as a string variable;
  • it is also a BBC BASIC operator to read or change ASCII strings in memory.

This page is concerned with $ as an operator.

$
Availability Present in all original versions of BBC BASIC.
Syntax BASIC I-V <string-var> = $<numeric>
$<numeric> = <string>
Token (hex) BASIC I-V 24 (operator, lvalue)
Description BASIC I-V In the first form, $ returns the string at the address given in the operand, minus its terminating carriage return.
In the second form, the string assigned to $ is written to the address given in the operand, followed by a terminating carriage return.
Associated keywords !, ?, CALL, USR

Description

$ is an operator providing access to the memory of the machine running BASIC. It allows strings in memory to be created, inspected or replaced. A string consists of 8-bit wide ASCII characters, and may be 0 up to 255 characters long.

Unlike string variables, which are stored in BASIC's internal format, strings in memory are written as one might expect: bare, with the first character at the starting address and proceeding upwards in memory. These strings are terminated with a carriage return code, byte value &0D. When writing, the $ operator adds this terminator automatically, unlike the EQUS directive.

$ is a unary operator that will either read or write to memory, depending on whether it is evaluated as an expression, or assigned a value. In the latter case the whole $<numeric> expression serves as an lvalue, or <string-var>.

For example, $oscli_block% = "ENABLE" places the string ENABLE plus carriage return in memory, starting from the address given by oscli_block%.

The statement, name$ = $table%, fills name$ with the contents of memory starting from the address in variable table%. If there is a carriage return at table%, name$ is left empty. Otherwise bytes of memory are added to name$ in ascending address order, until a carriage return is found (which is not added to name$). If no carriage return is found within 255 bytes, the string is left empty.

BBC BASIC does not allow memory in zero page to be treated as a string. If the $ operand address is less than 256, a $ range error results.

Warnings

The BBC Microcomputer User Guide is peppered with warnings about the use of ?, ! and $. They are not to be used to access memory-mapped devices or the system's internal variables – at least, not in published programs. The relevant addresses may change or disappear on different machines and MOS versions, or the program may find itself running on the other side of a Tube! The MOS offers a comprehensive API to access system functions in a portable way.

Address space

The address space in which $ operates is the one BASIC chooses to provide for $. Normally this is the address space of the processor running BASIC. The BASIC program appears in this space, between PAGE and TOP, but as mentioned above, well-behaved programs must not alter it.

BAS128 for the B+ and Master puts the 6502 address space from 0 to &FFFF, and adds an extended space between &10000 and &1FFFF. This is made of the four slots of sideways RAM and contains the user's program, variables and memory blocks. The MOS cannot access this space (unless its own extended addressing system is enabled) and machine code definitely cannot run in it although it can be assembled there with OPTions 4 to 7.

-- beardo 23:01, 30 March 2007 (BST)