Skip to content

Language guide

cheveron edited this page Feb 24, 2022 · 30 revisions

This documentation describes the SE Basic IV language, which aims to support programs written for Microsoft BASIC with minimal changes.

The Language Guide covers the language topic by topic, thematically grouping language elements used for a related purpose. Refer to the Language reference for a formal description of the language elements and their syntax.

Working with programs

StatementDescription
AUTOEnter automatic line numbering mode.
CHAINLoad a new program and run it, preserving common variables.
DELETEDelete lines from the program.
EDITPrint a program line to the screen for editing.
LISTPrint program lines to the screen.
LIST #Print program lines to another device.
LOADRead a new program from file.
MERGEOverlay a program file onto the current program
NEWClear the current program from memory.
OLDRestore the previously cleared program to memory.
RENUMReplace the program's line numbers.
RUNStart the current program.
SAVEStore the current program to file.
TRACEToggle line number tracing.

Control flow

A program is normally executed starting with its lowest line number (or the line number called by RUN). Statements on a line are executed from left to right. When all statements on a line are finished, execution moves to the next lowest line number, and so on until no line numbers are left. Control flow statements can be used to modify this normal flow of executon.

The END and STOP statements serve in a program to stop its execution and return to direct mode. When STOP is used, a Break message is printed. From direct mode, CONT can be executed to resume the program where it was stopped. While END seems intended to terminate the program, it does not preclude the user from resuming it with CONT.

Unconditional jumps can be made with GOTO. The program flow will continue at the line number indicated in the GOTO statement. Due to the SE Basic IV language's lack of sophisticated looping, branching and breaking constructs, unconditional jumps are essential and used frequently.

The GOSUB statement jumps to a subroutine. Similar to GOTO, this is an unconditional jump; however, the location of the call is stored and the program will continue its flow there after the subroutine terminates with a RETURN statement. Subroutines are somewhat like procedures in that they allow chunks of code that perform a given task to be separated from the main body of the program, but they do not have separate scope since all variables in SE Basic IV are global. They do not have return values. It is even possible to jump out of a subroutine to anywhere in the program by supplying the RETURN statement with a line number.

The ON statement provides an alternative branching construct. An integer value is used to selects one of a list of line numbers, and execution is continued from there. It can be used with a GOTO jump as wellas with a GOSUB subroutine call.

ON, GOTO and GOSUB can also be used from direct mode to start a program or subroutine without resetting variables.

The IF–THEN–ELSE construct tests for a condition and execute different code branches based on its truth value. This is not a block construct; all code in the THEN and ELSE branches must fit on one line. For this reason, branching is often used in combination with GOTO jumps. For example:

10 INPUT "How old are you"; AGE
20 IF AGE > 30 THEN 100
30 IF AGE < 30 THEN 200 ELSE PRINT "You are 30 years old."
40 END
100 PRINT "You are over 30."
110 END
200 PRINT "You are not yet 30."
210 END

The WHILE–WEND looping construct repeats the block of code between WHILE and WEND as long as a given condition remains true.

The FOR–NEXT construct repeats a block of code while a counter remains in a given range. The counter is set to a starting value at the first pass of the FOR statement and incremented by the STEP value at each pass of NEXT. For example:

10 FOR I=1 TO 10
20 PRINT STRING$(I, "*"); USING " [##]"; I
30 NEXT I

Looping constructs may be nested.

Control flow is also affected by event and error trapping.

StatementDescription
CONTContinue interrupted program.
ELSEIgnore the remainder of the line (standalone ELSE).
ENDStop execution of the program.
FORStart a for-loop.
GOSUBCall a subroutine.
GOTOJump to another location in the program.
IFBranch on a condition.
NEXTIterate a for-loop.
ONCalculated jump or subroutine call.
RETURNReturn from subroutine.
STOPInterrupt program execution.
WENDIterate a while-loop.
WHILEEnter a while-loop.

Arrays and variables

StatementDescription
DIMAllocate an array.
ERASEDeallocate an array.
LETAssign a value to a variable.
OPTION BASESet the starting index of arrays.
SWAPSwap two variables.

Type conversion

FunctionDescription
ASCCharacter to ordinal value.
CHR$Ordinal value to character.
STR$Numeric value to string representation. Defaults to decimal but can support base 2 (BIN$), 8 (OCT$), 16 (HEX$)
VALString representation to numeric value.
VAL$String representation to string value.

String operations

StatementDescription
LSETCopy a left-justified value into a string buffer.
MID$Copy a value into part of a string buffer.
RSETCopy a right-justified value into a string buffer.
FunctionDescription
INSTRFind.
LEFT$Left substring.
LENString length.
MID$Substring.
RIGHT$Right substring.
SPACE$Repeat spaces.
STRING$Repeat characters.

Text and the screen

StatementDescription
CLSClear the screen.
COLORSet colour and palette values.
LOCATESet the position and shape of the text screen cursor.
PALETTEAssign a colour to an attribute.
PALETTE USINGAssign an array of colours to attributes.
COPYCopy a screen page.
PRINTPrint expressions to the screen.
VIEW PRINTSet the text scrolling region.
WIDTHSet the number of text columns on the screen.
FunctionDescription
CSRLINCurrent row of cursor.
POSCurrent column of cursor.
SCREENCharacter or attribute at given location.

The printer

StatementDescription
PRINTPrint expressions to the printer.
FunctionDescription
POSCurrent column of cursor.

Keyboard input

StatementDescription
INPUTRetrieve user input on the console.
LINE INPUTRetrieve a line of user input on the console.
FunctionDescription
INKEY$Nonblocking read from keyboard.
INPUT$Blocking read from keyboard.

Function-key macros

StatementDescription
KEYDefine a function-key macro.

Calculations and maths

Mathematical functions

FunctionDescription
ABSAbsolute value.
ATANArctangent.
ACOSArccosine.
ASINArcsine.
COSCosine.
EXPExponential.
FIXTruncation.
INTFloor.
LOGNatural logarithm.
SINSine.
SGNSign.
SQRSquare root.
TANTangent.

Random numbers

StatementDescription
RANDOMIZESeed the random number generator.
FunctionDescription
RNDPseudorandom number.

Devices and files

File operations

StatementDescription
CLOSEClose a file.
FIELDAssign a string to a random-access record buffer.
GETRead a record from a random-access file.
INPUTRead a variable from a file.
LINE INPUTRead a line from a file.
LOCKLocks a file or a range of records against other use.
OPENOpen a data file.
PUTWrite the random-access record buffer to disk.
RESETClose all files.
UNLOCKUnlocks a file or a range of records against other use.
WIDTHSet the number of text columns in a file.
WRITEWrite expressions to a file.
FunctionDescription
EOFEnd of file.
LOCLocation in file.
LOFLength of file.
INPUT$Read a string from a file.

Devices

SE Basic IV handles devices differently than Microsoft BASIC. A device is connected to a specific channel and one or more I/O streams can be connected to that channel. For example, the display is connected to channel S and by default stream #2 is connected to that channel S.

DeviceChannelDefault stream
KeyboardK#1
ScreenS#2

Graphics

StatementDescription
CIRCLEDraw an ellipse or arc section.
DRAWDraw a shape defined by a Graphics Macro Language string.
GETStore a screen area as a sprite.
LINEDraw a line segment.
PAINTFlood-fill a connected region.
PSETPut a pixel.
PRESETChange a pixel to background attribute.
PUTDraw a sprite to the screen.
SCREENChange the video mode.
VIEWSet the graphics viewport.
WINDOWSet logical coordinates.
FunctionDescription
POINTGraphical pointer coordinates.
POINTPixel attribute.
PMAPConvert between physical and logical coordinates.

Sound

StatementDescription
BEEPBeep the speaker.
BEEPSpeaker switch.
NOISEGenerate noise.
PLAYPlay a tune encoded in Music Macro Language.
SOUNDGenerate a tone.
SOUNDSound switch.

Joystick and mouse

StatementDescription
STRIGJoystick switch.
FunctionDescription
MOUSEStatus of mouse.
STICKStatus of joystick.
STRIGStatus of joystick fire button.

Disks and DOS

In SE Basic IV, the DOS is integrated into BASIC and there is no SHELL command.

StatementDescription
CHDIRChange current directory.
COPYMake a copy of a file.
FILESList the files in the current directory.
KILLDelete a file on a disk device.
MKDIRCreate a new directory.
NAMERename a file on disk.
RMDIRRemove a directory.

Serial communications

StatementDescription
GETRead bytes from a serial port.
PUTWrite bytes to a serial port.
TERMOpen the terminal emulator.

Event handling

Event trapping allows to define subroutines which are executed outside of the normal course of operation. Events that can be trapped are:

  • Time intervals (ON TIMER)
  • Keypresses (ON KEY)
  • Serial port input (ON COM)
  • Music queue exhaustion (ON PLAY)
  • Joystick triggers (ON STRIG)
  • Mouse activation (ON MOUSE)

Event trapping subroutines are defined as regular subroutines. At the RETURN statement, the normal course of program execution is resumed. Event trapping can be switched on and off or paused temporarily with statements of the form MOUSE ON, MOUSE OFF, MOUSE STOP. Event trapping only takes place during program execution and is paused while the program is in an error trap. If an event occurs while event-trapping is paused, then the event is triggered immediately when event trapping is resumed.

StatementDescription
COMManage serial port event trapping.
KEYManage keyboard event trapping.
KEYDefine key to trap in keyboard event trapping.
ONDefine event-trapping subroutine.
PENManage light pen event trapping.
PLAYManage music queue event trapping.
STRIGManage joystick event trapping.
TIMERManage timer event trapping.

Error handling

Normally, any error will interrupt program execution and print a message on the console (exceptions are Overflow and Division by zero, which print a message but do not interrupt execution). It is possible to handle errors more graciously by setting an error-handling routine with the ON ERROR GOTO line_number statement. The error-handling routine starts at the given line number line_number and continues until a RESUME statement is encountered. Error trapping is in effect both when a program is running and in direct mode. Error trapping is switched off with the ON ERROR GOTO 0 statement. If an error occurs, or error trapping is switched off, while the program is executing an error-trapping routine, the program terminates and an error message is shown.

StatementDescription
ERRORRaise an error.
ON ERRORDefine an error handler.
RESUMEEnd error handler and return to normal execution.
FunctionDescription
ERRError number of last error.
ERLLine number of last error.

User-defined functions

StatementDescription
DEF FNDefine a new function.
FunctionDescription
FNUser-defined function.

Date and time

StatementDescription
DATE$Set the system date.
TIME$Set the system time.
FunctionDescription
DATE$System date as a string.
TIME$System time as a string.
TIMERSystem time in seconds since midnight.

Including data in a program

StatementDescription
DATADefine data to be used by the program.
READRetrieve a data entry.
RESTOREReset the data pointer.

Memory and machine ports

Only selected memory ranges are accessible in SE Basic IV. There is read and write support for video memory, font RAM and selected locations of the low memory segment, including the keyboard buffer. Additionally, there is read and write support for variable, array and string memory. Writing into the program code is permitted but not recommended. All ports including keyboard input and video modes are supported.

StatementDescription
BLOADLoad a binary file into memory.
BSAVESave a memory region to file.
CLEARClears BASIC memory.
DEF SEGSet the memory segment.
OUTWrite a byte to a machine port.
POKEWrite a byte to a memory location.
WAITWait for a value on a machine port.
FunctionDescription
FREAmount of free memory.
INPByte at machine port.
PEEKByte at memory address.
VARPTRMemory address of variable
VARPTR$Byte representation of length and memory address of variable

Legacy feature support

To imporve compatibility with legacy programs, SE Basic IV includes a preprocessor that attempts to convert MS BASIC syntax into SE Basic IV syntax. It applies the following conversions:

MS BASICSE Basic IVDescription
><, =>, =<<>, <=, >=not equal to, less than or equal to, greater than or equal to.
[, ](, )used when defining arrays.
&H,&O$,@hexadecimal and octal notation.
ATN(n)ATAN(n)
COLOURCOLORalternative spelling.
ELSE:ELSEa colon is required before an ELSE except if it is the first statement in a line.
FNaFN aa space is required after FN.
HEX$(n), OCT$(n)STR$(n, {16|8})hexadecimal or octal string.
RND(n)RNDparameters are not accepted with RND.
SPACE$(n)STRING$(n,32)string of n spaces.
THEN nTHEN GOTO nn cannot be an expression.
TRON, TROFFTRACE {ON|OFF}

Features not yet implemented

Features that are planned but not yet implemented are missing links to the language reference.

Unsupported features

SE Basic IV has direct access to all areas of memory and all devices. You can use machine-code subroutines to perform tasks for which it does not provide support. However, programs written for a different architecture, such as 8086, will not run.

It is not necessary, or possible, to define variable types. Like the 6502 versions of Microsoft BASIC, floating-point numbers are always stored in 40-bit MBF form. Unlike Microsoft BASIC, integers in the range [-65536 to 65535] are always stored as integers (also in 40-bits).

Clone this wiki locally