Language Fundamentals

 

Characters from the BASCOM character set are put together to form labels,
keywords, variables and operators.
These in turn combine to form statements that make up a program.

This chapter describes the character set and the format of BASCOM program lines. In particular, it discusses:

· The specific characters in the character set and the special meanings of some characters.
· The format of a line in a BASCOM program.
· Line labels.
· Program line length.

Character Set

The BASCOM BASIC character set consist of alphabetic characters,
numeric characters, and special characters.

The alphabetic characters in BASCOM are the uppercase letters (A-Z) and
lowercase letters (az) of the alphabet.

The BASCOM numeric characters are the digits 0-9.
The letters can be used as parts of hexadecimal numbers.
The following characters have special meanings in BASCOM statements and expressions:

Character
name

ENTER Terminates input of a line
Blank ( or space)
' Single quotation mark (apostrophe)
* Asterisks (multiplication symbol)
+ Plus sign
, Comma
- Minus sign
. Period (decimal point)
/ Slash (division symbol) will be handled as \
: Colon
" Double quotation mark
; Semicolon
< Less than
= Equal sign (assignment symbol or relational operator)
> Greater than
\ Backslash (integer/word division symbol)
The BASCOM program line
BASCOM program lines have the following syntax:

[[line-identifier]] [[statement]] [[:statement]] ... [[comment]]


Using Line Identifiers

BASCOM support one type of line-identifier; alphanumeric line labels:

An alphabetic line label may be any combination of from 1 to 32 letters and digits, starting with a letter and ending with a colon.
BASCOM keywords are not permitted. The following are valid alphanumeric line labels:


Alpha:
ScreenSUB:
Test3A:

Case is not significant. The following line labels are equivalent:

alpha:
Alpha:
ALPHA:

Line labels may begin in any column, as long as they are the first characters other than blanks on the line.
Blanks are not allowed between an alphabetic label and the colon following it.
A line can have only one label.


BASCOM Statements

A BASCOM statement is either " executable" or " nonexecutable" .
An executable statement advances the flow of a programs logic by telling the program what tot do next.

Non executable statement perform tasks such as allocating storage for variables, declaring and defining variable types.

The following BASCOM statements are examples of nonexecutable statements:

· REM or (starts a comment)
· DIM

A " comment" is a nonexecutable statement used to clarify a programs operation and purpose.
A comment is introduced by the REM statement or a single quote character(').
The following lines are equivalent:

PRINT " Quantity remaining" : REM Print report label.
PRINT " Quantity remaining" ' Print report label.

More than one BASCOM statement can be placed on a line, but colons(:) must separate statements, as illustrated below.

FOR I = 1 TO 5 : PRINT " Gday, mate." : NEXT I


BASCOM LineLength
If you enter your programs using the built-in editor, you are not limited to any line length, although it is advised to shorten your lines to 80 characters for clarity.

Data Types
Every variable in BASCOM has a data type that determines what can be stored in the variable. The next section summarizes the elementary data types.


Elementary Data Types

· Bit (1/8 byte)
· Byte (1 byte).

Bytes are stores as unsigned 8-bit binary numbers ranging in value from 0 to 255.

· Integer (two bytes).

Integers are stored as signed sixteen-bit binary numbers ranging in value from -32,768 to +32,767.

· Word (two bytes).

Words are stored as unsigned sixteen-bit binary numbers ranging in value from 0 to 65535.

· Long (four bytes).

Longs are stored as signed 32-bit binary numbers ranging in value from -2147483648 to 2147483647.

· Single

Singles are stored as signed 32 bit binary numbers.

· String (up to 254 bytes).

Strings are stored as bytes and are terminated with a 0-byte.
A string dimensioned with a length of 10 bytes will occupy 11 bytes.

Variables can be stored internal (default) or external.


Variables
A variable is a name that refers to an object--a particular number.

A numeric variable, can be assigned only a numeric value (either integer,byte long, single or bit).
The following list shows some examples of variable assignments:

· A constant value:

A = 5
C = 1.1

· The value of another numeric variable:

abc = def
k = g

· The value obtained by combining other variables, constants, and

operators:

Temp = a + 5
Temp = C + 5

Variable Names
A BASCOM variable name may contain up to 32 characters.
The characters allowed in a variable name are letters and numbers.
The first character in a variable name must be a letter.

A variable name cannot be a reserved word, but embedded reserved words are allowed.
For example, the following statement is illegal because AND is a reserved word.

AND = 8

However, the following statement is legal:


ToAND = 8

Reserved words include all BASCOM commands, statements, function names, internal registers and operator names.
(see BASCOM Reserved Words , for a complete list of reserved words).

You can specify a hexadecimal or binary number with the prefix &H or &B.
a = &HA , a = &B1010 and a = 10 are all the same.

Before assigning a variable you must tell the compiler about it with the DIM statement.

Dim b1 As Bit, I as Integer, k as Byte , s As String * 10


You can also use DEFINT, DEFBIT, DEFBYTE and/or DEFWORD.
For example DEFINT c tells the compiler that all variables that are not dimensioned and that are beginning with the character c are of the Integer type.



Expressions and Operators
This chapter discusses how to combine, modify, compare, or get information about expressions by using the operators available in BASCOM.

Anytime you do a calculation you are using expressions and operators.

This chapter describes how expressions are formed and concludes by describing the following kind of operators:

· Arithmetic operators, used to perform calculations.
· Relational operators, used to compare numeric values.
· Logical operators, used to test conditions or manipulate individual bits.
· Functional operators, used to supplement simple operators.



Expressions and Operators
An expression can be a numeric constant, a variable, or a single value
obtained by combining constants, variables, and other expressions with operators.

Operators perform mathematical or logical operations on values.
The operators provides by BASCOM can be divided into four categories, as follows:

1. Arithmetic
2. Relational
3. Logical
4. Functional



Arithmetic
Arithmetic operators are +, - , * and \.

· Integer

Integer division is denoted by the backslash (\).
Example: Z = X \ Y

· Modulo Arithmetic

Modulo arithmetic is denoted by the modulus operator MOD.
Modulo arithmetic provides the remainder, rather than the quotient, of an
integer division.
Example: X = 10 \ 4 : remainder = 10 MOD 4

· Overflow and division by zero

Division by zero, produces an error.

At this moment there is no message, so you have to insure yourself that such wont happen.

Relational Operators
Relational operators are used to compare two values as shown in the table below.
The result can be used to make a decision regarding program flow.


Operator Relation Tested Expression
= Equality X = Y
<> Inequality X <> Y
< Less than X < Y
> Greater than X > Y
<= Less than or equal to X <= Y
>= Greater than or equal to X >= Y

Logical Operators
Logical operators perform tests on relations, bit manipulations, or Boolean operators.
There are four operators in BASCOM, they are :

Operator Meaning
NOT Logical complement
AND Conjunction
OR Disjunction
XOR Exclusive or


It is possible to use logical operators to test bytes for a particular bit pattern.
For example the AND operator can be used to mask all but one of the bits
of a status byte, while OR can be used to merge two bytes to create a particular binary value.

Example
A = 63 And 19
PRINT A
A = 10 Or 9
PRINT A

Output
16
11


Floating point
A new datatype is added to BASCOM : the single.
Single numbers conform to the IEEE binary floating point standard.

An eight-bit exponent and 24 bit mantissa are supported.
Using four bytes, the format is shown below:

31 30________23 22______________________________0
s exponent mantissa

The exponent is biased by 128. Above 128 are positive exponents and
below are negative. The sign bit is 0 for positive numbers and 1 for
negative. The mantissa is stored in hidden bit normalized format so
that 24 bits of precision can be obtained.

All mathematical operations are supported by the single.

You can also convert a single to an integer or word or vise versa:
Dim I as Integer, S as Single
S = 100.1 'assign the single
I = S 'will convert the single to an integer
Take a look at the single.bas example for more information.


Arrays

Arrays are also a new extension to BASCOM.
An array is a set of sequentially indexed elements having the same type. Each element of an array has a unique index number that identifies it. Changes made to an element of an array do not affect the other elements.

The index must be a numeric constant, a byte, an integer or a word. This means that an array can hold 65535 elements as a maximum. The minimum value is 1 and not zero as in QB.

Arrays can be used on each place where a 'normal' variable is expected but there are a few exceptions.
These exceptions are shown in the help topics.
Note that there are no BIT arrays in BASCOM-8051.
Example:
Dim a(10) as byte 'make an array named a,with 10 elements (1 to 10)
Dim c as Integer

For C = 1 To 10
a(c) = c 'assign array element
Print a(c) 'print it
Next

Strings

Strings can be up to 254 characters long in BASCOM.
To save memory you must specify how long each string must be with the DIM statement.
Dim S As String * 10
This will reserve space for the string S with a length of 10 bytes. The actually length is 11 bytes because a nul(0) is used to terminate the string.

You can concatenate string with the + sign.

Dim S As String * 10 , Z As String * 10
S = "test"
Z = S + "abc"

In QB you can assign a string with a value and add the original string (or a part of it) too :.
S = "test"
S = "a" + s

This will result in the string "atest"
In BASCOM this is NOT possible because this would require a copy of the string.
In BASCOM the string S is assigned with "a" and on that moment the original string S is destroyed. So you must make a copy of the string yourself in the event you need this functionality.