N O T I C E The following text is from the MCS BASIC-52 USER'S MANUAL and is reprinted by permission of Intel Corporation, Copyright 1989 Intel Corporation. Intel reserves all rights to the materials not expressly granted herein, including the right to grant third parties the right to use the materials. 1.4 Definition of Terms COMMANDS: MCS BASIC-52 operates in two modes, the COMMAND or direct mode and the interpreter or RUN mode. MCS BASIC-52 commands can only be entered when the processor is in the COMMAND or direct mode. MCS BASIC-52 takes immediate action after a command has been entered. This document will use the terms RUN mode and COMMAND mode to refer to the two different modes of operation. STATEMENTS: A BASIC program is comprised of statements. Every statement begins with a line number, followed by the statement body, and terminated with a carriage return (cr), or a colon (:) in the case of multiple statements per line. Some statements can be executed in the COMMAND MODE, others cannot. The DESCRIPTION OF STATEMENTS section of this manual describes whether a statement can be executed in the COMMAND mode or only in the RUN mode. There are three general types of statements in MCS BASIC-52: ASSIGNMENTS, INPUT/OUTPUT, and CONTROL. The DESCRIPTION OF STATEMENTS section of this manual explains what type is associated with each statement. * EVERY line in a program must have a statement line number ranging between 0 and 65535 inclusive. * statement numbers are used by BASIC to order the program statements sequentially. * in any program, a statement number can be used only once. * statements need not be entered in numerical order, because BASIC will automatically order them in ascending order. * a statement may contain no more than 72 characters in version 1.0 and no more than 79 in version 1.1. * blanks (spaces) are ignored by BASIC and BASIC automatically inserts blanks during LIST. * more than one statement can be put on a line, if separated by a colon (:), but only one statement number is allowed per line. FORMAT STATEMENTS: Format statements may only be used within the PRINT STATEMENT. The format statements include TAB([expr]), SPC([expr]), USING (special symbols), and CR (carriage return with no line feed). Details of the format statements are provided in the description of the PRINT STATEMENT section of this manual. 1.4 DEFINITION OF TERMS DATA FORMAT: The range of numbers that can be represented in MCS BASIC-52 is: +,-1e-127 to .99999999e + 127. There are eight digits of significance in MCS BASIC-52. Numbers are internally rounded to fit this precision. Numbers may be entered and displayed in four formats: integer, decimal, hexadecimal, and exponential. Example: 129, 34.98, 0A6EH, 1.23456E+3 INTEGERS: In MCS BASIC-52, integers are numbers that ranges from 0 to 65535 or 0FFFFH. All integers can be entered in either decimal or hexadecimal format and all hexadecimal numbers must begin with a valid digit (e.g. the number A000H must be entered 0A000H). When an operator, such as .AND. Requires an integer, MCS BASIC-52 will truncate the fraction portion of number so it will fit the integer format. All line numbers used by MCS BASIC-52 are integers. This document will refer to integers and line numbers, respectively in the following manner: [integer]-[ln num] NOTE -throughout this document the brackets[] are used only to indicate an integer, constant, etc. They are not entered when typing the actual number or variable. CONSTANTS: A constant is a real number that ranges from: +,- 1 e-127 to +,-.99999999e + 127 A constant, of course, can be an integer. This document will refer to constants in the following manner: [const] OPERATORS: An operator performs a pre-defined operation on variables and/or constants. Operators require either one or two operands. Typical two operand or dyadic operators include ADD(+), SUBTRACT(-), MULTIPLY(*),and DIVIDE(/). Operators that require only one operand are often referred to as UNARY OPERATORS. Some typical UNARY OPERATORS are SIN, COS, and ABS. 1.4 definition of terms VARIABLES: In version 1.0 of MCS BASIC-52 a variable could be defined as either a letter, (i.e. a, x, I), a letter followed by a number, (i.e. q1, t7, l3), a letter followed by a one dementioned expression, (i.e. j(4), g(a+6), I(10*sin(x))), or a letter followed by a number followed by a one dimentioned expression (i.e. a1(8),p7(dby(9)), w8(a+b). In version 1.11 variables can be defined in the same manner as in version 1.0, however variables may also contain up to 8 letters or numbers including the underline character. This permits the user to use a more descriptive name for a given variable. Examples of valid variables in version 1.1 of MCS BASIC-52 are as follows: FRED VOLTAGE1 I_I1 array(ele_1) When using the expanded variable names available in version 1.1 of MCS BASIC-52 it is important to note that 1) it takes longer for MCS BASIC-52 to process these expanded variable names and 2) the user may not use any keyword as part of a variable name(i.e. the variables TABLE and DIET could not be used because TAB and IE are reserved words). BAD SYNTAX ERRORS will be generated if the user attempts to define a variable that contains a reserved word. Variables that include a ONE DIMENTIONED expression [expr] are often referred to as DIMENTIONED or ARRAYED variables. Variables that only involve a letter or a letter and a number are called SCALAR variables. The details concerning dimentioned variables are covered in the description of the STATEMENT ROUTINE DIM. This document will refer to VARIABLES as: [var]. MCS BASIC-52M allocates variables in a "static" manner. That means each time a variable is used, BASIC allocates a portion of memory (8 bytes) specifically for that variable. This means memory cannot be de-allocated on a variable by variable basis. That means if you execute a statement like q=3, later on you cannot tell basic that the variable q no longer exists so, please "free up" the 8 bytes of memory that belong to q. Sorry, it doesn't work this way. The only way the user can clear the memory that is allocated to variables is to execute a CLEAR STATEMENT. This statement "frees" all memory allocated to variables. IMPORTANT NOTE: Relative to a dimentioned variable, it takes MCS BASIC-52 a lot less time to find a scalar variable. That's because there is no expression to evaluate in a scalar variable. So, if you want to make a program run as fast as possible, use dimentioned variables only when you have to. Use scalars for intermediate variables, then assign the final result to a dimentioned variable. EXPRESSIONS: An expression is a logical mathematical formula that involves OPERATORS (both unary and dyadic), CONSTANTS, and VARIABLES. Expressions can be simple or quite complex, i.e. 12*exp(a)/100, h(1)+55, or (sin(a)*sin(a)+cos(a)*cos(a))/2. A "stand alone" variable [var] or constant [const] is also considered an EXPRESSION. This document will refer to EXPRESSIONS as: [expr]. 1.4 DEFINITION OF TERMS RELATIONAL EXPRESSIONS: Relational expressions involve the operators EQUAL (=), NOT EQUAL(<>), GREATER THAN (>), LESS THAN (<), GREATER THAN OR EQUAL TO (>=) and less than or equal to (<=). They are used in control statements to "test" a condition(ie. If a < 100 then...). Relational expressions always require two operands. This document will refer to relational expressions as: [rel expr]. SPECIAL FUNCTION OPERATORS: Virtually all of the special function registers on the 8052AH can be accessed by using the special function operators. The exceptions are PORTS 0,2 and 3 and non-I/O associated registers such as ACC, B, and PSW. Other SPECIAL FUNCTION OPERATORS are XTAL and TIME. Details of the SPECIAL FUNCTION OPERATORS are covered in the section SPECIAL FUNCTION OPERATORS. SYSTEM CONTROL VALUES: The system control values include the following: LEN (which returns the length of the program), FREE (which designates how many bytes of RAM are not used that are allocated to BASIC), and MTOP (which is the last memory location that assigned to BASIC), details of the system control values are covered in the section SYSTEM CONTROL VALUES. 2.1 DESCRIPTION OF COMMANDS COMMAND: RUN(cr) ACTION TAKEN: After RUN(cr) is typed all variables are set equal to zero, all BASIC evoked interrupts are cleared and program execution begins with the first line number of the selected program. The RUN command and the GOTO statement are the only way the user can place the MCS BASIC-52 interpreter into the RUN mode from the COMMAND mode. Program execution may be terminated at any time by typing a control-C on the console device. VARIATIONS: Unlike some Basic interpreters that allow a line number to follow the RUN command (i.e., RUN 100), MCS BASIC-52 does not permit such a variation on the RUN command. Execution always begins with the first line number. To obtain the same functionality as the RUN [ln num] command, use the GOTO[ln num] statement in the direct mode. SEE STATEMENT GOTO. EXAMPLE: >10 FOR 1=1 TO 3 >20 PRINT I >30 NEXT I >RUN 1 2 3 READY > 2.2 DESCRIPTION OF COMMANDS: COMMAND: CONT(cr) ACTION TAKEN: If a program is stopped by typing a control-C on the console device or by execution of a STOP statement, you can resume execution of the program by typing CONT(cr). Between the stopping and the restarting of the program you may display the values of variables or change the values of variables. However, you may NOT CONTinue if the program is modified during the STOP or after an error. VARIATIONS: None. EXAMPLE: >10 FOR I=I TO 10000 >20 PRINT I >30 NEXT I >RUN 1 2 3 4 5 - (TYPE CONTROL-C ON CONSOLE) STOP - IN LINE 20 READY >PRINT I 6 >I=10 >CONT 10 11 12 2.3 DESCRIPTION OF COMMANDS: COMMAND: LIST(cr) ACTION TAKEN: The LIST(cr) command prints the program to the console device. Note that the list command "formats" the program in an easy to read manner. Spaces are inserted after the line number and before and after statements. This feature is designed to aid in the debugging of MCS BASIC-52 programs. The "listing" of a program may be terminated at anytime by typing a control-C on the console device. VARIATIONS: Two variations of the LIST COMMAND are possible with MCS BASIC-52. They are: LIST [ln num] (cr) and LIST [in num] - [In num] (cr) The first variation causes the program to be printed from the designated line number (integer) to the end of the program. The second variation causes the program to be printed from the first line number (integer) to the second line number (integer). NOTE - the two line numbers MUST BE SEPARATED BY A DASH (-). EXAMPLE: READY >LIST 10 PRINT "LOOP PROGRAM" 20 FOR I=1 TO 3 30 PRINT I 40 NEXT I 50 END READY >LIST 30 30 PRINT I 40 NEXT I 50 END READY >LIST 20-40 20 FOR I=1 TO 3 30 PRINT I 40 NEXT I 2.6 DESCRIPTION OF COMMANDS COMMAND: NEW(cr) ACTION TAKEN: When NEW(cr) is entered, MCS BASIC-52 deletes the program that is currently stored in RAM memory. in addition, all variables are set equal to ZERO, all strings and all BASIC evoked interrupts are cleared. The REAL TIME CLOCK, string allocation, and the internal stack pointer value (location 3EH) are NOT effected. In general, NEW (cr) is used simply to erase a program and all variables. 4.2 DESCRIPTION OF STATEMENTS STATEMENT: CALL (integer) MODE: COMMAND AND/OR RUN TYPE: CONTROL The CALL [integer] STATEMENT is used to call an assembly language program. The integer following CALL is the address where the user must provide the assembly language routine. To return to BASIC the user must execute an assembly language RET instruction. Examples of how to use the CALL [integer] instruction are given in the ASSEMBLY LANGUAGE LINKAGE section of this manual. EXAMPLE: CALL 9000H Will cause the 8O52AH to execute the assembly language program beginning at location 9OOOH (i.e. the program counter will be loaded with 9OOOH). VARIATIONS: (VERSION 1.1 ONLY) If the integer following the CALL statement is between 0 and 127 (7FH), Version 1.1 of MCS BASIC-52 will multiply the user integer by two, then add 4l00H and vector to that location. This means that CALL 0 will call location 4lOOH, CALL 1 will call 4102H, CALL 2 - 4104H and so on. This permits the user to generate a simple table of assembly language routines without having to enter 4 digit hex integers after the CALL statement from the user supplied RESET routine. 4.3 DESCRIPTION OF STATEMENTS STATEMENT: CLEAR MODE: COMMAND AND/OR RUN TYPE: CONTROL The CLEAR STATEMENT sets all variables equal to 0 and resets all BASIC evoked interrupts and stacks. This means that after the CLEAR statement is executed an ONEX 1 or ONTIME statement must be executed before MCS BASIC-52 will acknowledge interrupts. ERROR trapping via the ONERR statement will also not occur until an ONERR[integer] STATEMENT is executed. The CLEAR STATEMENT does not affect the real time clock that is enabled by the CLOCK1 STATEMENT. CLEAR also does not reset the memory that has been allocated for STRINGS, so it is NOT necessary to enter the STRING [expr], [expr] STATEMENT to re-allocate memory for strings after the CLEAR STATEMENT is executed. In general, CLEAR is simply used to "erase" all variables. VARIATIONS: None. 4.4 DESCRIPTION OF STATEMENTS STATEMENTS: CLEARI (clear interrupts) CLEARS (clear stacks) MODE: COMMAND AND/OR RUN TYPE: CONTROL CLEARI The CLEARI STATEMENT clears all of the BASIC evoked interrupts. Specifically, the ONTIME and ONEX1 interrupts are DISABLED after the CLEARI STATEMENT is executed. This is accomplished by clearing bits 2 and 3 of the 8052AH's special function register, IE and by clearing the status bits that determine whether MCS BASIC-52 or the user is controlling these interrupts. The real time clock which is enabled by the CLOCK1 STATEMENT is not affected by CLEARI. This statement can be used to selectively DISABLE interrupts during specific sections of the users BASIC program. The ONTIME and/or ONEXl STATEMENTS MUST BE EXECUTED AGAIN before the specific interrupts will be enabled. CLEARS The CLEARS statement RESETS all of MCS BASIC-52's STACKS. The CONTROL and ARGUMENT STACKS are reset to their initialization value, 254 (0FEH) and 510 (1FEH) respectively. The INTERNAL STACK (the 8052AH's STACK POINTER, SPECIAL FUNCTION REGISTER-SP) is loaded with the value that is in INTERNAL RAM location 62 (3EH). This statement can be used to "purge" the stack should an error occur in a subroutine. In addition, this statement can be used to provide a "special" exit from a FOR-NEXT, DO-WHILE, or DO-UNTIL loop. EXAMPLE OF CLEARS: >10 PRINT "MULTIPLICATION TEST. YOU HAVE 3 SECONDS" >20 FOR I = 2 TO 9 >30 N = INT(RND*10) : A = N*I >40 PRINT "WHAT IS ",N,"*",I,"?": CLOCK1 >50 TIME = 0 : ONTIME 5,200 : INPUT R: IF R<>A THEN 100 >60 PRINT "THAT'S RIGHT":TIME=0: NEXT I >70 PRINT "YOU DID IT. GOOD JOB":END >100 PRINT "WRONG. TRY AGAIN": GOTO 50 >200 REM WASTE CONTROL STACK. TOO MUCH TIME >210 CLEARS: PRINT "YOU TOOK TOO LONG":GOTO 10 NOTE: When the CLEARS and CLEARI STATEMENTS are LISTED they will appear as CLEAR 5 and CLEAR I respectively. Don't he alarmed, that is the way it's supposed to work. 4.5 DESCRIPTION OF STATEMENTS STATEMENTS: CLOCK1 and CLOCKO MODE: COMMAND AND/OR RUN TYPE: CONTROL CLOCK1 The CLOCK1 STATEMENT enables the REAL TIME CLOCK feature resident on the MCS BASIC-52 device. The special function operator TIME is incremented once every 5 milliseconds after the CLOCK1 STATEMENT has been executed. The CLOCK1 STATEMENT uses TIMER/COUNTER 0 in the 13-bit mode to generate an interrupt once every 5 milliseconds. Because of this, the special function operator TIME has a resolution of 5 milliseconds. MCS BASIC-52 automatically calculates the proper reload value for TIMER/COUNTER 0 after the crystal value has been assigned (i.e. XTAL value). If no crystal value is assigned, MCS BASIC-52 assumes a value of 11.0592MHz. The special function operator TIME counts from 0 to 65535.995 seconds. After reaching a count of 65535.995 seconds TIME overflows back to a count of zero. Because the CLOCK1 STATEMENT uses the interrupts associated with TIMER/COUNTER 0 (the CLOCK1 statement sets bits 7 and 2 in the 8052AH's special function register, ie), the user may not use this interrupt in an assembly language routine if the CLOCK1 STATEMENT is executed in BASIC. The interrupts associated with the CLOCK1 STATEMENT cause MCS BASIC-52 programs to run at about 99.6% of normal speed. That means that the interrupt handling for the REAL TIME CLOCK feature only consumes about .4% of the total CPU time. This very small interrupt overhead is attributed to the very fast and effective interrupt handling of the 8052AH device. CLOCK0 The CLOCK0 (zero) STATEMENT disables or "turns off" the REAL TIME CLOCK feature. This statement clears bit 2 in the 8052AH's special function register, IE. After CLOCK0 is executed, the special function operator TIME will no longer increment. The CLOCK0 STATEMENT also returns control of the interrupts associated with TIMER/COUNTER 0 back to the user, so this interrupt may be handled at the assembly language level. CLOCK0 is the only MCS BASIC-52 statement that can disable the REAL TIME CLOCK. CLEAR and CLEAR1 will NOT disable the REAL TIME CLOCK. VARIATIONS: None. 4.6 DESCRIPTION OF STATEMENTS STATEMENTS: DATA - READ - RESTORE MODE: RUN TYPE: ASSIGNMENT DATA DATA specifies expressions that may be retrieved by a READ STATEMENT. If multiple expressions per line are used, they MUST be separated by a comma. READ READ retrieves the expressions that are specified in the DATA STATEMENT and assigns the value of the expression to the variable in the READ STATEMENT. The READ STATEMENT MUST ALWAYS be followed by one or more variables. If more than one variable follows a READ STATEMENT, they MUST be separated by a comma. RESTORE RESTORE "resets" the internal read pointer back to the beginning of the data so that it may be read again. EXAMPLE: >10 FOR I=1 TO 3 >20 READ A,B >30 PRINT A,B >40 NEXT I >50 RESTORE >60 READ A,B >70 PRINT A,B >80 DATA 10,20,10/2,20/2,SIN(PI),COS(PI) >RUN 10 20 5 10 0 -1 10 20 VARIATIONS: None. 4.6 DESCRIPTION OF STATEMENTS Explanation of previous example: Every time a READ STATEMENT is encountered the next consecutive expression in the DATA STATEMENT is evaluated and assigned to the variable in the READ STATEMENT. DATA STATEMENTS may be placed anywhere within a program,they will NOT be executed nor will they cause an error. DATA STATEMENTS are considered to be chained together and appear to be one BIG DATA STATEMENT If at anytime all the DATA has been read and another READ STATEMENT is executed then the program is terminated and the message ERROR: NO DATA - IN LINE XX is printed to the console device. MCS-BASIC-52 4.7 DESCRIPTION OF STATEMENTS STATEMENT: DIM MODE: COMMAND AND/OR RUN TYPE: ASSIGNMENT DIM reserves storage for matrices. The storage area is first assumed to be zero. Matrices in MCS BASIC- 52 may have only ONE DIMENSION and the size of the dimentioned array MAY NOT exceed 254 elements. Once a variable is dimentioned in a program it may not be re-dimentioned. An attempt to redimension an array will cause an ARRAY SIZE ERROR. If an arrayed variable is used that has NOT been dimentioned by the DIM STATEMENT, BASIC will assign a default value of 10 to the array size. All arrays are set equal to zero when the RUN COMMAND, NEW COMMAND, or the CLEAR STATEMENT is executed. The number of bytes allocated for an array is 6 times the (array size plus 1). So, the array A(l00) would require 606 bytes of storage. Memory size usually limits the size of a dimentioned array. VARIATIONS: More than one variable can be dimentioned by a single DIM STATEMENT, i.e., DIM A(10), B(15), AI(20). EXAMPLE: DEFAULT ERROR ON ATTEMPT TO RE-DIMENSION ARRAY >10 A(5)=10 -BASIC ASSIGNS DEFAULT OF 10 TO ARRAY SIZE -ERROR >20 DIM A(5) - ARRAY CANNOT BE RE-DIMENTIONED >RUN ERROR: ARRAY SIZE - IN LINE 20 20 DIM A(~) MCS BASIC-52 4.8 DESCRIPTION OF STATEMENTS STATEMENTS: DO - UNTIL [rel expr] MODE: RUN TYPE: CONTROL The DO - UNTIL [rel exprj instruction provides a means of "loop control" within an MCS BASIC-52 program. All statements between the DO and the UNTIL [rel expr] will be executed until the relational expression following the UNTIL statement is TRUE. DO - UNTIL loops may be nested. EXAMPLES: SIMPLE DO-UNTIL NESTED D0-UNTIL >10 A=0 >10 DO : A=A+1 : DO : B=B+1 >20 DO >20 PRINT A,B,A*B >30 A=A+1 >30 UNTIL B=3 >40 PRINT A >40 B=0 >50 UNTIL A=4 >50 UNTIL A=3 >60 PRINT "DONE" >RUN >RUN 1 1 1 1 1 2 2 2 1 3 3 3 2 1 2 4 2 2 4 DONE 2 3 6 3 1 3 READY 3 2 6 3 3 9 READY VARIATIONS: None MCS-BASIC-52 4.9 DESCRIPTION OF STATEMENTS STATEMENTS: DO - WHILE [ret expr] MODE: RUN TYPE: CONTROL The DO - WHILE [rel expr] instruction provides a means of "loop control" within an MCS BASIC-52 program. This operation of this statement is similar to the DO - UNTIL [rel expr] except that all statements between the DO and the WHILE [rel expr] will be executed as long as the relational expression following the WHILE statement is true. DO - WHILE and DO - UNTIL statements can be nested. EXAMPLES: SIMPLE DO-WHILE NESTED DO-WHILE - DO-UNTIL >10 DO >10 DO : A=A+1 : B=B+1 >20 A=A+1 >20 PRINT A,B,A*B >30 PRINT A >30 WHILE B<>3 >40 WHILE A<4 >40 B=0 >50 PRINT "DONE" >50 UNTIL A=0 >RUN >RUN 1 1 1 1 2 1 2 2 3 1 3 3 4 2 1 2 DONE 2 2 4 2 3 6 READY 3 1 3 > 3 2 6 3 3 9 READY > VARIATIONS: None MCS-BASIC-52 4.11 DESCRIPTION OF STATEMENTS STATEMENTS: FOR - TO - (STEP} - NEXT MODE: RUN VERSION 1.0 (COMMAND AND/OR RUN in Version 1.1) TYPE: CONTROL The FOR - TO - (STEP} - NEXT STATEMENTS are used to set up and control loops. EXAMPLE: 10 FOR A=B TO C STEP D 20 PRINT A 30 NEXT A If B=0, C= 10, and D=2, the PRINT STATEMENT at line 20 will be executed 6 times. The values of "A" that will be printed are 0, 2, 4, 6, 8, 10. "A" represents the name of the index or loop counter. The value of "B" is the starting value of the index, the value of "C" is the limit value of the index, and the value of "D" is the increment to the index. If the STEP STATEMENT and the value "D" are omitted, the increment value defaults to 1, therefore, STEP is an optional statement. The NEXT STATEMENT causes the value of "D" to be added to the index. The index is then compared to the value of "C," the limit. If the index is less than or equal to the limit, control will be transferred back to the statement after the FOR STATEMENT. Stepping "backwards" (i.e. FOR I = 100 TO I STEP-l) is permitted in MCS BASIC-52. Unlike some BASICS, the index MAY NOT be omitted from the NEXT STATEMENT in MCS BASIC-52 (i.e. the NEXT statement MUST always be followed by the appropriate variable). EXAMPLES: >10 FOR 1=1 TO 4 >10 FOR 1=0 TO 9 STEP 2 >20 PRINT I >20 PRINT I >30 NEXT I >30 NEXT I >RUN >RUN 1 0 2 2 3 4 4 6 8 READY READY > > MCS-BASIC-52 4.11 DESCRIPTION OF STATEMENTS In Version 1.1 of MCS BASIC-52 it is possible execute the FOR-TO-(STEP}.NEXT statement in the Command Mode. This makes it possible for the user to do things like display regions of memory by writing a short program like FOR 1=512 TO 560: PHO. XBY(I),: NEXT I. It may also have other uses, but they haven't been thought of. Also Version 1.1 allows the NEXT statement to be used without a variable following the statement. This means that programs like: EXAMPLE: 10 FOR I = 1 TO 100 20 PRINT I 30 NEXT Are permitted in Version 1.1 of MCS BASIC-52. The variable associated with the NEXT is always assumed to be the variable used in the last FOR statement. MCS-BASIC-52 4.12 DESCRIPTION OF STATEMENTS STATEMENTS: GOSUB[ln num] - RETURN MODE: RUN TYPE: CONTROL GOSUB The GOSUB [Ln num] STATEMENT will cause MCS BASIC-52 to transfer control of the program directly to the line number ([ln num]) following the GOSUB STATEMENT. In addition, the GOSUB STATEMENT saves the location of the STATEMENT following GOSUB on the control stack so that a RETURN STATEMENT can be performed to return control. RETURN This statement is used to "return" control back to the STATEMENT following the most recently executed GOSUB STATEMENT. The GOSUB-RETURN sequence can be "nested" meaning a subroutine called by the GOSUB STATEMENT can call another subroutine with another GOSUB STATEMENT EXAMPLES: SIMPLE SUBROUTINE >10 FOR I = 1 TO 5 >20 GOSUB 100 >30 NEXT I >100 PRINT I >110 RETURN >RUN 1 2 3 4 5 READY > NESTED SUBROUTINES >10 FOR 1=1 TO 3 >20 GOSUB 100 >30 NEXT I >40 END >100 PRINT I, >110 GOSUB 200 >120 RETURN >200 PRINT I*I >210 RETURN >RUN 1 1 2 4 3 9 READY > 4.12 DESCRIPTION OF STATEMENTS NOTE - The Control Stack on Version 1.1 permits a graceful exit from incompleted control loops, given the following example: EXAMPLE: . . . 50 GOSUB 1000 . . 1000 FOR I = 1 TO 10 1010 IF X = I THEN 1040 1020 PRINT I*X 1030 NEXT I 1040 RETURN Version 1.1 would permit the programmer to exit the subroutine even though the FOR-NEXT loop might not be allowed to complete if X did equal I. Version 1.0 of MCS BASIC-52 would yield a C-STACK error if the FOR-NEXT loop was not allowed to complete before the RETURN statement was executed. MCS-BASIC-52 4.13 DESCRIPTION OF STATEMENTS STATEMENT: GOTO [ln num] MODE: COMMAND AND/OR RUN TYPE: CONTROL The GOTO [ln num] STATEMENT will cause BASIC to transfer control directly to the line number ([In num]) following the GOTO STATEMENT EXAMPLE: 50 GOTO 100 Will, if line 100 exists, cause execution of the program to resume at line 100. If line number 100 does not exist the message ERROR: INVALID LINE NUMBER will be printed to the console device. Unlike the RUN COMMAND the GOTO STATEMENT, if executed in the COMMAND MODE, does not CLEAR the variable storage space or interrupts. However, if the GOTO STATEMENT is executed in the COMMAND MODE after a line has been edited, MCS BASIC-52 will CLEAR the variable storage space and all BASIC evoked interrupts. This is a necessity because the variable storage and the BASIC program reside in the same RAM memory. So editing a program can destroy variables. NOTE - (Version 1.0 only) Because of the way MCS BASIC-52's text interpreter processes a line, when an INVALID LINE NUMBER ERROR occurs on the GOTO, GOSUB, ON GOTO, and ON GOSUB STATEMENTS the line AFTER the GOTO or GOSUB STATEMENT will be printed out in the error message. This may be confusing, but it was a trade-off between execution speed, code size, and error handling. Error handling lost. EXAMPLE: >10 GOTO 100 >20 PRINT X RUN ERROR: INVALID LINE NUMBER - IN LINE 20 20 PRINT X Version 1.1 does not exhibit this particular anomaly. 4.14 DESCRIPTION OF STATEMENTS STATEMENTS: ON [expr] GOTO[ln num],[In num], . . [ln num] ON [expr] GOSUB[ln num], [ln num], . . [In num] MODE: RUN TYPE: CONTROL The value of the expression following the ON statement is the number in the line list that control will be transferred to. EXAMPLE: 10 ON Q G0T0 100,200,300 If Q was equal to 0, control would be transferred to line number 100. If Q was equal to 1, control would be transferred to line number 200. If Q was equal to 2, GOTO line 300, etc. All comments that apply to GOTO and GOSUB apply to the ON STATEMENT. If Q is less than ZERO a BAD ARGUMENT ERROR will be generated. If Q is greater than the line number list following the GOTO or GOSUB STATEMENT, a BAD SYNTAX ERROR will be generated. The ON STATEMENT provides "conditional branching" options within the constructs of an MCS BASIC-52 program. 4.15 DESCRIPTION OF STATEMENTS STATEMENTS: IF - THEN - ELSE MODE: RUN TYPE: CONTROL The IF statement sets up a conditional test. The generalized form of the IF THEN - ELSE statement is as follows: [ln num] IF [rel expr] THEN valid STATEMENT ELSE valid STATEMENT A specific example is as follows: >10 IF A=100 THEN A=0 ELSE A=A+1 Upon equal using same execution of line 10 lf A is equal to 100, THEN A would be assigned a value of 0. If A does not equal 100, A would be assigned a value of A + 1. If it is desired to transfer control to different line numbers using the IF statement, the GOTO statement may be omitted. The following examples would yield the same results: >20 IF INT(A)< 10 THEN GOTO 100 ELSE GOTO 200 >20 IF INT(A)< 10 THEN 100 ELSE 200 Additionally, the THEN statement can be replaced by any valid MCS BASIC-52 statement, as shown below: >30 IF A<>l0 THEN PRINT A ELSE 10 >30 IF A<>10 PRINT A ELSE 10 The ELSE statement may be omitted. If it is, control will pass to the next statement. EXAMPLE: >20 IF A=10 THEN 40 >30 PRINT A In this example, IF A equals 10 then control would be passed to line number 40. If A does not equal 10 line number 30 would be executed. 4.15 DESCRIPTION OF STATEMENTS COMMENTS ON IF-THEN-ELSE- Version 1,1 is not compatible with VI .0 when the IF-THEN-ELSE STATEMENT is used with multiple statements per line. In Vl .0, the following two examples would function in the same manner. EXAMPLE 1: 10 IF A=B THEN C=A : A=A/2 : GOTO 100 20 PRINT A EXAMPLE 2: 10 IF A=B THEN C=A 12 A=A/2 14 GOTO 100 20 PRINT A They function in the same manner because VI .0 treats the delimiter (;) exactly the same as a carriage return (cr) in every case. However, VI. 1 executes the remainder of the line if and only if the test A = B proves to be true. This means in EXAMPLE 1 IF A did equal B, Vi.1 would then set C=A, then set A = A/2, then execute line 100. IF A did not equal B, Vi. I would then PRINT A and ignore the statements C=A : A=A/2 GOTO 100. VI.l will execute EXAMPLE 2 exactly the same way as Vl.0. This same logical interpretation holds true for the ELSE statement as well. This example dictates a simple rule for maintaining IF-THEN-ELSE compatibility between the two versions. IF THE DELIMITER (:) IS NOT USED IN AN IF-THEN-ELSE STATEMENT, V 1.0 AND V 1.1 WILL TREAT THE STATEMENTS IN THE SAME MANNER!! This change was made because most users of MCS BASIC-52 felt that the Vl .1 interpretation of this statement was more useful because fewer GOTO statements need be employed in a typical program. 4.16 DESCRIPTION OF STATEMENTS STATEMENTS: INPUT MODE: RUN TYPE: INPUT/OUTPUT The INPUT statement allows users to enter data from the console during program execution. One or more variables may be assigned data with a single input statement. The variables must be separated by a comma. EXAMPLE: INPUT A,B Would cause the printing of a question mark (?) on the console device as a prompt to the operator to input two numbers separated by a comma. If the operator does not enter enough data, then MCS BASIC.52 responds by outputting the message TRY AGAIN to the console device. EXAMPLE: >10 INPUT A,B >20 PRINT A,B >RUN ?1 TRY AGAIN ?1,2 1 2 READY The INPUT statement may be written so that a descriptive prompt is printed to tell the user what to type. The message to be printed is placed in quotes after the INPUT statement. If a comma appears before the first variable on the input list, the question mark prompt character will not be displayed. EXAMPLES: >10 INPUT"ENTER A NUMBER"A >10 INPUT"ENTER A NUMBER-",A >20 PRINT SQR(A) >20 PRINT SQR(A) >RUN >RUN ENTER A NUMBER ENTER A NUMBER-100 ?100 10 10 4.16 DESCRIPTION OF STATEMENTS Strings can also be assigned with an INPUT statement. Strings are always terminated with a carriage return (cr). So, if more than one string input is requested with a single INPUT statement, MCS BASIC~52 will prompt the user with a question mark. EXAMPLES: >10 STRING 110.10 >20 INPUT "NAME: ",$(1) >30 PRINT "HI ",$(1) >RUN NAME. SUSAN HI SUSAN READY Additionally, strings and variables can be assigned with a single INPUT statement. EXAMPLE: >10 STRING 100,10 >20 INPUT"NAME(cr), AGE - ",$(1),A >30 PRINT "HELLO ",$(1),", YOU ARE ",A,"YEARS OLD" >RUN NAME (cr). AGE - FRED HELLO FRED, YOU ARE 15 YEARS OLD READY > 4.17 DESCRIPTION OF STATEMENTS STATEMENT: LET MODE: COMMAND AND/OR RUN TYPE: ASSIGNMENT The LET statement is used to assign a variable to the value of an expression. The generalized form of LET is: LET [var] = [expr] EXAMPLES: LET A = 10*SIN(B)/100 or LET A = A + 1 Note that the = sign used in the LET statement is not equality operator, but rather a "replacement" the statement should be read A is replaced by A plus one. THE WORD LET IS ALWAYS OPTIONAL,i,e. LET A = 2 is the same as A = 2 When LET is omitted the LET statement is called an IMPLIED LET. This document will use the word LET to refer to both the LET statement and the IMPLIED LET statement. The LET statement is also used to assign the string variables, i.e: LET 5(I)-THIS IS A STRING" or LET 5(2)=5(1) Before Strings can be assigned the STRlNG [expr], [expr] STATEMENT MUST be executed, or else a MEMORY ALLOCATION ERROR will occur. SPECIAL FUNCTION VALUES can also be assigned by the LET statement, i.e.: LET IE = 82H or LET XBYTE(2000H)=5AH or LET DBYTE(25)=XBYTE(1000) 4.18 DESCRIPTION OF STATEMENTS STATEMENT: ONERR[ln num] MODE: RUN TYPE: CONTROL The ONERR[ln num] statement lets the programmer handle arithmetic errors, should they occur, during program execution. Only ARITH. OVERFLOW, ARITH. UNDERFLOW, DiVIDE BY ZERO, and BAD ARGUMENT errors can be "trapped" by the ONERR statement, all other errors are not. if an arithmetic error occurs after the ONERR statement is executed, the MCS BASiC-52 interpreter will pass control to the line number following the ONERR[ln num] statement. The programmer can handle the error condition in any manner suitable to the particular application. Typically, the ONERR[ln num]statement should be viewed as an easy way to handle errors that occur when the user provides inappropriate data to an INPUT statement. With the ONERR[ln num] statement, the programmer has the option of determining what type of error occurred. This is done by examining external memory location 257 (lOlH) after the error condition is trapped. The error codes are as follows: ERROR CODE = 10 - DIVIDE BY ZERO ERROR CODE = 20 - ARITH. OVERFLOW ERROR CODE = 30 - ARITH. UNDERFLOW ERROR CODE = 40 - BAD ARGUMENT This location may be examined by using an XBY(257) statement. 4.20 DESCRIPTION OF STATEMENTS STATEMENT: ONTIME [expr], [ln num] MODE: RUN TYPE: CONTROL Since MCS BASIC-52 processes a line in the millisecond time frame and the timer/counters on the 8O52AH operate in the micro-second time frame, there is an inherent incompatibility between the timer/counters on the 8O52AH and MCS BASlC-52. To help solve this situation the ONTIME [expr],[ln num] statement was devised. What ONTIME does is generate an interrupt everytime the SPECIAL FUNCTION OPERATOR,TIME, is equal to or greater than the expression following the ONTIME statement. Actually, only the integer portion of TIME is compared to the integer portion of the expression. The interrupt forces a GOSUB to the line number ([In num]) following the expression ([expr]) in the ONTIME statement. Since the ONTIME statement uses the SPECIAL FUNCTION OPERATOR, TIME, the CLOCK I statement must be executed in order for ONTIME to operate. If CLOCK 1 is not executed the SPECIAL FUNCT1ON OPERATOR, TIME, will never increment and not much will happen. Since the ONTIME statement generates an interrupt when TIME is greater than or equal to the expression following the ONTIME statement, how can periodic interrupts be generated? That's easy, the ONTIME statement must be executed again in the interrupt routine: EXAMPLE: >l0 TIME=0 : CLOCK1 : ONTIME 2,100 DO >20 WHILE TIME<10 : END >100 PRINT "TIMER INTERRUPT AT -",TIME,"SECONDS" >110 ONTIME TIME+2, 100 : RETI >RUN TIMER INTERRUPT AT - 2. 045 SECONDS TIMER INTERRUPT AT - 4. 045 SECONDS TIMER INTERRUPT AT - 6. 045 SECONDS TIMER INTERRUPT AT - 8. 045 SECONDS TIMER INTERRUPT AT - 10. 045 SECONDS READY You may wonder why the TIME that was printed out was 45 milliseconds greater than the time that the interrupt was supposed to be generated. That's because the terminal used in this example was running at 4800 BAUD and it takes about 45 milliseconds to print the message TIMER INTERRUPT AT." If the programmer does not want this delay, a variable should be assigned to the SPECIAL FUNCTION OPERATOR, TIME, at the beginning of the interrupt routine. EXAMPLE: >10 TIME-0 : CLOCKl : ONTIME 2 100: DO >20 WHILE TIME<10 : END >100 A=TIME >110 PRINT "TIMER INTERRUPT AT -",A,"SECONDS" >120 ONTIME A+2,100 : RETI >RUN TIMER INTERRUPT AT - 2 SECONDS TIMER INTERRUPT AT - 4 SECONDS TIMER INTERRUPT AT - 6 SECONDS TIMER INTERRUPT AT - 8 SECONDS TIMER INTERRUPT AT - 10 SECONDS READY Like the ONEX 1 statement, the ONTIME interrupt routine must be exited with a RETI statement. Failure to do this will "lock-out" all future interrrupts. The ONTIME interrupt has priority over the ONEX1 interrupt. This means that the ONTIME interrupt can interrupt the ONEX1 interrupt routine. This priority was established because time related functions in control applications were viewed as critical routines. If the user does not want the ONEX1 routine to be interrupted by the ONTIME interrupt, a CLOCKO or a CLEARI statement should be executed at the beginning of the ONEXl routine. The interrupts would have to be re-enabled before the end of the ONEX1 routine. The ONEX1 interrupt cannot interrupt an ONTIME routine. The ONTIME statement in MCS BASIC-52 is unique, relative to most BASICS. This powerful statement eliminates the need for the user to "test" the value of the TIME operator periodically throughout the BASIC program. 4.21 DESCRIPTION OF STATEMENTS STATEMENT: PRINT or P. (?VERSION 1.1 ONLY) MODE: COMMAND and/or RUN TYPE: INPUT/OUTPUT The PRINT statement directs MCS BASIC-52 to output to the console device. The value of expressions, strings, literal values, variables or test strings may be printed out. The various forms may be combined in the print list by separating them with commas. If the list is terminated with a comma, the cariage return/ line feed will be suppressed. P. is a "shorthand" notation for PRINT. In Version 1.1 ? is also "shorthand" notation for PRINT EXAMPLES: >PRINT 10*10,3*3 >PRINT "MCS-51" >PRINT 5,1E3 100 9 MCS-51 5 1000 Values are printed next to one another with two intervening blanks. A PRINT statement with no arguments causes a carriage return/line feed sequence to be sent to the console device. SPECIAL PRINT FORMATTING STATEMENTS TAB([expr]) The TAB([expr]) function is used in the PRINT statement to cause data to be printed out in exact locations on the output device. TAB([expr]) tells MCS BASIC-52 which position to begin printing the next value in the print list. If the printhead or cursor is on or beyond the specified TAB position, MCS BASIC-52 will ignore the TAB function. EXAMPLE: >PRINT TAB(5),"x",TAB(10),"Y" x Y SPC([expr]) The SPC([expr]) function is used in the PRINT statement to cause MCS BASIC-52 to output the number of spaces in the SPC argument. EXAMPLE: >PRINT A,SPC(5),B may be used to place an additional 5 spaces between the A and B over and above the two that would normally be printed. 4.21 DESCRIPTION OF STATEMENTS CR The CR function is interesting and unique to MCS BASIC-52. When CR is used in a PRINT statement it will force a carriage return, but no line feed. This can be used to create one line on a CRT device that is repeatedly updated. EXAMPLE: )1O FOR I=I TO 1000 )20 PRINT I,CR, )3O NEXT I will cause the output to remain only on one line. No line feed will ever he sent to the console device. USING(special characters) The USING function is used to tell MCS BAS1C-52 what format to display the values that are printed. MCS BASIC.52 "stores" the desired format after the USING statement is executed. So, all outputs following a USING statement will he in the format evoked by the last USING statement executed. The USING statement need not be executed within every PRINT statement unless the programmer wants to change the format. U. is a "shorthand" notation for USiNG. The options for USING are as follows: USING(Fx) - This will force MCS BASIC-52 to output all numbers using the floating point format. The value of x deterrnines how many significant digits will he printed. If x equals 0, MCS BASIC-52 will not output any trailing zeros, so the number of digits will vary depending upon the number. MCS BASlC-52 will always output at least 3 significant digits even if x is 1 or 2. The maximum value for x is 8. EXAMPLE: 10 PRINT USING(F3),1,2,3 20 PRINT USING(F4),1,2,3 30 PRINT USING(F5),1,2,3 40 FOR 1=10 TO 40 STEP 10 50 PRINT I 60 NEXT I > RUN 1.00 E 0 2.00 E 0 3.00 E 0 1.000 E 0 2.000 E 0 3.000 E 0 1.0000 E 0 2.0000 E 0 3.0000 E 0 1.0000 E+1 2.0000 E+1 3.0000 E+1 4.0000 E+1 READY 4.21 DESCRIPTION OF STATEMENTS USING(#.#) - This will force MCS BASlC-52 to output all numbers using an integer and/or fraction format. The number of "#"'s before the decimal point represents the number of significant integer digits that will be printed in the fraction. The decimal point may he omitted, in which case only integers will be printed. USING may be abbreviated U. USING (###.###), USING(######) and USING(######.##) are all valid in MCS BASIC-52. The maximum number of "#" characters is 8. If MCS BASIC-52 cannot output the value in the desired format (usually because the value is too large) a question mark (?) will be printed to console device, then BASIC will output the number in the FREE FORMAT described below. EXAMPLE: >10 PRINT USING(##.##),1,2,3 >20 FOR I=1 TO 120 STEP 20 >30 PRINT I >40 NEXT I >RUN 1.00 2.00 3.00 1.00 21.00 41.00 61.00 81. 00 ? 101 READY NOTE: The USING(Fx) and the USING(#.#) formats will always "align" the decimal points when printing a numher. This feature makes displayed columns of numbers easy to read. USING(O) - This argument lets MCS BASIC-52 determine what format to use. The rules are simple, if the numher is between +/-99999999 and +/-.1, BASIC will display integers and fractions. If it is out of this range, BASIC will use the USING(FO)format. Leading and trailing zeros will always he suppressed. After reset, MCS BASIC-52 is placed in the USING(O) format. 4.23 DESCRIPTION OF STATEMENTS STATEMENTS: PHO., PH1., PHO.#, PH1.# MODE: COMMAND and/or RUN TYPE: INPUT/OUTPUT The PHO. and PH1. statements do the same thing as the PRINT statement except that the values are printed out in a hexadecimal format. The PHO. statement suppresses two leading zeros if the number to he printed is less than 255 (OFFH). The PHI. statement always prints out four hexadecimal digits. The character "H" is always printed after the number when PHO. or PHI. is used to direct an output. The values printed are always truncated integers. If the number to be printed is not within the range of valid integer (i.e. between 0 and 65535 (OFFFFH) inclusive), MCS BASlC-52 will default to the normal mode of print. If this happens no 'H'' will be printed out after the value. Since integers can be entered in either decimal or hexadecimal form the statements PRINT, PHO., and PH1. can he used to perform decimal to hexadecimal and hexadecimal to decimal conversion. All comments that apply to the PRINT statement apply to the PHO. and PHl. statements. PHO.# and PHl.# do the same thing as PHO. and PH1. respectively, except that the output is directed to the list device instead of the console device. EXAMPLES: >PH0. 2*2 >PHl. 2*2 >PRINT 99H >PH0. 100 04H 0004H 153 64H >PH0. 1000 >PHl. 1000 >P. 3E8H >PHO. PI 3E8H 03E8H 1000 03H 4.25 DESCRIPTION OF STATEMENTS STATEMENT: PUSH[expr] MODE: COMMAND AND / OR RUN TYPE: ASSIGNMENT The arithmetic expression, or expressions following the PUSH statement are evaluated and then sequentially placed on MCS BASIC-52's ARGUMENT STACK. This statement, in conjunction with the POP statement provide a simple means of passing parameters to assembly language routines. In addition, the PUSH and POP statements can be used to pass parameters to BASIC subroutines and to "SWAP" variables. The last value PUSHED onto the ARGUMENT STACK will he the first value POPPED off the ARGUMENT STACK. VARIATIONS: More than one expression can be pushed onto the ARGUMENT stack with a single PUSH statement. The expressions are simply followed by a comma: PUSH[expr],[expr],......[expr]. The last value PUSHED onto the ARGUMENT STACK will be the last expression [expr] encountered in the PUSH STATEMENT. EXAMPLES: SWAPPING VARIABLES >10 A=I0 >20 [3-20 >30 PRINT A,B >40 PUSH A,B >50 POP A,B >60 PRINT A,B >RUN 10 20 20 10 READY > SUBROUTINE PASSING >10 PUSH 1,2,3 >20 GOSUB 100 >30 POP Rl,R2 >40 PRINT R1,R2 >50 END >100 REM GUADRATIC A=2,B=3,C=1 IN EXAMPLE >110 POP A,B,C >120 PUSH (-B+SQR(B*B-4*A*C))/(2*A) >130 PUSH (-B-SQR(B*B-4*A*C))/(2*A) >140 RETURN RUN -1 -.5 READY > 4.26 DESCRIPTION OF STATEMENTS STATEMENT: POP[var] MODE: COMMAND AND / OR RUN TYPE: ASSIGNMENT The top of the ARGUMENT STACK is assigned to the variable following the POP statement and the ARGUMENT STACK is "POPPED" (i.e. incremented by 6). Values can be placed on the stack by either the PUSH statement or by assembly language CALLS. NOTE - If a POP statement is executed and no number is on the ARGUMENT STACK, an A-STACK ERROR will occur. VARIATIONS: More than one variable can be popped off the ARGUMENT stack with a single POP statement. The variables are simply followed by a comma (i.e. POP [var],[var],...........[var]. EXAMPLES: See PUSH statement. COMMENT: The PUSH and POP statements are unique to MCS BASlC-52. These powerful statements can be used to "get around" the GLOBAL variable problems so often encountered in BASIC PROGRAMS. This problem arises because in BASIC the "main" program and all subroutines used by the main program are required to use the same variable names (i.e. GLOBAL VARIABLES). It is not always convenient to use the same variables in a subroutine as in the main program and you often see programs re-assign a number of variables (i.e. A=Q) before a GOSUB STATEMENT is executed. 1f the user reserves some variable names JUST for subroutines (i.e. S1, S2) and passes variables on the stack as shown in the previous example, you will avoid any GLOBAL variable problems in MCS BASlC-52. 4.28 DESCRIPTION OF STATEMENTS STATEMENT: REM MODE: RUN (Version 1.0) COMMAND AND/OR RUN (Version 1.1) TYPE: CONTROL - PERFORMS NO OPERATION REM is short for REMark. It does nothing, but allows the user to add comments to a program. Comments are usually needed to make a program a little easier to understand. Once a REM statement appears on a line the entire line is assumed to be a remark, so a REM statement may not be terminated by a colon (:), however, it may be placed after a colon. This can be used to allow the programmer to Place a comment on each line. EXAMPLES: >10 REM INPUT ONE VARIABLE >20 INPUT A >30 REM INPUT ANOTHER VARIABLE >40 INPUT B >50 REM MULTIPLY THE TWO >60 Z=A*B >70 REM PRINT THE ANSWER >80 PRINT Z >10 INPUT A : REM INPUT ONE VARIABLER >20 INPUT B : REM INPUT ANOTHER VARIABLE >30 Z=A*B : REM MULTIPLY THE TWO >40 PRINT Z : REM PRINT THE ANSWER The following will NOT work because the entire line would be interpreted as a REMark, so the PRINT statement would not be executed: >10 REM PRINT THE NUMBER : PRINT A NOTE - The reason the REM statement was made executable in the command mode in Version 1.1 of MCS BASlC-52 is that if the user is employing some type of UPLOAD/DOWNLOAD routine with a computer, this lets the user insert REM statements, without line numbers in the text and not download them to the MCS BASIC-52 device. This helps to conserve memory. 4.29 DESCRIPTION OF STATEMENTS STATEMENT: RETI MODE: RUN TYPE: CONTROL The RETI statement is used to exit from interruPts that are handled by an MCS BASIC-52 program. Specifically, the ONTIME and the ONEX1 interrupts. The RETI statement does the same thing as the RETURN statement except that it also clears a software interrupt flags so interrupts can again be acknowledged. If the user fails to execute the RETI statement in the interrupt procedure, all future interrupts will be ignored. 4.30 DESCRIPTION OF STATEMENTS STATEMENT: STOP MODE: RUN TYPE: CONTROL The STOP statement allows the programmer to break program execution at specific points in a program. After a program is STOPped variables can be displayed and/or modified. Program execution may be resumed with a CONTinue command. The purpose of the STOP statement is to allow for easy program "debugging." More details of the STOP-CONT sequence are covered in the DESCRIPTION OF COMMAND - CONT section of this manual. EXAMPLE: >10 FOR I-1 TO 100 >20 PRINT I >30 STOP >40 NEXT I >RUN 1 STOP - IN LINE 40 READY >CONT 2 Note that the line number printed out after the STOP statement is executed is the line number following the STOPstatement, NOT the line number that contains the STOP statement!!! 4.31 DESCRIPTION OF STATEMENTS STATEMENT: STRING [expr], [expr] MODE: COMMAND and/or RUN TYPE: CONTROL The STRING [expr],[expr] statement allocates memory for strings. Initially, no memory is allocated for strings. If the user attempts to define a string with a statement such as LET $(l)= "HELLO" before memory has been allocated for strings, a MEMORY ALLOCATION ERROR will be generated. The first expression in the STRING [expr],[expr] statement is the total number of bytes the user wishes to allocate for string storage. The second expression denotes the maximum number of bytes that are in each string. These two numbers determine the total number of defined string variables. You might think that the total number of defined strings would be equal to the first expression in the STRING [expr],[expr] statement divided by the second expression. Ha,ha, do not be so presumptuous. MCS BASIC-52 requires one additional byte for each string, plus one additional byte overall. This means that the statement STRING 100,10 would allocate enough memory for 9 string variables, ranging from $(O) to $(8) and all of the 100 allocated bytes would be used. Note that $(0) is a valid string in MCS BASIC-52. After memory is allocated for string storage, neither commands, such as NEW nor statements, such as CLEAR, will"de-allocate" this memory. The only way memory can be de-allocated is to execute a STRING 0,0 statement. STRING 0,0 will allocate no memory to string variables. IMPORTANT NOTE Every time the STRING [expr],[expr] statement is executed, MCS BASIC-52 executes the equivalent of a CLEAR statement. This is a necessity because string variables and numeric variables occupy the same external memory space. Sc>, after the STRING statement is executed, all variables are "wiped-out". Because of this, string memory allocation should be performed early in a program (like the first statement or so) and string memory should never be "re-allocated" unless the programmer is willing to destroy all defined variables. 4.34 DESCRIPTION OF STATEMENTS STATEMENT: IDLE (VERSION 1.1 ONLY) MODE: RUN TYPE: CONTROL The IDLE statement forces the MCS BASIC-52 device into a "wait until interrupt mode." Execution of statements is halted until either an ONTIME [expr], [In num] or an ONEXI [In num] interrupt is received. The user must make sure that one or both of these interrupts have been enabled before executing the IDLE instruction or else the MCS BASIC-52 device will enter a "wait forever mode" and for all practical purposes the system will have crashed. When an ONTIME [expr], [ln num] or an ONEXl [ln num] is received while in the IDLE mode, the MCS BASIC-52 device will execute the interrupt routine, then execute the statement following the IDLE instruction. Hence, the execution of the IDLE instruction is terminated when an interrupt is received. While in the IDLE mode, the MCS BASlC-52 device asserts the /DMA ACKNOWLEDGE pin (PORT 1, BIT 6 = 0) to indicate that the IDLE instruction is active and that no external bus activity will occur. This PIN is physically pin 7 on the MCS BASIC-52 device. When the MCS BASIC-52 device exits from the IDLE mode, this pin is placed back into the logically 1 (non-active) state. The user may also exit from the IDLE mode with an assembly language interrupt routine. This is accomplished by setting BIT 33 (21H) (which is in Bit addressable RAM location 36.1) when returning from the assembly language interrupt routine. If this bit is not set by the user, the MCS BASlC-52 device will remain in the IDLE mode when the user assembly language routine returns to BASIC. An attempt to execute the 1DLE statement in the direct mode will yield a BAD SYNTAX ERROR. 4.36 DESCRIPTION OF STATEMENTS STATEMENTS: LD@ [expr] and ST@ [expr] (VERSION 1.1 ONLY) MODE: COMMAND AND/OR RUN TYPE: INPUT/OUTPUT ST@ The ST@ [expr] statement lets the user specify where MCS BASIC-52 floating point numbers are to be stored. The expression [expr] following the ST@ statement specifies the address of where the number is to be stored and the number is assumed to be on the argument stack. The ST@ [expr] statement is designed to be used in conjunction with the LD@ [expr] statement. The purpose of these two statements is to allow the user to save floating point numbers anywhere in memory with the assumption that the user will employ some type of batter back-up or non-volatile scheme with this memory. LD@ The LD@ [expr] statement lets the user retrieve floating point numbers that were saved with the ST@ [expr] statement. The expression [expr] following the LD@ statement specifies where the number is stored and after executing the LD@ [expr] statement, the number is placed on the argument stack. EXAMPLE: Saving and retrieving a ten element array at location array at location 0F000H. 10 REM *** ARRAY SAVE *** 20 FOR I = 0 to 9 30 PUSH A(I) : REM PUT ARRAY VALUE ON STACK 40 ST@ OF005H+6*I : REM STORE IT, SIX BYTES PER NUMBER 50 NEXT I 60 REM *** GET ARRAY *** 70 FOR I = 0 to 9 80 LD@ OFOOO5H+6*I 90 POP B(I) 100 NEXT I Remember that each floating point number requires 6 bytes of storage. Also note that expression in the ST@ [expr] and LD@ [expr] statements point to the most significant byte of the stored number. Hence, ST@ (0F005H) would save the number in locations 0F005H, 0F003H, 0F002H, 0F001H, and 0F000H. Description of Arithmetic/Logic Operators and Expressions 5.1 DUAL OPERAND OPERATORS MCS BASIC-52 contains a complete set of arithmetical and logical operators. Operators are divided into two groups, dual operand or dyadic operators and single operand or unary operators. The generalized form of all dual operand instructions is as follows: [expr] OP [expr], where OP is one of the following operators: + ADDITIONAL OPERATOR Example: print 3+2 5 / DIVISION OPERATOR Example: PRINT 100/5 20 **EXPONENTIATION OPERATOR Raises the first expression to the power of the second expression. The power any number can be raised to is limited to 255. The notation ** was chosen instead of the sometimes used "UP ARROW" symbol because the "up arrow" symbol appears different on various terminals. To eliminate confusion ** notation was chosen. Example: Print 2**3 B *MULTIPLICATION OPERATOR Example: Print 3*3 9 - SUBTRACTION OPERATOR Example: Print 9-6 3 5.1 DUAL OPERAND OPERATIONS .AND. LOGICAL AND OPERATOR Example: Print 3. and. 2 2 .OR. LOGICAL OR OPERATOR Example: Print 1. or. 4 5 .XOR. LOGICAL EXCLUSIVE OR OPERATOR Example: Print 7. xor. 6 1 COMMENTS ON LOGICAL OPERATIONS .AND., .OR., and .XOR. These operators perform a BIT-WISE logical function on valid INTEGERS. That means both arguments for these operators must be between 0 and 65535 (OFFFFH) inclusive. If they are not, MCS BASIC-52 will generate a BAD ARGUMENT ERROR. All non-integer values are truncated. NOT rounded. You may wonder why the notation .OP. was chosen for the logical functions. The only reason for this is that MCS BASIC-52 eliminates ALL spaces when it processes a user line and inserts spaces before and after STATEMENTS when it LISTS a user program. MCS BASIC-52 does not insert spaces before and after operators. So, if the user types in a line such as 10 A = 10 * 10, this line will be listed as 10 A=10*10. All spaces entered by the user before and after the operator will be eliminated. The .OP. notation was chosen for the logical operators because a line entered as 10 B = A AND B would be listed as 10 B=AANDB. This just looked confusing, so the dots were added to the logical instructions and the previous example would be listed as 10 B=A.AND.B, which is easier to read. 5.2.1 UNARY OPERATORS - GENERAL PURPOSE ABS([expr]) Returns the ABSOLUTE VALUE of the expression. Examples: PRINT ABS (5) PRINT ABS (-5) 5 5 NOT ([expr]) Returns a 16 bit one's complement of the expression. The expression must be a valid integer (i.e. between 0 and 65535 (OFFFFH) inclusive). Non-integers will be truncated, not rounded. Examples: PRINT NOT (65000) PRINT NOT (0) 535 65535 INT ([expr]) Returns the integer portion of the expression. Examples: PRINT INT (3.7) PRINT INT (100.876) 3 100 SGN ([expr]) Will return a value of +1 if the argument is greater than zero, zero if the argument is equal to zero, and -1 if the argument is less than zero. Examples: PRINT SGN (52) PRINT SGN (0) PRINT SGN (-8) 1 0 -1 5.2.1 UNARY OPERATORS - GENERAL PURPOSE SQR ([expr]) Returns the square root of the argument. The argument may not be less than zero. The result returned will be accurate to within +/- a value of 5 on the least significant digit. Examples: PRINT SGR (9) PRINT SQR (45) PRINT SQR (100) 3 6.7082035 10 RND Returns a pseudo-random number in the range between 0 and 1 inclusive. The RND operator uses a 16-bit binary seed and generates 65546 pseudo-random numbers before repeating the sequence. The numbers generated are specifically between 0/65535 and 65535/65535 inclusive. Unlike most BASICS, the RND operator in MCSA BASIIC-52 does not require an argument or a dummy argument. In fact, if an argument is placed after the RND operator, a BAD SYNTAX error will occur. Examples: PRINT RND 30278477 PI PI is not really an operator, it is a stored constant. In MCS BASIC-52, PI is stored as 3.14115926. Math experts will notice that PI is actually closer to 3.141592653, so proper rounding for PI should yield the number 3.14115927. The reason MCS BASIC-52 uses a 6 instead of a 7 for the last digit is that errors in the SIN, COS and TAN operators were found to be greater when the 7 was used instead of 6. This is because the number PI/2 is needed for these calculations and it is desireable, for the sake of accuracy to have the equation PI/2 + PI/2 = PI hold true. This cannot be done if the last digit in PI is an odd number, so the last digit of PI was rounded to 6 instead of 7 to make these calculations more accurate. 5.2.2 UNARY OPERATIONS - LOG FUNCTIONS LOG ([expr]) Returns the natural logarithm of the argument. The argument must be greater than 0. This calculation is carried out to 7 significant digits. Examples: PRINT LOG (12) PRINT LOG (EXP (1)) 2.484906 1 EXP ([expr]) This function raises the number "e" (2.7182818) to the power of the argument. Examples: PRINT EXP (1) PRINT EXP (LOG (2)) 2.7182818 2 5.2.3 UNARY OPERATORS - TRIG FUNCTIONS SIN ([expr]) Returns the SIN of the argument. The argument is expressed in radians. Calculations are carried out to 7 significant digits. The argument must be between +/-200000. Examples: PRINT SIN (PI/4) PRINT SIN (0) 7071067 0 COS ([expr]) Returns the COS of the argument. The argument is expressed in radians. Calculations are carried out to 7 significant digits. The argument must be between 200000. Examples: PRINT COX (PI/4) PRINT COS (0) .707167 1 5.2.3. UNARY OPERATORS - TRIG FUNCTIONS TAN ([expr]) Returns the TAN of the arguent. The argument is expressed in radians. The argument must be between 200000. Examples: PRINT TAN (PI/4) PRINT TAN (0) 1 0 ATN ([expr]) Returns the ARCTANGENT of the argument. The result is in radians. Calculations are carried out to 7 significant digits. The ATN operator returns a result between -PI/2 (3.1415926/2) and PI/2. Examples: PRINT ATN (PI) PRINT ATN (1) 1.2626272 .78539804 COMMENTS ON TRIG FUNCTIONS The SIN, COS, and TAN operators use a Taylor series to calculate the function. These operators first reduce the argument to a value that is between 0 and PI/2. This reduction is accomplished by the following equation: REDUCED ARGUMENT = (user arg/PI - INT (user arg/PI) * PI The REDUCED ARGUMENT. From the above equation, will be between 0 and PI. The REDUCED ARGUMENT is then tested to see if it is greater than PI/2. If it is, then it is subtracted from PI to yield the final value. If it isn't, then the REDUCED ARGUMENT is the final value. Although this method of angle reduction provides a simple and economical means of generating the appropriate arguments for a Taylor series, there is an accuracy problem associated with this technique. The accuracy problem is noticed when the user argument is large (i.e. greater than 1000). That is because significant digits, in the decimal (fraction) portion of REDUCED ARGUMENT are lost in the (user arg/PI - INT (user arg/PI) expression. As a general rule, try to keep the arguments for the TRIG function as small as possible! 5.3 UNDERSTANDING PRECEDENCE OF OPERATORS The hierarchy of mathematics dictates that some operations are carried out before others. If you understand the hierarchy of mathematics, it is possible to write complex expressions using only a minimum amount of parentheses. It's easy to illustrate what precedence is all about, examine the following equation: 4+3*2 = ? Should you add (4 + 3) then multiply seven by 2, or should you multiply (3*2) then add 4? Well, the hierarchy of mathematics says that multiplication has precedence over addition, so you would multiply (3*2) first then add 4. So, 4 + 3*2 = 10 The rules for the hierarchy of math are simple. When an expression is scanned from left to right an operation is not performed until an operator of lower or equal precedence is encountered. In the example addition could not be performed because multiplication has higher precedence. The precedence of operators from highest to lowest in MCS BASIC-52 is as follows: 1) OPERATORS THAT USE PARENTHESES ( ) 2) EXPONENTATION (**) 3) NEGATION (-) 4) MULTIPLICATION (*) AND DIVISION (/) 5) ADDITION (+) AND SUBTRACTION (-) 6) RELATIONAL EXPRESSIONS (=, <>,>,>=,<,<=) 7) LOGICAL AND (.AND.) 8) LOGICAL OR (.OR.) 9) LOGICAL XOR (.XOR.) Relative to operator precedence, the rule of thumb should always be; when in doubt, use parentheses. 5.4 HOW RELATIONAL EXPRESSIONS WORK Relational expressions involve the operators =, <>, >,>=, and <=. These operators are typically used to "test" a condition. In MCS BASIC-52 relational operators return a result of 65535 (OFFFFH) if the relational expression is true, and a result of 0, if the relation expression is false. But, where is the result returned? It is returned to the argument stack. Because of this, it's possible to actually display the result of a relational expression. Examples: PRINT 1=0 PRINT>0 PRINT A<>A PRINT A=A 0 65535 0 65535 It may seem strange to have a relational expression actually return a result, but it offers a unique benefit in that relational expressions can actually be "chained" together using the logical operators .AND., .OR., and .XOR.. This makes it possible to test a rather complex condition with ONE statement. Example: >10 IF A>B.AND. A>C. OR. A>D THEN.... Additionally, the NOT ([expr]) operator can be used. Example: >10 IF NOT (A>B).AND. A10 STRING 100,20 >20 $(1)="THIS IS A STRING," >30 INPUT "WHAT'S YOUR NAME? - ",$(2) >40 PRINT $(1), $(2) >RUN WHAT'S YOUR NAMR? - FRED THIS IS A STRING, FRED STRINGS can also be assigned to each other with a LET statement. Example: $(2)=$(1) Would assign the STRING value in $(1) to the STRING $(2). 6.2 THE ASC OPERATOR In MCS BASIC-52, two operators manipulate STRINGS. These operators are ASC ( ) and CHR ( ). Admittedly, the string operators contained in MCS BASIC-52 are not quite as powerful as the string operators contained in some BASICS. But surprisingly enough, by using the string operators available in MCS BASIC-52 it is possible to manipulate strings in almost any way imaginable. This in itself is a commendable feat since MCS BASIC-52 was designed primarily to be a sophisticated BASIC language oriented controller, not a string manipulator. The string operators available in MCS BASIC-52 are as follows: ASC( ) The ASC( ) operator returns the integer value of the ASCII character placed in the parentheses. Example: >PRINT ASC(A) 65 65 is the decimal representation for the ASCII character "A." In addition, individual characters in a pre-defined ASCII string can be evaluated with the ASC( ) operator . Example: >10 $(1)="THIS IS A STRING" >20 PRINT $(1) >30 PRINT ASC ($(1),1) >RUN THIS IS A STRING 84 When the ASC( ) operator is used in the manner shown above, the $([expr]) denotes what string is being accessed and the expression after the comma "picks out" an individual character is the string. In the above example, the first character in the string was picked out and 84 is the decimal representation for the ASCII character "T." 6.2 THE ASC OPERATOR Example: >10 $(1)="ABCDEFGHIJKL" >20 FOR X=1 TO 12 >30 PRINT ASC ($(1),X), >40 NEXT X >RUN 65 66 67 68 69 70 71 72 73 74 75 76 The numbers printed in the previous example are the values thaat represent the ASCII characters A,B,C,,...L. Additionally, the ASC ( ) operator can be used to change individual charracters in a defined string. Example: >10 $(1)="ABCDEFGHIJKL" >20 PRINT $(1) >30 ASC ($(1),1)=75 >40 PRINT $(1) >50 ASC ($(1),2)=ASC($(1),3) >60 PRINT $(1) >RUN ABCDEFGHIJKL KBCDEFGHIJKL KCCDEFGHIJKL In general, the ASC( ) operator lets the programmer manipulate individual characters in a string. A simple proram can determine if two strings are identical. Example: >10 $(1)="SECRET" : REM SECRET IS THE PASSWORD >20 INPUT "WHAT'S THE PASSWORD - ",$(2) >30 for I=1 TO 6 >40 IF ASC ($(1), I)=ASC ($(2),I) THEN NEXT I ELSE 70 >50 PRINT "YOU GUESSED IT!" >60 END >70 PRINT "WRONG, TRY AGAIN" : GOTO 20 >RUN WHAT'S THE PASSWORD - SECURE WRONG, TRY AGAIN WHAT'S THE PASSWORD - SECRET YOU GUESSED IT 6.3 THE CHR OPERATOR CHR( ) The CHR( ) operator is the converse of the ASC( ) operator. It converts a numeric expression to an ASCII character. Example: >PRINT CHR (65) A Like the ASC( ) operator, the CHR( ) operator can also "pick out" individual characters in a defined ASCII string. Example: >10 $(1)="MCS BASIC-52" >20 FOR I=1 TO 12 : PRINT CHR ($(1),I), : NEXT I >30 PRINT : FOR I=12 TO 1 STEP -1 >40 PRINT CHR ($(1),I), : NEXT I >RUN MCS BASIC-52 25-CISAB SCM In the above example, the expressions contained within the parentheses, following the CHR operator have the same meaning as the expression in the ASC( ) operator. Unlike the ASC( ) operator, the CHR( ) operator CANNOT be assigned a value. A statement such as CHR($(1),1) = H, is INVALID and will generate a BAD SYNTAX ERROR. Use the ASC( ) operator to change a value in a string. The CHR( ) operator can only be used within a print statement. 7.1 SPECIAL FUNCTION OPERATORS The following operators directly manipulate the 805AH's special function registers. Specific details of the operation of these registers is in the MICROCONTROLLER USERS HANDBOOK, available from INTEL. IE The IE operator is used to retrieve or assign a value to the 8052AH's special function register IE. Since the IE register on the 8052AH is a BYTE register, the value assigned to IE must be between 0 and 255. The IE register on the 8052AH contains an unused bit. BIT IE.6. Since this bit is "undefined," it may be read as a random one or zero, so the user may want to mask this bit when reading the IE register. This can be done with a statement like A=IE.AND.OBFH. The only statements in MCS BASIC-52 that write to the IE register are the CLOCK0, CLOCK1, ONEX1, CLEAR, AND CLEARI statements. Examples: IE = 81H and A = IE.AND.OBFH IP The IP operator is used to retrieve or assign a value to the 8052AH's special function register IP. Since the IP register on the 8052AH is a BYTE register, the value assigned to IP must be between 0 and 255. The IP register on the 8052AH contains two unused bits, BIT IP.6 and IP.7. Since these two bits arae "undefined," they may be read as a random 1 or 0, so the user may want to mask these bits when readint the IP register. This can be done with a statement such as B=IP.AND.3FH. MCS BASIC-52 does not write to the IP register during initialization, so user can establish whatever interrupt priorities are required in a given application. Examples: IP = 3 and A = IP.AND.3FH PORT1 The PORT1 operator is used to retrieve or assign a value to the 8052AH's PORT1 I/O port. Since PORT1 on the 8052AH is a BYTE wide register, the value assigned to PORT1 must be between 0 and 255 inclusive. Certain bits on PORT1 have pre-defined functions. If the user does ot implement any of the hardware associated with these pre-defined functions, The PORT1 instruction can be used in any manner appropriate in the application. 7.1 SPECIAL FUNCTION OPERATORS PCON The PCON operator is used to retrieve or assign a value to the 8052AH's PCON register. In the 8052AH, only the most significant bit of the PCON register is used, all other bits are undefined. Setting this bit will double the baud rate if TIMER/COUNTER 1 is used as the baud rate generator for the serial port. PCON is a byte register. RCAP2 The RCAP2 operaator is used to retrieve and/or assign a value to the 8052AH's special function registers RCAP2H and RCAP2L. This operator treats RCAP2H and RCAP2L as a 16-bit register pair. RCAP2H is the high byte and RCAP2L is the low byte. The RCAP2H and RCAP2L registers are the reload/capture registers for TIMER2. The user must use caution when writing to RCAP2 register because RCAP2 controls the BAUD rate of the serial port on the MCS BASIC-52 device. The following can be used to determine what BAUD rate the MCS BASIC-52 device is operating at: BAUD = XTAL/(32*(65536-RCAP2) ) T2CON The T2CON operator is used to retrieve and/or assign a value to the 8052AH's special function register T2CON. The T2CON is a byte register that controls TIMER2's mode of operation and determines which timer (TIMER1 or TIMER2) is used as the 8052AH's baud rate generator. MCS BASIC-52 initializes T2CON with the values 52 (34H) and assumes that its value is never changed. Randomly changing the value of T2CON, without knowing what you are doing can "crash" the serial port on the 8052AH. Beware! 7.1 SPECIAL FUNCTION OPERATORS TCON The TCON operator is used to retrieve and/or assign value to the 8052AH's special function register TCON. TCON is a byte register that is used to enable or disable TIMER0 and TIMER1, plus the interrupts that are associated with these timers. Additionally, TCON determines whether the external interrupt pins on the 8052AH are operating in a level sensitive or edge-triggered mode. MCS BASIC-52 initializes TCON with the value 244 (OF4H) and assumes that it is never changed. The value 244 (OF4H) places both TIMER0 and TIMER1 in the run (enabled) mode. If the user disables the operation of TIMER0, by clearing BIT 4 in the TCON register, the REAL TIME CLOCK will NOT work. If the user disables the operation of TIMER1, by clearing BIT 6 in the TCON register, the EPROM programming routines, the software serial port, and the PWM statemment will NOT work. Use caution when changing TCON!!! TMOD The TMOD operator is used to retrieve and/or assign a value to the 8052AH's special function register TMOD. TMOD is a byte register that controls TIMER0 and TIMER1's mode of operation. MCS BASIC-52 initializes the TCON register with a value of 16 (10H). The value 16 (10H) places TIMER0 in mode 0, which is a 13-bit counter mode and TIMER1 in mode 1, which is a 16-bit counter mode. MCS BASIC-52 assumes that the modes of these two timer/counters are never changed. If the user changes the mode of TIMER0, the REAL TIME CLOCK will not operate properly. If the user changes the mode of TIMER1, EPROM programming, the software serial port, and the PWM statement will not work properly. If the user does not use these features available in MCS BASIC-52, either timer/counter can be placed in any mode required by the specific application. 7.1 SPECIAL FUNCTION OPERATORS TIME The TIME operator is used to retrieve and/or assign a value to the REAL TIME CLOCK resident in MCS BASIC-52. After reset, TIME is equal to 0. The CLOCK1 statement enables the REAL TIME CLOCK. When the REAL TIME CLOCK is enabled, the SPECIAL FUNCTION OPERATOR, TIME will increment once every 5 milliseconds. The TIME operator uses TIMER0 and the interrupts associated with TIMER0 on the 8052AH. The unit of TIME is seconds and the appropriate XTAL value must be assigned to insure that the TIME operator is accurate. When TIME is assigned a value with a LET statement (i.e. TIME = 100), only the integer portion of TIME will be changed. Example: >CLOCK1 (Enable REAL TIME CLOCK) >CLOCK0 (Disable REAL TIME CLOCK) >PRINT TIME (display TIME) 3.315 >TIME = 0 (Set TIME = 0) >PRINT TIME (display TIME) .315 (only the integer is changed) The "fraction" portion of TIME can be changed by manipulating the contents of internal memory locaiton 71 (47H). This is accomplished by a DBY(71) statement. Note that each count in internal memory location 71(47H) represents 5 milliseconds of TIME. Continuing with the EXAMPLE; >DBY(71) = 0 (Fraction of TIME = 0) >PRINT TIME 0 >DBY (71) = 3 (fraction of TIME = 3, 15 MS) >PRINT TIME 1.5 E-2 7.1 SPECIAL FUNCTION OPERATORS The reason only the integer portion of TIME is changed when assigned a value is that it allows the user to generate accurate time intervals. For instance, let's say you want to creaate an accurate 12 hour clock. There are 43200 seconds in a 12 hour period, so an ONTIME 43200, [ln num] statement is used. Now, when the TIME interrupt occurs the statement TIME = 0 is executed, but the millisecond counter is ot re-assigned a value so if interrupt latency happens to exceed 5 milliseconds, the clock will still remain accurate. TIMER0 The TIMER0 operator is used to retrieve or assign a value to the 8052AH's special function registers TH0 and TL0. This operator treats the byte registers TH0 and TL0 as a 16-bit register pair. TH0 is the high byte and TL0 is the low byte. MCS BASIC-52 uses TH0 and TL0 to implement the REAL TIME CLOCK function. If the user does not implemment the REAL TIME CLOCK function (i.e. does not use the statement CLOCK1) in the BASIC program TH0 and TL0 may be used in any manner suitable to the particular application. TIMER1 The TIMER1 operator is used to retrieve or assign a value to the 8052AH's special function registers TH1 and TL1. This operator treats the byte registers TH1 and TL1 as a 16-bit register pair. TH1 is the high byte and TL1 is the low byte. MCS BASIC-52 uses TH1 and TL1 to implement the timings for the software serial port, the EPROM programming feature, and the PWM statement. If the user does not use any of these features TH1 and TL1 may be used in any manner suitable to the particular application. TIMER2 The TIMER2 operator is used to retrieve or assign a value to the 8052AH's special function registers TH2 and TL2. This operator treats the byte registers TH2 and TL2 as a 16-bit register pair. TH2 is the high byte and TL2 is the low byte. MCS BASIC-52 uses TH2 and TL2 to generate the baud rate for the serial port. If the user does not use TIMER2 to clock the serial port, TH2 and TL2 may be used in any anner suitable to the particular application. 9.3 SPECIAL FUNCTION OPERATORS XTAL The XTAL operator tells MCS BASIC-52 what frequency the system is operating at. The XTAL operator is used by MCS BASIC-52 to calculaate he REAL TIME CLOCK reload value, the PROM programming timing, and the software serial port baud rate generation. The XTAL value is expressed in Hz. So, XTAL = 9000000 would set the XTAL value to 9 MHz. 7.2 EXAMPLES OF MANIPULATING SPECIAL FUNCTION VALUES Using the logical operators available in MCS BASIC-52, it is possible to write or read from any byte of the special function registers that MCS BASIC-52 treats as a register pair: Example: WRITING TO THE HIGH BYTE >TIMER0 = (TIMER0 .AND. OOFFH) + INT(256*(USER BYTE)) Example: WRITING TO THE LOW BYTE >TIMER0 = (TIMER0 .AND. OOFFOOH) + (USER BYTE) Example: READING HIGH BYTE >PHO. INT (TIMER0/256) Example: READING LOW BYTE >PHO. TIMERO .AND. OFFH TIMER1 can function as the baud rate generator for MCS BASIC-52. To assign TIMER1 as the baud rate generator, the following instructions must be executed: >TMOD = 32 - TIMER1 in auto reload mode >TIMER1 = 256*(256-(65536-RCAP2)/12) - load TIMER1 >T2CON = 0 - use TIMER1 as baud rate gen This sequence of instructions can be executed in either the direct mode or as part of a program. When TIMER1 is used as the baud rate generator, TIMER2 can be used in any way suitable to the application. The PROG, FPROG, LIST#, PRINT# and PWM commands/statements cannot be used when TIMER1 functions as the baud rate generator for the MCS BASIC-52 device. Certain crystals may not be able to use TIMER1 as the baud rate generator, especially at high (above 2400) baud rates. 7.3 SYSTEM CONTROL VALUES The SYSTEM CONTROL VALUES determine or reveal how memory is allocated by MCS BASIC-52. MTOP After reset, MCS BASIC-52 sizes the external memory and assigns the last valid memory address to the SYSTEM CONTROL VALUE, MTOP, MCS BASIC-52 will not use any external RAM memory beyond the value assigned to MTOP. If the user wishes to allocate some external memory for an assembly language routine the LET statement can be used (e.g. MTOP = USER ADDRESS). If the user assigns a value to MTOP that is greater than the last valid memory address, a MEMORY ALLOCATION ERROR will be generated. Examples: >PRINT MEOP 2047 >MTOP=2000 >PRINT MTOP 2000 LEN The SYSTEM CONTROL VALUE, LEN, tells the user how many bytes of memory the current selected program occupies. Obviously, LEN cannot be assigned a value, it can only be read. A NULL program (i.e. no program) will return a LEN of 1. The 1 represents the end of program file character. FREE The SYSTEM CONTROL VALUE, FREE, tells the user how many bytes of RAM memory are available to the user. When the current selected is in RAM memory, the following relationship will always hold true. FREE = MTOP - LEN - 511 NOTE: Unlike some BASICS, MCS BASIC-52 does not require any "dummy" arguments for the SYSTEM CONTROL VALUES. ERROR MESSAGES, BELLS, WHISTLES AND ANOMALIES 8.1 ERROR MESSAGES MCS BASIC-52 has relatively sophisticated ERROR processor. When BASIC is in the RUN mode, the generalized form of the ERROR message is as follows: ERROR: XXX - IN LINE YYY YYY BASIC STATEMENT --------------------z Where XXX is the ERROR TYPE and YYY is the line number of the program in which the error occurred. A specific example is: ERROR: BAD SYNTAX - IN LINE 10 10 PRINT 34*21* ----------------------x The X signifies approximately where the ERROR occurred in the line number. The specific location of the X may be off by one or two characters or expressions depending on the type of error and where the error occurred in the program. If an ERROR occurs in the COMMAND MODE only the ERROR TYPE will be printed out NOT the Line number. This makes sense, because there are no line numbers in the COMMAND MODE. The ERROR TYPES are as follows: BAD SYNTAX A BAD SYNTAX error means that either an invalid MCS BASIC-52 COMMAND< STATEMENT, or OPERATOR was entered and BASIC cannot process the entry. The user should check and make sure that everything was typed in correctly. In Version 1.1 of MCS BASIC-52 a BAD SYNTAX ERROR is also generated if the programmer attempts to use a reserved keyword as part of a variable. BAD ARGUMENT When the argument of an operator is not within the limits of the operator a BAD ARGUMENT ERROR will be generated. For instance, DBY(257) would generate a BAD ARGUMENT ERROR because the argument for the DBY operator is limited to the rante 0 to 255. Similarly, XBY (5000H) = -1 would generate a BAD ARGUMENT ERROR because the value of the XBY operator is limited to the range 0 to 255. 8.1 ERROR MESSAGES ARITH. UNDERFLOW If the result of an arithmetic operation exceeds the lower limit of an MCS BASIC-52 floating point number, an ARITH. UNDERFLOW ERROR will occur. The smallest floating point number in MCS BASIC-52 is IE - 127. For instance, IE - 80/IE+80 would cause an ARITH. UNDERFLOW ERROR. ARITH. OVERFLOW If the result of an arithmetic operation exceeds the upper limit of an MCS BASIC-52 floating point number, an ARITH. OVERFLOW ERROR will occur. The largest floating point number in MCS BASIC-52 is .99999999E+127. For instance, IE+70 would cause an ARITH. OVERFLOW ERROR. DIVIDE BY ZERO A division by ZERO was attempted i.e. 12/0, will cause a DIVIDE BY ZERO ERROR. ILLEGAL DIRECT (VERSION 1.0 ONLY) Some statements, such as IF-THEN and DATA cannot be executed while the MCS BASIC-52 device is in the COMMAND MODE. If you attempt to execute one of these statements the message ERROR: ILLEGAL DIRECT will be printed to the console device. The ILLEGAL DIRECT ERROR is not trapped in Version 1.1 of MCS BASIC-52, ILLEGAL DIRECT ERRORS return a BAD SYNTAX ERROR in Version 1.1. LINE TOO LONG (VERSION 1.0 ONLY) If you type in a line that contains more than 73 characters the message ERROR: LINE TOO LONG will be printed to the console dvice. MCS BASIC-52's input buffer can only handle up to 73 characers. NOTE This error does not exist in VERSION 1.1. Instead the input buffer has been increased to 79 characters and MCS BASIC-52 will echo a bell character to the user terminal if too many characters are entered into the input buffer. NO DATA If a READ STATEMENT is executed and no DATA STATEMENT exists or all DATA has been read and a RESTORE instruction was not executed, the message ERROR" NO DATA - IN LINE XXX will be printed to the console device. 8.1 ERROR MESSAGES CAN'T CONTINUE Program execution can be halted by either typing in a control-C to the console device or by executing a STOP STATEMENT. Normally, program execution can be resumed by typing in the CONT command. However, if the user edits the program after halting execution and then enters the CONT command, a CAN'T CONTINUE ERROR will be generated. A control-C must be typed during program execution of a STOP STATEMENT must be executed before the CONT command will work. PROGRAMMING If an error occurs while the MCS BASIC-52 device is programming in EPROM, a PROGRAMMING ERROR will be generated. An error encountered during programming destroys the EPROM FILE STRUCTURE, so the user cannot save any more programs on that particular EPROM once a PROGRAMMING ERROR occurs. A-STACK An A-STACK (ARGUMENT STACK) error occurs when the argument stack pointer is forced "out of bounds." This can happen if the user overflows the argument stack by PUSHing too many expressions oto the stack, or by attempting to POP data off the stack when no data is present. C-STACK A C-STACK (CONTROL STACK) error will occur if the control stack pointer is forced "out of bounds." 158 bytes of external memory are allocated for the control stack, FOR - NEXT loops require 17 bytes of control stack DO - UNTIL, DO - WHILE, and GOSUB require 3 bytes of control stack. This means that 9 nested FOR - NEXT loops is the maximum that MCS BASIC-52 can handle because 9 times 17 equals 153. If the user attempts to use more control stack than is available in MCS BASIC-52 a C-STACK error will be generated. In addition, C-STACK errors will occur if a RETURN is executed before a GOSUB, a WHILE or UNTIL before a DO, or a NEXT before a FOR. 8.1 ERROR MESSAGES I-STACK An I-STACK (INTERNAL STACK) error occurs when MCS BASIC-52 does not have enough stack space to evaluate an expression. Normally, I-STACK errors will not occur unless insufficient memory has been allocated to the 8052AH's stack pointer. Details of how to allocate memory to the tack pointer are covered in the ASSEMBLY LANGUAGE LIKAGE section of this manual. ARRAY SIZE If an array is dimensioned by a DIM statement and then you attempt to access a variable that is outside of the dimensioned bounds, an ARRAY SIZE error will be generated. Example: >DIM A (10) >PRINT A(11) ERROR: ARRAY SIZE READY MEMORY ALLOCATION MEMORY ALLOCATION ERRORS are generated when the user attempts to access STRINGS that are "outside" the defined string limits. Additionally, if the SYSTEM CONTROL VALUE< MTOP is assigned a value that does not contain any RAM memory, a MEMORY ALLOCATION ERROR will occur. 8.5 ANOMALIES Most dictionaries define an anomaly as a deviation from the normal or common order or as an irregularity. Anomalies to an extreme become "BUGS" or something that is wrong with the program. Like all programs, MCS BASIC-52 contains some anomalies, hopefully, no bugs. The purpose of mentioning the known anomalies here is that it may save the programmer some time, should strange things happen during program execution. The known anomalies and cautions are as follows: 1) When using the variable H after a line number, make sure you put a space between the line number and the H, or else BASIC will assume that the line number is a HEX number. Examples: >20H=10 (WRONG) >20 H=10 (RIGHT) >LIST >LIST 32 =10 20 H=10 2) When using the variable I before an ELSE statement, make sure you put a space between the I and the ELSE statement, or else BASIC will assume that the IE portion of IELSE is the special function operator IE. Examples: >20 IF I>10 THEN PRINT IELSE 100 >LIST 20 IF I>10 THEN PRINT IELSE 100 (WRONG) >20 IF I>10 THEN PRINT I ELSE 100 >LIST 20 IF I>10 THEN PRINT I ELSE 100 (RIGHT) 3) A space character may not be placed inside the ASC( ) operator. In other words, a statement like PRINT ASC( ) will yield a BAD SYNTAX ERROR. Spaces may be placed in strings however, so a statement like LET $(1) = "HELLO, HOW ARE YOU" will work properly. The reason ASC( ) yields an error is because MCS BASIC-52 eliminates all spaces when a line is processed, so ASC( ) will be stored as ASC( ) and MCS BASIC-52 interprets this as an error. 1.5 INSTRUCTION SET SUMMARY COMMANDS STATEMENTS OPERATORS RUN BAUD ADD(+) CONT CALL DIVIDE(/) LIST CLEAR EXPONENTIATION(**) LIST# CLEAR (S&I) MULTIPLY(*) LIST@(V1.1) CLOCK(1&0) SUBTRACT(-) NEW DATA LOGICAL AND (.AND.) NULL READ LOGICAL OR (.OR.) RAM RESTORE LOGICAL X-OR (.XOR.) ROM RIM LOGICAL NOT (.OR) XFER DO-WHILE ABS( ) PROG DO-UNTIL INT( ) PROG1 END SGN( ) PROG2 FOR-TO-STEP SQR( ) PROG3 (V1.1) NEXT RND PROG4 (V1.1) GOSUB LOG( ) PROG5 (V1.1) RETURN EXP( ) PROG6 (V1.1) GOTO SIN( ) FPROG ON-GOTO COS( ) FPROG1 ON-GOSUB TAN( ) FPROG2 IF-THEN-ELSE ATN( ) FPROG3 (V1.1 INPUT =, >, >=, < <=, <> FPROG4 (V1.1) LET ASC( ) FPROG5 (V1.1) ONERR CHR( ) FPROG6 (V1.1) ONEX1 CBY( ) ONTIME DBY( ) PRINT XBY( ) PRING# GET PRINT@ (V1.1) IE PHO. IP PHO.# PORT1 PHO.@ (V1.1) PCON PH1. RCAP2 PH1.# T2CON PH1.@ (V1.1) TCON PGM (V1.1) TMOD PUSH TIME POP TIMER0 PWM TIMER1 REM TIMER2 RETI XTAL STOP MTOP STRING LEN UI(1&0) FREE UO(1&0) PI LD@ (V1.1) ST@ (V1.1) IDLE (V1.1) RROM (V1.1)