Table Of Contents:
General info
About variables
Mathematical and logical operations
Standard Basic language elements
Memory access
Subroutines
Bit-oriented language elements
Communication with I/O ports
Structured language support (procedures and functions)
Table Of Contents:
• General info
• About variables
• Mathematical and logical operations
• Standard Basic language elements
• Memory access
• Subroutines
• Bit-oriented language elements
• Communication with I/O ports
• Structured language support (procedures and functions)
• General info
Basic compiler editor is composed of editor panel (for user program editing) and source explorer (for easy navigation through all elements of user program - variables, symbols, constants, subroutines, procedures and functions). Editor formats and colorizes entered lines of user program, that simplifies the debugging process.
In all the user-defined program element names, that is in all the variable names, names of the procedures, functions, subroutines, constants, symbols and labels, both lower-case and upper-case characters can be used, along with the underscore and numeric characters. A numeric character will not be accepted by the compiler to be the leading character in the element name.
The primary output of the compiler is an assembler source file. However, with an appropriate command from the menu it can be assembled and even loaded in the simulator with a single click. Menu commands and options are rich, as well as the commands from the right-click popup menus for the editor and source explorer. Basic compiler's assembler output contains many useful comment lines, that makes it very helpful for educational purposes, also.
Show Warnings
If Show Warnings option is enabled, in the Warnings window Basic compiler will show information about unused declarations, subroutines, procedures and functions in the user basic program.
Do Not Compile Unused Code
If this option is enabled, Basic compiler will not compile unused declarations, subroutines, procedures and functions, in order to save memory resources.
Initialize Variables On Declaration
If this option is enabled, Basic compiler will reset to zero all memory locations allocated for variables, at the position of their declaration in the basic program. This option is useful for beginners, because RAM memory is filled with random values at device power-up, and it is easy to make a mistake to assume that all variables are reset to zero at power-up. Experienced users can save some program memory, by disabling this option and taking control of variable initial values by user program where necessary.
Optimize Variables Declaration
This option will turn on the compiler internal routine that will optimize the variables declaration order based on the usage frequency of the variables. In this way, the most frequently used variables will be stored in higher RAM memory locations, resulting in possibly smaller size of the generated code.
• About variables
Five data types are supported:
Boolean - 1-byte, True or False
Short - 1-byte integers in the range -128 to 127
Integer - 2-byte integers in the range -32,768 to 32,767
Long - 4-byte integers in the range -2,147,483,648 to 2,147,483,647
Single - 4-byte single precision floating point numbers, 7 digits precision, IEEE754 standard
Variables can be global (declared in the main program, before the End statement) or local (declared in subroutines, procedures and functions). Variable name used for a variable with global scope can be used again for local variable names. The compiler will reserve separate memory locations for them. There are no other limits for the total number of variables, but 64K RAM memory. Variables are declared using DIM statement:
Dim a As Boolean
Dim b As Short
Dim c As Integer
Dim d As Long
Dim e As Single
Dim statement allows multiple declarations in one line of code by using comma-separated list of variable names:
Dim c1, c2, c3 As Integer
It is possible to use one-dimensional arrays for all variable types. For example:
Dim a(100) As Integer
declares an array of 100 Integer variables with array index in the range [0-99].
It is possible to make conversions between all data types (except Boolean) by simple assignment statements:
Dim a As Long
Dim b As Single
b = 123.456
a = b
This will result in variable A holding integer value 123.
Variables are stored by the compiler at the end of the RAM space. For systems with less than 64K RAM memory, the correct value should be assigned to the RAMEND parameter using the #define directive (alias: Define) to override its default value of 65535. For example:
#define RAMEND = 4095
Constants can be used in decimal number system with no special marks, in hexadecimal number system with leading 0x or leading $ notation (or with H at the end) and in binary system with leading % mark (or with B at the end). ASCII value of a character can be expressed in string format (e.g. "A"). Keywords True and False are also available for Boolean type constants. For example:
Dim a As Boolean
Dim b As Short
Dim c As Integer
a = True
b = %01010101
c = 0x55aa
c = "C"
Constants can be assigned to symbolic names using CONST directive. Constants can be global or local. One example:
Dim a As Single
Const pi = 3.14159
a = pi
It is possible to use comments in basic source programs. The comments must begin with single quote symbol (') and may be placed anywhere in the program.
Comment sign '//' is an alternative for the standard single quote sign.
Lines of assembler source code may be placed anywhere in basic source program and must begin with ASM: prefix. If labels are used, no space should be left between the ASM: prefix and the label. For example:
ASM: NOP
ASM:LABEL1: LD A,(BC)
Symbolic names of all variables and constants (global and local) can be used as arguments of assembler statements. The compiler will replace that symbolic name with the proper variable address or constant value:
Dim varname As Short
varname = 0
ASM: LD A,55H
ASM: LD (VARNAME),A
If large amount of assembler code should be used, it can be loaded from an external assembler file and included to the current program by using IncludeASM directive. Its only argument is a string containing the path to the external .ASM file. This can be the full path or only the file name, if the external file is located in the same folder as the current basic program file. During the compilation process the external assembler code will be appended to the current program at its end, and not at the position of the directive. Multiple files can be included with separate IncludeASM directives. External assembler files should not contain ASM: prefix used for inline assembler code. It is also strongly suggested not to use ORG directives in the external assembler code.
• Mathematical and logical operations
Five arithmetic operations (+, -, *, /, MOD) are available for integer data types. MOD operation is not applicable for Single data type variables. The compiler is able to compile all possible complex arithmetic expressions, including those containing math functions and user-defined functions. Arithmetic operations are allowed only in assignment statements and all variables in one such statement must be of the same data type. For example:
Dim a As Long
Dim b As Long
Dim c As Long
a = 1234
b = 2345
b = a * b
c = a * 100 - (a + b)
There are seven single precision mathematical functions (SQR, SIN, COS, TAN, EXP, LN, LOG) that can be used with Single data type variables. All math functions can also be used in complex math expressions. For example:
Dim a As Single
a = 2
a = Sqr(a)
For Boolean and Short data type variables seven basic logical operations are supported. It is possible to make only one logical operation in one single statement. Logical operations are allowed only in assignment statements. For example:
Example 1:
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean
a = True
b = False
c = Not a
c = a And b
c = a Or b
c = a Xor b
c = a Nand b
c = a Nor b
c = a Nxor b
Example 2:
Dim a As Short
a = 0x55
a = Not a
• Standard Basic language elements
Unconditional jumps are performed by GOTO statement. It uses line label name as argument. Line labels can be global or local. Line labels must be followed by colon mark ':'. Here is one example:
Dim a As Long
a = 0
loop: a = a + 1
Goto loop
Three standard BASIC statements are supported: FOR-TO-STEP-NEXT, WHILE-WEND and IF-THEN-ELSE-ENDIF. In FOR-TO-STEP-NEXT statement all variables must be Integer data type. Here are several examples:
Example 1:
Dim a As Integer
Dim b(100) As Single
For a = 0 To 99
b(a) = a
Next a
Example 2:
Dim a As Long
a = 100000
While a > 0
a = a - 1
Wend
Example 3:
Dim a As Integer
Dim b As Integer
For a = 0 To 10000
If a < 1000 Then
b = a
Else
b = 1000
Endif
Next a
For statement will accept all available variable types for the running variable. Exit For statement provides a way to exit a For-Next loop. It transfers control to the statement following the Next statement.
After IF-THEN statement in the same line can be placed almost every other possible statement and then ENDIF is not used. Six standard comparison operators are available: =, <>, >, >=, <, <=. There are no limits for the number of nested statements of any kind.
• Memory access
Standard BASIC elements for accessing memory are available: POKE statement and PEEK function. They can be used with integer constants and also with variables of Short, Integer or Long data type. For example:
Dim a As Integer
Dim b As Integer
Dim c As Integer
For a = 0 To 15
b = Peek(a)
c = 240 + a
Poke c, b
Next a
• Subroutines
Structured programs can be written using subroutine calls with GOSUB statements that use line label names as arguments. Return from a subroutine is performed by a RETURN statement. Users need to take care that the program structure is consistent. When using subroutines, the main routine must end with an END statement. The END statement is compiled as a HALT instruction. Here is an example:
Dim a As Integer
Dim b As Integer
b = 100
Gosub fillmemory
b = 101
Gosub fillmemory
End
fillmemory:
For a = 20000 To 21000
Poke a, b
Next a
Return
• Bit-oriented language elements
SETBIT and RESETBIT statements can be used to set or reset the individual bits in Short data type variables. The first argument is a Short variable that will be the target of the operation, and the second argument is target bit number and it must be a constant in the range 0-7.
Dim a As Short
a = 0xf0
SetBit a, 0
ResetBit a, 7
By using TESTBIT and MAKEBIT functions it is possible to assign a Boolean data type variable the value contained in the specific bit of a Short data type variable, and vice versa, to copy the value of a Boolean data type variable to the specific bit of a Short data type variable. The first argument of these functions is target bit number and it must be a constant in the range 0-7. For example:
Dim a As Boolean
Dim b As Short
a = TestBit(0, b)
b = MakeBit(7, a)
• Communication with I/O ports
The communication with the outside world is done using GET function and PUT and PRINT statements. The argument of the GET function is port number and must be a constant value in the range [0-255]. It can be used to assign the value received on the port to a variable of Short, Integer or Long data type. For example:
Dim a As Integer
a = Get(10)
PUT statement can be used to send data to the specified port. The data can be a constant value in the range [0-255] or contained in a variable of Short, Integer or Long data type. Only the lowest byte of the variable is sent to the port. For example:
Dim a As Integer
a = 200
Put 10, a
PRINT statement can be used to send string constants and decimal string representations of any supported data type variables to the specified port.
String constants should begin and end with the opening and closing double quotation mark. There are four symbolic string constants available: Qt (or """") for the double quotation mark (ASCII code 34), CrLf for the carriage return - line feed sequence (ASCII codes 13-10), Cr for the carriage return (ASCII code 13) and Lf for the line feed character (ASCII code 10). User-defined names for string constants with Const directive can also be used.
Here is an example:
Dim a As Single
a = 123.456
Print 10, "THE NUMBER IS "
Print 10, a
Print 10, CrLf
This can also be done using only one PRINT statement:
Print 10, "THE NUMBER IS ", a, CrLf
• Structured language support (procedures and functions)
Procedures can be declared with PROC statement. They can contain up to 5 arguments (comma separated list) and all available data types can be used for argument variables. Argument variables are declared locally, so they do not need to have unique names in relation to the rest of user basic program, that makes very easy to re-use once written procedures in other basic programs. The procedures can be exited with EXIT statement. They must be ended with END PROC statement and must be placed after the END statement in program. Calls to procedures are implemented with CALL statement. The list of passed arguments can contain both variables and numeric constants. For example:
Dim x As Integer
For x = 0 To 255
Call port_display(x)
Next x
End
Proc port_display(arg1 As Integer)
Print 10, "THE NUMBER IS ", arg1, CrLf
End Proc
All facts stated for procedures are valid for functions, also. Functions can be declared with FUNCTION statement. They can contain up to 5 arguments and argument variables are declared locally. Functions can be exited with EXIT statement and must be ended with END FUNCTION. The name of the function is declared as a global variable, so if the function is called with CALL statement, after its execution the function variable will contain the result. Standard way of function calls in assignment statements can be used, also. One simple example:
Dim x As Integer
Dim y As Integer
For x = 0 To 100
y = square(x) + 1
Next x
End
Function square(arg1 As Integer) As Integer
square = arg1 * arg1
End Function
The default mechanism is to pass an argument to a procedure by value. Either no prefix or ByVal prefix can be used for that argument. The procedure will use the argument value and will not change the input value of the calling variable. There are two passing mechanisms for passing arguments to procedures by reference - ByRef and ByRefOut. When ByRef prefix is used to pass a variable argument to a procedure, the procedure will use the input variable value, but it can also change the value of the variable during the procedure execution. The calling variable will be exposed to change. When ByRefOut prefix is used for the procedure argument, the input value of the calling variable will not be passed to the procedure at all. The procedure will only return an output value to the calling code through that argument. So, ByRefOut mechanism should be used when the procedure has a genuine need to output only a value to the calling code, and in that way the result will be a more optimized code compared to the use of the standard ByRef mechanism.
One test example:
Dim in_only As Short
Dim inc_me As Short
Dim add_inc_me_and_in_only As Short
in_only = 5
inc_me = 10
Call testbyref(in_only, inc_me, add_inc_me_and_in_only)
'after this call
'inc_me = 11
'add_inc_me_and_in_only = 16
End
Proc testbyref(arg1 As Short, ByRef arg2 As Short, ByRefOut arg3 As Short)
arg2 = arg2 + 1
arg3 = arg1 + arg2
End Proc
Procedures (and functions) can also be called without the Call statement. In that case, the procedure name should be followed by the comma-separated list of arguments.
The lines of code with the same effect:
Call port_display(x)
port_display x
Basic source code from an external file can be included to the current program by using INCLUDE directive. Its only argument is a string containing the path to the external .BAS file. This can be the full path or only the file name, if the external file is located in the same folder as the current basic program file. During the compilation process the external basic source will be appended to the current program. Multiple files can be included with separate INCLUDE directives. To maintain the overall basic code structure, it is strongly suggested that the external file contains global declarations, subroutines, procedures and functions, only. Here is one very simple example for the demonstration:
main.bas:
Dim i As Integer
Dim j As Integer
Include "inc1.bas"
Include "inc2.bas"
For i = 1 To 10
j = func1(i, 100)
Call proc1(j)
Next i
End
inc1.bas:
Dim total As Integer
Proc proc1(i As Integer)
total = total + i
End Proc
inc2.bas:
Function func1(i As Integer, j As Integer) As Integer
func1 = i + j
End Function