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
Sinclair 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!
==Description== ===Program editing=== Like most home-computer BASICs, Sinclair BASIC is anchored around its [[line editor]]. When the machine is booted, it runs BASIC and displays an [[inverse video]] "K" at the bottom of the screen to indicate the entry point. When a line is entered and the {{keypress|NEW LINE}} key is pressed, it either runs immediately if it does not have a [[line number]] prefix, or clears the screen and performs the equivalent of a {{code|LIST}} command, placing a ">" cursor after the line number in the most recently entered line. In contrast to most machines of the era, the editor does not allow freeform editing at any point on the screen. Instead, when the user presses {{key press|EDIT}}, the current line of code is copied back to the bottom of the screen. The user can move horizontally through this line using the [[cursor key]]s and commits their changes by pressing {{key press|NEW LINE}} again. In contrast, on machines like the [[Commodore 64]] or [[Atari 8-bit computers]], the up and down keys can be used to move among the lines in the program and edit them in-place. The most notable feature of the editor is that keywords are entered using single keystrokes. For instance, on the ZX81, the {{key press|P}} key on the keyboard would cause the entire keyword {{code|PRINT}} to be entered into the currently editing line. Once a keyword has been entered, the cursor changes to an "L" to indicate what follows will be interpreted as normal text. For instance, pressing {{key press|P}} again at this point would enter a single letter "P". Keys generally had two separate keywords assigned to them{{snd}} one above it and one below. Pressing the key in "K" mode would enter the keyword above the key, like {{code|PRINT}} for {{key|P}}. If the system was in "L" mode, one could return to "K" by pressing the [[shift key]]{{snd}} the systems did not initially support [[lowercase]] text, so the shift was not otherwise needed. The keywords below the keys required a second keystroke, {{key press|SHIFT|NEW LINE}}, which put the editor into "function mode", changing the cursor to an "F". Entering common code often resulted in a significant number of keystrokes. The system has the advantage of representing all multi-character keywords as a single character in memory, which was a significant savings in the early machines that shipped with only 1 KB of RAM. This single-character representation included multi-character items like {{code|<>}}. This has the added advantages of simplifying the runtime, as it can immediately determine whether a character in the [[source code]] is a keyword or text, and also means that keywords are never entered directly, meaning that one can, for instance, have a variable named "PRINT", as the system can determine that it is not the same as the keyword. As the systems evolved and added new keywords, the entry system became increasingly difficult to use. 48 BASIC in the Spectrum required every key to host up to four keywords. Entering keywords was a time-consuming process of looking over the relatively small type on the keyboard for the appropriate key, and then correctly entering the multiple keystrokes needed to enter it properly. For instance, entering {{code|BEEP}} required one to type {{keypress|CAPS SHIFT|SYMBOL SHIFT}} to access extended mode (later models include a separate {{keypress|EXTENDED MODE}} key), keeping {{keypress|SYMBOL SHIFT}} held down, and then and pressing {{keypress|Z}}. To improve the complex entry on the Spectrum, the keywords were colour-coded to indicate the required mode:{{sfn|Vickers|1983|p=7–8}} * {{font color|white|gray|White}}: key only * {{font color|red|gray|Red}} on the key itself: {{keypress|SYMBOL SHIFT}} plus the key * {{font color|green|black|Green}} above the key: {{keypress|EXTENDED MODE}} followed by the key * {{font color|red|black|Red}} below the key: {{keypress|EXTENDED MODE}} followed by {{keypress|SYMBOL SHIFT}} plus the key This concept had run its course, and later machines running '''128 BASIC''' (ZX Spectrum 128, +2, +3, +2A, and +2B) featured a more traditional editor where the user typed-in the keyword as individual characters, similar to other home computers of the era. This required a new tokenizer to convert the line into a similar internal format. The resulting in-memory storage of the program was otherwise similar to [[Microsoft BASIC]], in that only the keywords are presented as tokens, while non-keywords{{snd}} like string and numeric constants and variable names{{snd}} are left in their original typed-in format. However, that typed-in format was not [[ASCII]], but an internal character code that contains both printable characters and the keyword tokens. Although portions of the table, the capital letters A to Z, for instance, are in the same order as in ASCII, their characters correspond to different numeric values, i.e. their [[code point]]s' offset from zero is different from ASCII's offset of 65. Other characters, like punctuation, may have been moved about even more. The [[ZX Spectrum character set]] is the most prominent example of such a character code. Related computers running Sinclair BASIC used similar variants, e.g. the [[ZX80 character set|ZX80]] or [[ZX81 character set]]s. All of these different but related character sets included Sinclair BASIC tokens. ===Data types=== One uncommon feature of Sinclair BASIC is the way it stores variables in memory. Typically, interpreters use a fixed-size entry to hold data, making it easy to scan the variable table. Due to the extremely limited memory of the ZX series, any wasted space had to be avoided, and this led to the use of a variable-length format. The data types included numbers stored in a 5-byte values, strings with a length and then the characters, and arrays of both of those types. The data was stored in the table itself, which contrasts with most BASICs of the era, where strings and array entries were stored in a separate [[Heap (data structure)|heap]]. The first byte for a variable entry always held the type in the first three bits, and the first character of the name in the next five bits. As was the case in most microcomputer dialects, <code>A</code>, <code>A$</code>, <code>A()</code> and <code>A$()</code> were all different variables and could store different values. Most variables could only have a single-character name; the exception are numeric variables (not arrays), where an alternate format held the first character of the name in those same five bits, but was then followed by additional characters ending with one with its high-bit set. Long variable names were whitespace-independent, and case-insensitive in later versions, so <syntaxhighlight lang="basic" inline>LET Number Of Apples = 5</syntaxhighlight> is the same as <syntaxhighlight lang="basic" inline>LET numberofapples = 5</syntaxhighlight> referred to the same variable. The downside to this approach is that scanning the table to look up the value of a variable reference is more complex. In addition to testing whether the name matches using the subset of the first byte, if the entry is not the one that is being looked for, the type has to be read from the upper three bits and then the next location of a variable in storage calculated using the type. For instance, if the program encounters the variable "A" and the table starts with the entry for "B$", it fails to match A with B, then reads the type to see it is a string, and then has to read the following length byte and skip forward by that amount of bytes to find the next entry in the table. To make this somewhat easier, arrays also stored a two-byte length, so the entire structure could be skipped over more easily. A unique feature was the "short float", or integer type. Any numeric variable could store either type, the storage itself did not change and used 5 bytes in either case. Integers were indicated by setting the exponent byte to zero, while floating-point values were stored with an [[excess-128]] format exponent. This meant that it could not store zero as a float and lost one possible exponent magnitude. It also did not use any less memory, as the values were still 5-byte in memory. The advantage to this format is performance; the math library included tests to look for the zero exponent, and if it was seen, it would not attempt to perform various operations on the remaining 3 bytes under certain conditions. ===Keyword details=== The ZX81 '''8K BASIC''' used the shorter forms <code>GOTO</code>, <code>GOSUB</code>, <code>CONT</code> and <code>RAND</code>, whereas the Spectrum '''48 BASIC''' used the longer forms <code>GO TO</code>, <code>GO SUB</code>, <code>CONTINUE</code> and <code>RANDOMIZE</code>. The ZX80 '''4K BASIC''' also used these longer forms but differed by using the spelling <code>RANDOMISE</code>. The ZX81 '''8K BASIC''' was the only version to use <code>FAST</code>, <code>SCROLL</code>, <code>SLOW</code> and <code>UNPLOT</code>. The ZX80 '''4K BASIC''' had the exclusive function <code>TL$()</code>; it was equivalent to the string operator {{nowrap|<code>(2 TO )</code>}} in later versions. Unique code points are assigned in the [[ZX80 character set]], [[ZX81 character set]] and [[ZX Spectrum character set]] for each keyword or multi-character operator, i.e. <code><=</code>, <code> >=</code>, <code><></code>, <code>""</code> (tokenized on the ZX81 only), <code>**</code> (replaced with <code>↑</code> on the Spectrum). These are expanded by referencing a token table in ROM. Thus, a keyword uses one byte of memory only, a significant saving over traditional letter-by-letter storage. This also meant that the [[BASIC interpreter]] could quickly determine any command or function by evaluating one byte, and that the keywords need not be ''reserved words'' like in other BASIC dialects or other programming languages, e.g., it is allowed to define a variable named <code>PRINT</code> and output its value with <code>PRINT PRINT</code>. This is also related to the syntax requirement that every line start with a command keyword, and pressing the one keypress for that command at the start of a line changes the editor from command mode to letter mode. Thus, variable assignment requires <syntaxhighlight lang="basic" inline>LET</syntaxhighlight> (i.e., <syntaxhighlight lang="basic" inline>LET A=1</syntaxhighlight> not only <syntaxhighlight lang="basic" inline>A=1</syntaxhighlight>). This practice is also different from other BASIC dialects. Further, it meant that unlike other BASIC dialects, the interpreter needed no parentheses to identify functions; <code>SIN x</code> was sufficient, no <code>SIN(x)</code> needed (though the latter was allowed). The 4K BASIC ROM of the ZX80 had a short list of exceptions to this: the functions <code>CHR$()</code>, <code>STR$()</code>, <code>TL$()</code>, <code>PEEK()</code>, <code>CODE()</code>, <code>RND()</code>, <code>USR()</code> and <code>ABS()</code> did not have one-byte tokens but were typed in letter-by-letter and required the parentheses. They were listed as the INTEGRAL FUNCTIONS on a label above and to the right of the keyboard.<ref>{{Cite web|url=https://upload.wikimedia.org/wikipedia/commons/5/54/Sinclair_ZX80_%281980%29_-_Computer_History_Museum.jpg|title=Picture of ZX80}}</ref> '''128 BASIC''', present on ZX Spectrum 128, +2, +3, +2A, and +2B, stored keywords internally in one-byte code points, but used a conventional letter-by-letter BASIC input system. It also introduced two new commands: * <code>PLAY</code>, which operated the 128k models' [[General Instrument AY-3-8910]] music chip * <code>SPECTRUM</code>, which switched the 128k Spectrum into a 48k Spectrum compatibility mode The original Spanish ZX Spectrum 128 included four additional BASIC editor commands in Spanish,<ref name="Disassembly"/><ref>{{Cite book |last=Sinclair |url=http://archive.org/details/128_20220808_202208 |title=ZX Spectrum 128 Introducción y Guía de Funcionamiento |publisher=Sinclair Research Ltd and Investronica, S. A. |year=1985 |pages=9}}</ref> one of which was undocumented: * <code>EDITAR</code> (to edit a line number or invoke the full screen string editor) * <code>NUMERO</code> (to renumber the program lines) * <code>BORRAR</code> (to delete program lines) * <code>ANCHO</code> (to set the column width of the [[RS-232]] device, but undocumented as the code was broken) Unlike the <code>LEFT$()</code>, <code>MID$()</code> and <code>RIGHT$()</code> functions used in the ubiquitous Microsoft BASIC dialects for home computers, parts of strings in Sinclair BASIC are accessed by numeric range. For example, {{nowrap|<code>A$(5 TO 10)</code>}} gives a substring starting with the 5th and ending with the 10th character of the variable <code>a$</code>. Thus, it is possible to replace the <code>LEFT$()</code> and <code>RIGHT$()</code> commands by simply omitting the left or right array position respectively; for example {{nowrap|<code>a$( TO 5)</code>}} is equivalent to <code>LEFT$(A$,5)</code>. Further, <code>a$(5)</code> alone is enough to replace <code>MID$(A$,5,1)</code>.
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)