ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 12.06.2025
Просмотров: 8284
Скачиваний: 1
© MCS Electronics, 1995-2007
BASCOM Language Fundamentals
Changes compared to BASCOM-8051
The design goal was to make BASCOM-AVR compatible with BASCOM-8051.
For the AVR compiler some statements had to be removed.
New statements were also added. And some statements were changed.
They need specific attention, but the changes to the syntax will be made available to BASCOM-8051 too in the future.
Statements that were removed
STATEMENT |
DESCRIPTION |
||
$LARGE |
Not needed anymore. |
||
$ROMSTART |
Code always starts at address 0 for the AVR. Added again in 1.11.6.2 |
||
$LCDHEX |
Use LCD Hex(var) instead. |
||
$NOINIT |
Not needed anymore. Added in 1.11.6.2 |
||
$NOSP |
Not needed anymore |
||
$NOBREAK |
Can't be used anymore because there is no object code that can be used for |
||
it. |
|||
$OBJ |
Removed. |
||
BREAK |
Can't be used anymore because there is no object code that can be used for |
||
it. |
|||
PRIORITY |
AVR does no allow setting priority of interrupts |
||
PRINTHEX |
You can use Print Hex(var) now |
||
LCDHEX |
You can use Lcd Hex(var) now |
||
Statements that were added |
|||
STATEMENT |
DESCRIPTION |
||
FUNCTION |
You can define your own user FUNCTIONS. |
||
LOCAL |
You can have LOCAL variables in SUB routines or FUNCTIONS. |
||
^ |
New math statement. Var = 2 ^ 3 will return 2*2*2 |
||
SHIFT |
Because ROTATE was changed, I added the SHIFT statement. SHIFT |
||
works just like ROTATE, but when shifted left, the LS BIT is cleared and |
|||
the carry doesn't go to the LS BIT. |
|||
LTRIM |
LTRIM, trims the leftmost spaces of a string. |
||
RTRIM |
RTRIM, trims the rightmost spaces of a string. |
||
TRIM |
TRIM, trims both the leftmost and rightmost spaces of a string. |
||
Statements that behave differently |
|||
STATEMENT |
DESCRIPTION |
||
ROTATE |
Rotate now behaves like the ASM rotate, this means that the carry will go to |
||
the most significant bit of a variable or the least significant bit of a variable. |
|||
CONST |
String were added to the CONST statement. I also changed it to be |
||
compatible with QB. |
|||
page -169-
© MCS Electronics, 1995-2007 |
||
DECLARE |
BYVAL has been added since real subprograms are now supported. |
|
DIM |
You can now specify the location in memory of the variable. |
|
Dim v as byte AT 100, will use memory location 100. |
||
Language Fundamentals
Characters from the BASCOM character set are put together to formlabels, keywords, variables and operators.
These in turn are combined to form the statements that make up a program.
This chapter describes the character set and the format of BASCOM programlines. 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 consists of alphabetic characters, numeric characters, and special characters.
The alphabetic characters in BASCOM are the uppercase letters (A-Z) and lowercase ettersl (a-z) of the alphabet.
The BASCOM numeric characters are the digits 0-9.
The letters A-H 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) |
page -170-
© MCS Electronics, 1995-2007 |
||
> |
Greater than |
|
\ |
Backslash (integer/word division symbol) |
|
^ |
Exponent |
|
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. When there is a label on the line, no other identifiers may be used on the same line. So the label is the sole identifier on a line.
BASCOM Statements
A BASCOM statement is either "executable" or " non-executable".
An executable statement advances the flow of a programs logic by telling the programwhat to 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 non-executable statements:
REM or (starts a comment)
DIM
A "comment" is a non-executable statement used to clarify a programs operation and purpose.
A comment is introduced by the REM statement or a single quote character(').
page -171-
© MCS Electronics, 1995-2007
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). A bit can hold only the value 0 or 1. A group of 8 bits is called a 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. Ranging in value from1.5 x 10^–45 to 3.4 x 10^38
Double. Doubles are stored as signed 64 bit binary numbers. Ranging in value from5.0 x 10^–324 to 1.7 x 10^308
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) , external or in EEPROM.
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
page -172-
© MCS Electronics, 1995-2007
k = g
The value obtained by combining other variables, constants, and operators: Temp = a + 5
Temp = C + 5
The value obtained by calling a function: Temp = Asc(S)
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
The STRING type needs an additional parameter to specify the length.
You can also use DEFINT, DEFBIT, DEFBYTE ,DEFWORD ,DEFLNG or DEFSNG.
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 or string values.
Logical operators, used to test conditions or manipulate individual bits.
Functional operators, used to supplement simple operators.
page -173-
© MCS Electronics, 1995-2007
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 provided 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 the moment no message is produced, so you have to make sure yourself that this won't 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 programflow.
Operator |
Relation Tested |
Expression |
|
= |
Equality |
X = Y |
|
<> |
Inequality |
X <> Y |
|
< |
Less than |
X < Y |
|
> |
Greater than |
X > Y |
|
<= |
Less than or |
X <= Y |
|
equal to |
|||
>= |
Greater than or |
X >= Y |
|
equal to |
|||
Logical Operators
Logical operators perform tests on relations, bit manipulations, or Boolean operators.
page -174-
© MCS Electronics, 1995-2007
There four operators in BASCOM 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
19
11
Floating point SINGLE (4 BYTE)(ASM code used is supplied by Jack Tidwell) Single numbers conforming 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
Here is a fragment from the Microsoft knowledge base about FP:
Floating-point mathematics is a complex topic that confuses many programmers. The tutorial below should help you recognize programming situations where floating-point errors are likely to occur and how to avoid them. It should also allow you to recognize cases that are caused by inherent floating-point math limitations as opposed to actual compiler bugs.
page -175-
© MCS Electronics, 1995-2007
Decimal and Binary Number Systems
Normally, we count things in base 10. The base is completely arbitrary. The only reason that people have traditionally used base 10 is that they have 10 fingers, which have made handy counting tools.
The number 532.25 in decimal (base 10) means the following:
(5 * 10^2) + (3 * 10^1) + (2 * 10^0) + (2 * 10^-1) + (5 * 10^-2)
500 + 30 + 2 + 2/10 + 5/100
_________
= 532.25
In the binary number system (base 2), each column represents a power of 2 instead of 10. For example, the number 101.01 means the following:
(1 * 2^2) + (0 * 2^1) + (1 * 2^0) + (0 * 2^-1) + (1 * 2^-2) 4 + 0 + 1 + 0 + 1/4
_________
= 5.25 Decimal
How Integers Are Represented in PCs
-----------------------------------
Because there is no fractional part to an integer, its machine representation is much simpler than it is for floating-point values. Normal integers on personal computers (PCs) are 2 bytes (16 bits) long with the most significant bit indicating the sign. Long integers are 4 bytes long.
Positive values are straightforward binary numbers. For example:
1 Decimal = 1 Binary
2 Decimal = 10 Binary
22 Decimal = 10110 Binary, etc.
However, negative integers are represented using the two's complement scheme. To get the two's complement representation for a negative number, take the binary representation for the number's absolute value and then flip all the bits and add 1. For example:
4 Decimal = 0000 0000 0000 0100
1111 1111 1111 1011 Flip the Bits
-4 = 1111 1111 1111 1100 Add 1
Note that adding any combination of two's complement numbers together
using ordinary binary arithmetic produces the correct result.
page -176-