# CHAPTER 2 - BASIC
The BASIC of MSX2 has been upgraded: the new version is called MSX BASIC version 2.0. And, when using a disk system, MSX DISK-BASIC can be used, which consists of MSX BASIC version 2.0 and additional instructions for disk operations. The following sections describe these two versions of BASIC.
## Index
[1. LIST OF INSTRUCTIONS](#1-list-of-instructions)
[1.1 Instructions of MSX BASIC version 2.0](#11-instructions-of-msx-basic-version-20)
[1.2 Instructions of MSX DISK-BASIC](#12-instructions-of-msx-disk-basic)
[2. DIFFERENCES IN MSX BASIC VERSION 2.0](#2-differences-in-msx-basic-version-20)
[2.1 Additions or Modifications to Screen Mode](#21-additions-or-modifications-to-screen-mode)
[2.2 Additions or Modifications for the Colour Specification](#22-additions-or-modifications-for-the-colour-specification)
[2.3 Additions or Modifications for the Character Display](#23-additions-or-modifications-for-the-character-display)
[2.4 Additions or Modifications for the Graphics Display](#24-additions-or-modifications-for-the-graphics-display)
[2.5 Additions or modifications for VDP access](#25--additions-or-modifications-for-vdp-access)
[2.7 Additions or Modifications for Sprite](#27-additions-or-modifications-for-sprite)
[2.8 Additions for Optional Features](#28-additions-for-optional-features)
[2.9 Additions for Timer Features](#29-additions-for-timer-features)
[2.10 Additions for Memory Switch](#210-additions-for-memory-switch)
[2.11 Additions for RAM Disk](#211-additions-for-ram-disk)
[2.12 Other Additions](#212-other-additions)
[3. INTERNAL STRUCTURE OF BASIC](#3-internal-structure-of-basic)
[3.1 User's Area](#31-users-area)
[3.2 Detailed View of the User's Area](#32-detailed-view-of-the-users-area)
[3.3 Storage format of BASIC programs](#33-storage-format-of-basic-programs)
[4. LINKING WITH ASSEMBLY LANGUAGE PROGRAMS](#4--linking-with-assembly-language-programs)
[4.1 USR Function](#41-usr-function)
[4.2 Data Exchange by the Argument and Return Value of USR Function](#42-data-exchange-by-the-argument-and-return-value-of-usr-function)
[4.3 Making New Commands](#43-making-new-commands)
[4.4 Expansion of CMD command](#44-expansion-of-cmd-command)
[4.5 Interrupt usage](#45-interrupt-usage)
[5. NOTES ON SOFTWARE DEVELOPMENT](#5-notes-on-software-development)
[Error code list](#error-code-list)
[Changes from the original](#changes-from-the-original)
## 1. LIST OF INSTRUCTIONS
First of all, the sentence and function for each instruction of BASIC are listed. Each instruction is listed in the format shown in [Figure 2.1](#figure-21--instruction-list-format).
##### _Figure 2.1 Instruction list format_

#### (a) Syntax of instructions
If there is an **"*"** followed by a keyword, it indicates that the syntax or function of the instruction has just been modified after version 1.0, or that the instruction has been added to version 2.0.
Descriptions of sentences use the following notational conventions.
```
* [item] .............. the item is optional
* [, item ... ] ....... more items having the same form may appear
* [item1 | item2] ..... choose item1 or item2
```
And ``, which is used in the sentence, is a string specifying I/O devices or files for input/output in the format listed below. `` for a cassette files is a string consisting of any combination of up to 6 characters. `` for disk or RAM disk is a string, whose form is `" + "`. `` is one of characters from A to H (depending on the number of drives connected).
```
"CAS: " ..... Cassette file
"MEM: " ..... RAM disk
"CRT:" ................ Text screen
"GRP:" ................ Graphic screen
"LPT:" ................ Printer
":" .. Disk file
```
#### (b) Instruction type
There are four types of instructions:
```
* Function ............ Returns a certain value depending on the given parameter(s).
* System variable ..... Variables available from BASIC. Generally, assignment is allowed.
* Statement ........... Takes a certain action.
* Command ............. Gives an instruction to BASIC interpreter itself.
```
#### (c) Function or action of instruction
The following list gives a brief description of the action for each instruction. More detailed descriptions about instructions which have been modified or added at version 2.0 are given in [section 2](#2-differences-in-msx-basic-version-20).
### 1.1 Instructions of MSX BASIC version 2.0
#### --- A ---
| Format | Type | Function or action |
| --- | --- | --- |
| `ABS ()` | Function | Returns absolute value of ``. |
| `ASC ()` | Function | Returns the code of the first character of ``. |
| `ATN ()` | Function | Returns arc tangent of `` in radians. |
| `AUTO [[, ]]` | Command | Produces line numbers automatically. |
#### --- B ---
| Format | Type | Function or action |
| --- | --- | --- |
| **`* BASE ()`** | System variable | Contains the table address of the screen assigned on VRAM. |
| `BEEP` | Statement | Produces beep to the audio terminal. |
| `BIN$ ()` | Function | Converts the value of `` to a string of binary expression, then returns its result. |
| `BLOAD ""[,R[,offset]]` | Command | Loads an assembly language program. |
| `BSAVE "",,[,]` | Command | Saves an assembly language program. |
#### --- C ---
| Format | Type | Function or action |
| --- | --- | --- |
| `CALL [([,...])]` | Statement | Calls the extended statements by inserting various cartridges. |
| **`* CALL MEMINI [()]`** | Statement | Specifies the upper limit of memory for Ram disk. |
| **`* CALL MFILES`** | Statement | Lists file names in RAM disk. |
| **`* CALL MKILL ("")`** | Statement | Deletes a file in RAM disk. |
| **`* CALL MNAME ("" AS "")`** | Statement | Renames a file in RAM disk. |
| `CDBL ()` | Function | Converts the value of `` to a double precission real value and returns its result. |
| `CHR$ () Returns a character which has the code of` | Function | `` value. |
| `CINT ()` | Function | Converts the value of `` to an integer value and returns its result. |
| **`* CIRCLE {(X,Y) \| STEP(X,Y)},[, [, [, [, ]]]]`** | Statement | Draws a circle whose center is at (X,Y) and whose size depends on ``. |
| `CLEAR [[, ]]` | Statement | Initialises variables and sets the size of memory area. |
| `CLOAD [""]` | Command | Loads a program from cassette. |
| `CLOAD? [""]` | Command | Compares a program on cassette with the one in memory. |
| `CLOSE [[#][, [#]...]]` | Command | Closes a file represented by ``. |
| `CLS` | Statement | Clears screen. |
| **`* COLOR [[, [, ]]]`** | Statement | Specifies the colours of each part of the screen. |
| **`* COLOR [=NEW]`** | Statement | Initialises the palette. |
| **`* COLOR = (, , , )`** | Statement | Sets the palette colour. |
| **`* COLOR = RESTORE`** | Statement | Puts the contents of the colour palette storage table into the palette register. |
| **`* COLOR SPRITE ()=`** | Statement | Sets the colour to the sprite of `` to the specified colour. |
| **`* COLOR SPRITE$ ()=`** | Statement | Sets the colour of each horizontal line of the sprite using ``. |
| `CONT` | Command | Resumes the execution of the program which has been stopped. |
| **`* COPY TO `** | Statement | Transfers the screen data among the screen, array, and disk file. |
| **`* COPY SCREEN []`** | Statement | Writes colour bus data into VRAM (optional). |
| `COS ()` | Function | Returns the cosine value of ``. |
| `CSAVE ""[, ]` | Command | Saves a program to cassette. |
| `CSGN ()` | Function | Converts the value of `` to a single precision real value, and returns its result. |
| `CSRLIN` | System variable | Contains the vertical screen location of the cursor. No assignment is allowed. |
#### --- D ---
| Format | Type | Function or action |
| --- | --- | --- |
| `DATA [, ...]` | Statement | Prepares data to be read by READ statement. |
| `DEF FN [([, ...])]=` | Statement | Defines a user-defined function. |
| `DEFINT [, ...]` | Statement | Declares the specified variable(s) as integer type. |
| `DEFSNG [, ...]` | Statement | Declares the specified variable(s) as single precision real type. |
| `DEFDBL [, ...]` | Statement | Declares the specified variable(s) as double precision real type. |
| `DEFSTR [, ...]` | Statement | Declares the specified variable(s) as character type. |
| `DEF USR []=` | Statement | Defines the starting address for the execution of assembly language routine, called by USR function. |
| `DELETE {[-] \| \| -}` | Command | Deletes the specified portion of the program. |
| `DIM ([, ...])` | Statement | Defines an array variable and allocates it into memory. |
| `DRAW ` | Statement | Draws a line or lines on the screen according to ``. |
#### --- E ---
| Format | Type | Function or action |
| --- | --- | --- |
| `END` | Statement | Ens the program, close all files, and returns to the command level. |
| `EOF ()` | Function | Checks if the file is finished and returns -1 if at the end of file. |
| `ERASE [, ...]` | Statement | Deletes the array variable(s). |
| `ERL` | System variable | Contains the error code for the preceding error. No assignment is allowed. |
| `ERR` | System variable | Contains the line number of the previous error. No assignment is allowed. |
| `ERROR ` | Statement | Puts the program into the error condition. |
| `EXP ()` | Function | Returns the exponent (power) of the natural exponential form of ``. |
#### --- F ---
| Format | Type | Function or action |
| --- | --- | --- |
| `FIX ()` | Function | Returns the value of ``, without any decimal fractions. |
| `FOR = TO [STEP ]` | Statement | Repeats the execution from FOR statement to NEXT statement for the specified times. |
| `FRE ({ \| })` | Function | Returns the size of unused user's area or unused character area. |
#### --- G ---
| Format | Type | Function or action |
| --- | --- | --- |
| **`* GET DATE [, A]`** | Statement | Assigns date into a string variable. |
| **`* GET TIME [, A]`** | Statement | Assigns time into a string variable. |
| `GOSUB ` | Statement | Calls the subroutine at ``. |
| `GOTO ` | Statement | Jumps to ``. |
#### --- H ---
| Format | Type | Function or action |
| --- | --- | --- |
| `HEX$ ()` | Function | Converts the value of `` to a string of hexadecimal expression, then returns its result. |
#### --- I ---
| Format | Type | Function or action |
| --- | --- | --- |
| `IF THEN { \| } [ELSE { \| }]` | Statement | Judges the condition. If `` is not zero, it is true. |
| `IF GOTO [ELSE { \| }]` | Statement | Judges the condition. If `` is not zero, it is true. |
| `INKEY$` | Function | Returns a character when a key is being pressed, or when not, returns null string. |
| `INP ()` | Function | Reads the port specified by `` and returns its result. |
| `INPUT ["";][, ...]` | Statement | Assigns data input from keyboard into the specified variable(s). |
| `INPUT #, [, ...]` | Statement | Reads data from the file and assigns the data into the specified variable(s). |
| `INPUT$ ([, [#]])` | Function | Reads the specified size of string from the keyboard or file. |
| `INSTR ([,],)` | Function | Searches `` from the left of ``, and returns its location if found, otherwise zero. `` is the character location to start searching. |
| `INT ()` | Function | Returns the largest integer less than ``. |
| `INTERVAL {ON \| OFF \| STOP}` | Statement | Allows, suppresses, or suspends the timer interrupt. |
#### --- K ---
| Format | Type | Function or action |
| --- | --- | --- |
| `KEY ,` | Command | Redefines a function key. |
| `KEY LIST` | Command | Displays the contents of function keys. |
| `KEY (){ON \| OFF \| STOP}` | Statement | Allows, supresses, os suspends the function key interrupt. |
| `KEY {ON \| OFF}` | Statement | Specifies whethter to display the contents of function keys at the bottom of the screen. |
#### --- L ---
| Format | Type | Function or action |
| --- | --- | --- |
| `LEFT$ (,)` | Function | Gets `` characters from the left of ``. |
| `LEN ()` | Function | Returns the number of characters of ``. |
| `[LET] = ` | Statement | Assigns the value of `` to the variable. |
| **`* LINE [{(X1,Y1) \| STEP(X1,Y1)}] - {(X2,Y2) \| STEP(X2,Y2)}[, [, {B\|BF}[, ]]]`** | Statement | Draws a line or a rectangle on the screen. |
| `LINE INPUT ["";]` | Statement | Assigns a whole line of string data from the keyboard into the string variable. |
| `LINE INPUT# , ` | Statement | Reads data in lines from the file and assigns the data into the string variable. |
| `LIST [[] - []]` | Command | Displays the program in memory on the screen. |
| `LLIST [[] - []]` | Command | Sends the program in memory to the printer. |
| `LOAD "" [,R]` | Command | Loads a program saved in ASCII format. |
| **`* LOCATE [[, [, ]]]`** | Statement | Locates the cursor on the text screen. |
| `LOG ()` | Function | Returns the natural logarithm of ``. |
| `LPOS ()` | System variable | Contains the location of the printer head. No assignment is allowed. |
| `LPRINT [[{; \| ,}...]` | Statement | Outputs characters or numerical values to the printer. |
| `LPRINT USING