Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Locomotive BASIC
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Variables and functions == Variable names must begin with a letter but can have up to 40 character long names made up of <CODE>A-Z</CODE>, the digits <CODE>0-9</CODE> and the <CODE>.</CODE> symbol. Names are case-insensitive, but case-as-typed is maintained in program listings. Three data-types are supported signified by an identifier suffix on the variable name and can be selectively presumed by use of the <CODE>DEFINT, DEFREAL</code> and <code>DEFSTR</code> directives: <ul> <li>Strings with type marker $, e.g.<CODE>MyString$</CODE>, up to 255 bytes in length</li> <li>2-byte signed integers with type marker %, e.g.<CODE>My.Integer%</CODE>, in the range -32768 to +32767</li> <li>5-byte real (floating point) with type marker !, e.g.<CODE>My99Real!</CODE>, in the approximate range +/-10E+38 to +/-4E-39 with nine significant digits. Type real is the default for un-typed variable names. All Floating point mathematics is the result of Amstrad BASIC exposing the MATHS_PACK of the firmware (very complete and entirely usable from machine code).</li> </ul> Unlike some dialects, variable names are unique down to type, e.g. <CODE>S%</CODE> and <CODE>S!</CODE> are separate variables. Dimensioned variables (arrays) have 0 as their lowest element number(s), and can use square brackets around their elements which helps distinguish them from other parentheses in complex expressions in program code. e.g. <CODE>A(3)</CODE> can be written as <CODE>A[3]</CODE>. Named single-line functions are supported and follow the type and name convention of variables but pre-fixed with <CODE>FN</CODE>. e.g. <code>FNMy.Func!</CODE> The address of a variable in memory can be determined with the @ directive, e.g. <CODE>PRINT @A$</CODE>. This replaces the VARPTR() function found in other dialects of BASIC and is mostly used to pass a variable pointer to machine code routines allowing bi-directional data transfer. Integer and Real variable values are stored in the variable table (see memory map below). String variable ''contents'' are stored on the heap. This allows the aggregation of fragmented memory via [[Garbage collection (computer science)|Garbage Collection]]. Consequently, for strings, the variable table held a three byte descriptor made up of the length of the variable and the address in the heap where the actual contents are to be found. === Two missed opportunities? === BBC Basic uses a clever method for handling some integer variables, which despite claims of its heavy influence on Locomotive BASIC (and by extension Amstrad BASIC), was not replicated in the latter. BBC BASIC has a 54-byte table reserved for the values of the 27 single letter integers <code>@%, A%, B% ... Z%</code> so they do not occupy nor require lookup in the variable table - a simple left-shift and addition is all that is required to determine the value of a given variable. This makes access to the subject variables very fast and considering their ubiquitous nature, it is perhaps, surprising this mechanism was not used in Amstrad BASIC. For strings, the descriptor is three bytes in the variable table, made up of a length descriptor and a further (little-endian) address of the actual payload. Had the value returned by @ been the two byte address in the heap (and the length prefix moved to the heap also) this would have substantially reduced access time by replacing the two address lookups with just one. [[File:CPC A$.png|640px|frameless|center]] Variable table structure for strings.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)