## CHAPTER 4

### Description of Statements

4.1 DESCRIPTION OF STATEMENTS

STATEMENT: BAUD [expr]

MODE: COMMAND and/or RUN

TYPE: CONTROL

The BAUD [expr] statement is used to set the baud rate for the software line printer port resident on the MCS BASIC-52 device. In order for this statement to properly calculate the baud rate, the crystal (special function operator - XTAL) must be correctly assigned (e.g. XTAL = 9000000). MCS BASIC-52 assumes a crystal value of 11.0592 MHz if no XTAL value is assigned. The software line printer port is P1.7 on the 8052AH device. The main purpose of the software line printer port is to let the user make a "hard copy" of program listings and/or data. The command LIST# and the statement PRINT# direct outputs to the software line printer port. If the BAUD [expr] statement is not executed before a LIST# or PRINT# command/statement is entered, the output to the software line printer port will be at about 1 BAUD and it will take a long time to output something. You may even think that BASIC has crashed, but it hasn't. It's just outputting at a very slow rate. So be sure to assign a BAUD rate to the software printer port before using LIST# or PRINT#. The maximum baud rate that can be assigned by the BAUD statement depends on the crystal. In general, 4800 is a reasonable maximum baud rate. however the user may want to experiment with different rates. The software serial transmits 8 data bits, 1 start bit, and two stop bits. No parity is transmitted.

EXAMPLE:

    BAUD 1200

Will cause the line prlnter port to output data at 1200 BAUD.

VARIATIONS:

None.

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 8052AH to execute the assembly language program beginning at location 9000H (i.e. the program counter will be loaded with 9000H).

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 4100H and vector to that location. This means that CALL 0 will call location 4100H, 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 ONEX1 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 ONEX1 statements must be executed again before the specific interrupts will be enabled.

CLEARS

The CLEARS statement resets all of MCS BASlC-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 5 SECONDS"
    >20 FOR I = 2 TO 9
    >30 N = INT(RND*10) : A - N*I
    >40 PRINT "WHAT IS ",N,"*",I,"?": CLOCK1
    >50 TIME = O : 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 S and CLEAR I respectively. Don't be alarmed, that is the way it's supposed to work.

4.5 DESCRIPTION OF STATEMENTS

STATEMENTS: CLOCK1 and CLOCK0

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.0592 MHz. 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 0.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 CLEARI 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 RRINT A,B
    >40 NEXT I
    >50 RESTORE
    >60 READ A,B
    >770 PRINT A,B
    >80 DATA 10,20,10/2,20/2,SIN(PI),COS(PI)
    >RUN

    10    2O
    5    10
    0   -1
    10   20

VARIATIONS:

None.

Explanation of previous example:

Everytime 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.

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 dimensioned array may not exceed 254 elements. Once a variable is dimensioned in a program it may not be re-dimensioned. An attempt to re-dimension an array will cause an ARRAY SIZE error. If an arrayed variable is used that has not been dimensioned 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 6). So, the array A(100) would require 606 bytes of storage. Memory size usually limits the size of a dimensioned array.

VARIATIONS:

More than one variable can be dimensioned by a single DIM statement, i.e., DIM A(10), B(15), A1(20).

EXAMPLE:

Default error on attempt to re-dimension array...

    >10 A(5)=10     - BASIC ASSIGNS DEFAULT OF 10 TO ARRAY SIZE HERE
    >20 DIM A(5)    - ARRAY CANNOT BE RE-DIMENSIONED
    >RUN

    ERROR ARRAY SIZE - IN LINE 20

    20 DIM A(5)

4.8 DESCRIPTION OF STATEMENTS

STATEMENTS: DO-UNTIL [rel expr]

MODE: RUN

TYPE: CONTROL

The DO-UNTIL [rel expr] 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 DO-UNTIL

    >10 A=0                       >10 DO : A=A+1 : DO : B=B+1
    >20 DO                        >20 PRINT A,B,A*B
    >30 A=A+I                     >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
     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

4.9 DESCRIPTION OF STATEMENTS

STATEMENTS: DO-WHILE [rel 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=3
    >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
                           3 1 3
                           3 2 6
                           3 3 9

                          READY
                          >

VARIATIONS:

None

4.10 DESCRIPTION OF STATEMENTS

STATEMENT: END

MODE: RUN

TYPE: CONTROL

The END statement terminates program execution. The continue command, CONT will not operate it the END statement is used to terminate execution (i.e. a CAN'T CONTINUE error will be printed to the console). The last statement in an MCS BASIC-52 program will automatically terminate program execution if no END statement is used.

EXAMPLES:

    LAST STATEMENT TERMINATION          END STATEMENT TERMINATION

    >1O FOR I=1 TO 4                    >1O FOR I=1 TO 4
    >20 PRINT I                         >20 GOSUB 100
    >30 NEXT I                          >30 NEXT I
    >RUN                                >40 END
                                        >100 PRINT I
     1                                  >110 RETURN
     2                                  >RUN
     3
     4                                   1
                                         2
    READY                                3
    >                                    4

                                        READY
                                        >

VARIATIONS:

None

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=3 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 1 STEP-1) 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 I=1 TO 4        >10 FOR I=0 TO 8 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
                            >

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 I=512 TO 560: PH0. 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:

    10 FOP 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.

The loop is always executed once, that is the test for continuance is done at the NEXT statement.

EXAMPLE:

    >10 FOR I=5 TO 0
    >20 PRINT I
    >30 NEXT I
    >RUN

     5

    READY
    >

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 that a subroutine called by the GOSUB statement can call another subroutine with another GOSUB statement.

EXAMPLES:

    SIMPLE SUBROUTINE                NESTED SUBROUTINES

    >10 FOR I = 1 TO 5                >10 FOR I = 1 TO 3
    >20 GOSUB 100                     >20 GOSUB 100
    >30 NEXT I                        >30 NEXT I
    >100 PRINT I                      >40 END
    >110 RETURN                       >100 PRINT I,
    >RUN                              >110 GOSUB 200
                                      >120 RETURN
     1                                >200 PRINT I*I
     2                                >210 RETURN
     3                                >RUN
     4
     5                                 1 1
                                       2 4
    READY                              3 9
    >
                                      READY
                                      >

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 1. 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.

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 [ln 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 BASlC-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 l00
    >20 PRINT X
    >RUN

    ERROR INVALID LINE NUMER - 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], [ln num], . . . [ln num]

ON [expr] GOSUB[ln num], [ln num], . . . [ln 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 GOTO 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 statement ELSE statement

A specific example is as follows:

    >10 IF A=100 THEN A=0 ELSE A=A+1

Upon execution of line 10 IF 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 <> 10 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.

Comments on IF-THEN-ELSE

Version 1.1 is not compatible with V1.0 when the IF-THEN-ELSE statement is used with multiple statements per line. In V1.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

EXAMPLE 2:

    10 IF A=B THEN C=A
    12 A=A/2
    14 GOTO 100
    20 PRINT

They function in the same manner because V1.0 treats the delimiter (:) exactly the same as a carriage return (cr) in every case. However, V1.1 executes the remainder of the line if and only if the test A = B proves to be true. This means in example1 IF A did equal B, V1.1 would then set C=A, then set A = A/2, then execute line 100. IF A did not equal B, V1.1 would then PRINT A and ignore the statements C=A: A=A/2: GOTO 100. V1.1 will execute example2 exactly the same way as V1.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, V1.0 and V1.1 will treat the statements in the same manner!!

This change was made because most users of MCS BASIC-52 felt that the V1.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 varable 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

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         >10 STRING 110,10
    >20 INPUT "NAME: "$(1)    >20 INPUT "NAMES: ",$(1),$(2)
    >30 PRINT "HI ",$(1)      >30 PRINT "HI ",$(1)," AND ",$(2)
    >RUN                      >RUN

    NAME: SUSAN               NAMES: BILL
    HI SUSAN                  ?ANN
                              HI BILL AND ANN
    READY
    >                         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
    ?15
    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" operator and that 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 $(1)="THIS IS A STRING" or

    LET $(2)=$(1)

Before strings can be assigned the STRING [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 XBY(2000H)=5AH or
    LET DBY(25)=XBY(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 (101H) 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.19 DESCRIPTION OF STATEMENTS

STATEMENT: ONEX1 [ln num]

MODE: RUN

TYPE: CONTROL

The ONEX1 [ln num] statement lets the user handle interrupts on the 8052AH's INT1 pin with a BASIC program. The line number following the ONEX1 statement tells the MCS BASIC-52 interpreter which line to pass control to when an interrupt occurs. In essence, the ONEX1 statement "forces" a GOSUB to the line number following the ONEX1 statement when the INT1 pin on the 8052AH is pulled low. The programmer must execute a RETI statement to exit from the ONEX1 interrupt routine. If this is not done all future interrupts on the INT1 pin will be "locked out" and ignored until a RETI is executed.

The ONEX1 statement sets bits 7 and 2 of the 8052AH's interrupt enable register IE. Before an interrupt can be processed, the MCS BASIC-52 interpreter must complete execution of the staement it is currently processing. Because of this, interrupt latency can vary from microseconds to tens of milliseconds. The ONTIME [expr], [ln num] interrupt has priority over the ONEX1 interrupt. So, the ONTIME interrupt can interrupt the ONEX1 interrupt routine.

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 8052AH operate in the micro-second time frame, there is an inherent incompatibility between the timer/counters on the 8052AH and MCS BASIC-52. To help solve this situation the ONTIME [expr], [ln num] statement was devised. What ONTIME does is generate an interrupt everytime the special finction 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 [ln num] following the expression [expr] in the ONTIME statement.

Since the ONTIME statement uses the special function operator, TIME, the CLOCK1 statement must be executed in order for ONTIME to operate. If CLOCK1 is not executed the special function 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:

    >10 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 : CLOCK1 : 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 ONEX1 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 CLOCK0 or a CLEARI statement should be executed at the beginning of the ONEX1 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 carriage 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.

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:

    >10 FOR I=1 TO 1000
    >20 PRINT I,CR,
    >30 NEXT I

will cause the output to remain only on one line. No line feed will ever be sent to the console device.

USlNG(special characters)

The USING function is used to tell MCS BASIC-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 be 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 determines how many significant digits will be 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 BASIC-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 I=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
    >

USING(#.#) - This will force MCS BASIC-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 be 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 USlNG(Fx) and the USING(#.#) formats will always "align" the decimal points when printing a number. This feature makes displayed columns of numbers easy to read.

USING(0) - This argument lets MCS BASIC-52 determine what format to use. The rules are simple, if the number is between +- 99999999 and +- .1, BASIC will display integers and fractions. If it is out of this range, BASIC will use the USING(F0) format. Leading and trailing zeros will always be suppressed. After reset, MCS BASIC-52 is placed in the USING(0) format.

4.22 DESCRIPTION OF STATEMENTS

STATEMENT: PRINT# or P.# (?# VERSION 1.1 ONLY)

MODE: COMMAND and/or RUN

TYPE: INPUT/OUTPUT

The PRINT#, P.#, and ?# (in Version 1.1 only) statement does the same thing as the PRINT, P. and ? (in Version 1.1 only) statement except that the output is directed to the list device instead of the console device. The BAUD rate to the list device must be initialized by the statement BAUD[expr] before the PRINT#, P.#, or, ?# statement is used. All comments that apply to the PRINT, P. or, ? statement apply to the PRINT#, P.#, or ? statement. P.# and ?# (in Version 1.1 only) are "shorthand" notations for PRINT#.

4.23 DESCRIPTION OF STATEMENTS

STATEMENTS: PH0., PH1., PH0.#, PH1.#

MODE: COMMAND and/or RUN

TYPE: INPUT/OUTPUT

The PH0. and PH 1. statements do the same thing as the PRINT statement except that the values are printed out in a hexadecimal format. The PH0. statement suppresses two leading zeros if the number to be printed is less than 255 (0FFH). The PH1. statement always prints out four hexadecimal digits. The character "H" is always printed after the number when PH0. or PH1. 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 (0FFFFH) inclusive), MCS BASIC-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, PH0., and PH1. can be used to perform decimal to hexadecimal and hexadecimal to decimal conversion. All comments that apply to the PRINT statement apply to the PH0. and PH1. statements. PH0.# and PH1.# do the same thing as PH0. and PH1. respectively, except that the output is directed to the list device instead of the console device.

EXAMPLES:

    >PH0.  2*2    >PH1. 2*2   >PRINT 99H  >PH0.  100
     04H           0004H       153         64H

    >PH0.   1000  >PH1. 1000  >P. 3E8H      >PH0.  PI
     3E8H         03E8H        1000          03H

4.24 DESCRIPTION OF STATEMENTS

STATEMENT: PRINT@, PH0.@, PH1.@ (VERSION 1.1 ONLY)

MODE: COMMAND and/or RUN

TYPE: INPUT/OUTPUT

The PRINT@ (P.@ or ?@), PH0.@, and PH1.@ statements do the same thing as the PRINT (P. or ?), PH0., and PH1. statements respectively except that the output is directed to a user defined output driver. These statements assume that the user has placed an assembly language output routine in external code memory location 403CH. To enable the @ driver routine the user must set bit 27H (39D) in the internal memory of the MCS BASIC-52 device. Bit 27H (39D) is bit 7 of internal memory location 24H (36D). This bit can be set by the BASIC statement DBY(24H) = DBY(24H) .OR. 80H or by a user supplied assembly language routine. If the user evokes the @ driver routine and this bit is not set, the output will be directed to the console driver. The only reason this bit must be set to enable the @ driver is that it adds a certain degree of protection from accidently typing LIST@ when no assembly language routine exist. The philosophy here is that if the user sets the bit, the user provides the driver or else!!!

When MCS BASIC-52 calls the user output driver routine at location 403CH, the byte to output is in the accumulator and R5 of register bank 0 (RB0). The user may modify the accumulator (A) and the data pointer (DPTR) in the assembly language output routine, but cannot modify any of the registers in RB0. This is intended to make it real easy for the user to implement a parallel or serial output driver without having to do a PUSH or a POP.

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 be 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        SUBROUTINE
    VARIABLES       PASSING

    >10 A=10       >10 PUSH 1,3,2
    >20 B=20       >20 GOSUB 100
    >30 PRINT A.C  >3O POP R1,R2
    >40 PUSH A,C   >40 PRINT R1,R2

    >5O POP A,B    >5O END
    >60 PRINT A,B  >100 REM QUADRATIC A=2,B=3,C=1 IN EXAMPLE
    >RUN           >110 POP A,B,C
                   >120 PUSH (-B+SQR(B*B-4*A*C))/(2*A)
    10 20          >130 PUSH (-B-SOR(B*B-4*A*C))/(2*A)
    20 10          >140 RETURN
                   >RUN
    READY
    >              -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 BASIC-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. If 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 BASIC-52.

4.27 DESCRIPTION OF STATEMENTS

STATEMENT: PWM [expr], [expr], [expr]

MODE: COMMAND and/or RUN

TYPE: INPUT/OUTPUT

PWM stands for Pulse Width Modulation. What it does is generate a user defined pulse sequence on P1.2 (bit 2 of I/O PORT1) of the MCS BASIC-52 device. The first expression following the PWM statement is the number of clock cycles the pulse will remain high. A clock cycle is equal to 12/XTAL, which is 1.085 microseconds at 11.0592 MHz. The second expression is the number of clock cycles the pulse will remain low and the third expression is the total number of cycles the user wishes to output. All expressions in the PWM statement must be valid integers (i.e. between 0 and 65535 (0FFFFH) inclusive). Additionally, the minimum value for the first two expressions in the PWM statement is 25.

The PWM statement can be used to create "audiable" feedback in a system. In addition, just for fun, the programmer can play music using the PWM statement. More details about using the PWM statement are in the appendix.

EXAMPLE:

    >PWM 100,100,1000

At 11.0592 MHz would generate 1000 cycles of a square wave that has a period of 217 microseconds (4608 Hz) on P1.2.

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 VARIA3LE
    >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 VARIABLE
    >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 BASIC-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".

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 STOP statement, 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],[exprl 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 $(1)="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 $(0) 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. So, 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.32 DESCRIPTION OF STATEMENTS

STATEMENTS: UI1 and UI0 (USER INPUT)

MODE: COMMAND and/or RUN

TYPE: CONTROL

UI1

The UI1 statement permits the user to write specific console input drivers for MCS BASIC-52. After UI1 is executed BASIC will call external program memory location 4033H when a console input is requested. The user must provide a jump instruction to an assembly language input routine at this location. The appropriate ASCII input from this routine is placed in the 8052AH's accumulator and the user input routine returns back to BASIC by executing an assembly language return instruction. The user must not modify any of the 8052AH's registers in the assembly language program with the exception of the memory and register bank allocated to the user. The assembly language linkage section of this manual explains what memory MCS BASIC-52 allocates to the user and how the user may allocate additional memory if needed.

In addition to providing the input driver routine for the UI1 statement, the user must also provide a console status check routine. This routine checks to see if the console device has a character ready for MCS BASIC-52 to read. BASIC calls external memory location 4036H to check the console status. The console status routine sets the carry bit to 1 (c = 1) if a character is ready for BASIC to read and clears the carry bit (c = 0) if no character is ready. Again, the contents of the registers must not be changed. MCS BASIC-52 uses the console status check routine to examine the keyboard for a control-C character during program execution and during a program LISTing. This routine is also used to perform the GET operation.

UI0

The UI0 statement assigns the console input console routine back to the software drivers resident on the MCS BASIC-52 device. UI0 and UI1 may be placed anywhere within a program. This allows the BASIC program to accept inputs from different devices at different times.

NOTE: The UI0 and UI1 function is controlled by bit 30 (IEH) in the 8052AH's internal memory. Bit 30 is in internal memory location 35.6 (23.6H) i.e. the sixth bit in internal memory location 35 (23H). When bit 30 is set (bit 30 = 1), the user routine will be called. When bit 30 is cleared (bit 30 = 0), the MCS BASIC-52 input driver routine will be used. The assembly language programmer can use this information to change the input device selection in assembly language.

4.33 DESCRIPTION OF STATEMENTS

STATEMENTS: UO1 and UO0 (USER OUTPUT)

MODE: COMMAND and/or RUN

TYPE: CONTROL

UO1

The UO1 statement permits the user to write specific console output drivers for MCS BASIC-52. After UO1 is executed BASIC will call external program memory location 4030H when a console output is requested. The user must provide a jump instruction to an assembly language output routine at this location. MCS BASIC-52 places the output character in register 5 (R5) of register bank 0 (RB0). The user returns back to BASIC executing an assembly language return instruction. The user must not modify any of the 8052AH's registers, including the accumulator during the user output procedure with the exception of the memory and register bank allocated to the user. UO1 gives the user the freedom to write custom output routines for MCS BASIC-52.

UO0

UO0 statement assigns the console output routine back to the software drivers resident on the MCS BASIC-52 device. UO0 and UO1 may be placed anywhere within a program. This allows the BASIC program to output characters to different devices at different times.

NOTE: The UO0 and UO1 function is controlled by bit 28 (1CH) in the 8052AH's internal memory. Bit 28 is in the internal memory location 35.4 (23.4H), i.e. the fourth bit in the internal memory location 35 (28H). When bit 28 is set (bit 28 = 1), the user routines will be called. When bit 28 is cleared, (bit 28 = 0), the MCS BASIC-52 output drivers will be used. The assembly language programmer can use this information to change the output device selection in assembly language.

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], [ln num] or an ONEX1 [ln 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 ONEX1 [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 BASIC-52 device asserts the /DMA acknowlege 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 BASIC-52 device will remain in the IDLE mode when the user assembly language routine returns to BASIC.

An attempt to execute the IDLE statement in the direct mode will yield a BAD SYNTAX error.

4.35 DESCRIPTION OF STATEMENTS

STATEMENT: RROM [integer] (VERSION 1.1 ONLY)

MODE: COMMAND and/or RUN

TYPE: CONTROL

RROM stands for RUN ROM. What it does is select a program in the EPROM file, then execute the program. The integer after the RROM statement selects what program in the EPROM file is to be executed. In the command mode RROM 2 would be equivalent to typing ROM 2, then RUN. But, notice that RROM [integer] is a statement. This means that a program that is already executing can actually force the execution of a completely different program that is in the EPROM file. This gives the user the ability to "change programs" on the fly.

If the user executes a RROM [integer] statement and an invalid integer is entered (say 6 programs are contained in the EPROM file and the user enters RROM 8, or no EPROM is in the system), no error will be generated and MCS BASIC-52 will execute the statement following the RROM [integer] statement.

NOTE - Every time the RROM [integer] statement is executed, all variables and strings are set equal to zero, so variables and strings cannot be passed from one program to another by using the RROM [integer] statement. Additionally, all MCS BASIC-52 evoked interrupts are cleared.

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 battery 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@ 0F005H+6*I : REM STORE IT, SIX BYTES PER NUMBER
    5O NEXT I
    60 REM *** GET ARRAY ***
    70 FOR I = 0 TO 9
    8O LD@ 0F005H+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, 0F004H, 0F003H, 0F002H, 0F01H, and 0F000H.

4.37 DESCRIPTION OF STATEMENTS

STATEMENT: PGM
PGM is disabled in version 1.2 and will return an ERROR : PROGRAMMING.

MODE: COMMAND and/or RUN

TYPE: INPUT/OUTPUT

The PGM statement gives the user the ability to program an EPROM or EEPROM while executing a BASIC program. The PGM statement requires that the user set up internal memory locations 18H (24D), 19H (25D), 1AH (26D), 1BH (27D), 1EH (30D) and 1FH (31D). Note that these internal memory locations are normally reserved for the user!!

The User must initialize these internal memory locations with the following:

EXAMPLE:

       LOCATION        CONTENTS

        1BH:19H        THE ADDRESS OF THE SOURCE INFORMATION THAT IS TO BE
       (27D:25D)       PROGRAMMED INTO THE EPROM. LOCATION 19H IS THE LOW
                       BYTE AND LOCATION 1BH IS THE HIGH BYTE

        1AH:18H        THE ADDRESS - 1 OF THE EPROM LOCATION(S) THAT ARE TO
       (26D:24D)       BE PROGRAMMED, LOCATION 18H IS THE LOW BYTE AND
                       LOCATION 1AH IS THE HIGH BYTE

        1FH:1EH        THE NUMBER OF BYTES THAT THE USER WANTS TO PROGRAM
       (31D:30D)       LOCATION 1EH IS THE LOW BYTE AND LOCATION 1FH IS
                       THE HIGH BYTE

The user must also initialize the width of the desired EPROM programming pulse and store the value in intemal memory locations 40H (64D) (high byte) and 41H (65D) (low byte). The reload for a 50 millisecond EPROM programming pulse is calculated as follows:

    10     REM R = RELOAD VALUE. W = WIDTH IN SECONDS (50 MILLISECONDS)
    20     W = .05
    30     R = 65536 - W * XTAL/12
    40     DBY(40H) = R/256
    50     DBY(41H) = R .AND. 0FFH

In addition, the user must also set or clear bit 38.3 (26.3H) to select the INTELligent EPROM programming algorithm. The bit is set to select INTELligent programming and cleared to select the normal 50 millisecond algorithm. To set the bit, execute a DBY(38) = DBY(38) .OR. 8H Statement, to clear the bit, execute a DBY(38) = DBY(38) .AND. 0F7H instruction.

IMPORTANT NOTE!

When executed in the run mode, The PGM statement will not generate an error if the EPROM fails to program properly. Instead, the control of the program will be passed back to the user just as if the EPROM programmed properly. The user must then examine locations 1EH and 1FH. If the contents of locations 1EH and 1FH both equal zero, then the EPROM programmed properly. If they do not, then an error occurred during the programming process. The user can then examine locations 1AH:18H to determine what location in the EPROM failed to program.

Well, this sounds like a lot to do just to program an EPROM, but it's not so bad. The following program is an example of a universal EPROM/EEPROM programmer built around MCS BASIC-52. This program can program a block of RAM into an EPROM or EEPROM that is addressed at 8000H or above.

EXAMPLE:

    1     XTAL=12000000
    10     PRINT "UNIVERSAL PROM PROGRAMMER" :  PRINT "WHAT TYPE OF DEVICE ?"
    20     PRINT  :  PRINT "1 = EEPROM" :  PRINT "2 = INTELLIGENT EPROM"
    30     PRINT "3 = NORMAL (50 MS) EPROM" :  PRINT  :  INPUT "TYPE (1,2,3) - ",T
    40     ON (T-1) GOSUB 340,350,360
    50     REM this sets up intelligent programming if needed
    60     IF W=.001 THEN DBY(26)=DBY(26).OR.8 ELSE DBY(26)=DBY(26).AND.0F7H
    70     REM calculate pulse width and save it
    80     PUSH (65536-(W*XTAL/12)) :  GOSUB 380
    90     POP G1 : DBY(40H)=G1 :  POP G1 : DBY(41H)=G1 :  PRINT
    100    INPUT " STARTING DATA ADDRESS - ",S :  IF S<512.OR.S>0FFFFH THEN 100
    110    PRINT  :  INPUT " ENDING DATA ADDRESS - ",E
    120    IF E < 0FFFFH THEN 110
    130    PRINT  :  INPUT " PROM ADDRESS - ",P :  IF P<8000H.OR.P>0FFFFH THEN 130
    140    REM calculate the number of bytes to program
    150    PUSH (E-S)+1 :  GOSUB 380 :  POP G1 : DBY(31)=G1 :  POP G1 : DBY(30)=G1
    160    REM set up the eprom address
    170    PUSH (P-1) :  GOSUB 380 :  POP G1 : DBY(26)=G1 :  POP G1 : DBY(24)=G1
    180    REM set up the source address
    190    PUSH S :  GOSUB 380 :  POP G1 : DBY(27)=G1 :  POP G1 : DBY(25)=G1
    200    PRINT  :  PRINT "TYPE A 'CR' ON THE KEYBOARD WHEN READY TO PROGRAM"
    210    REM wait for a 'cr' then program the eprom
    220   X=GET :  IF X<>0DH THEN 220
    230    REM program the eprom
    240    PGM
    250    REM see if any errors
    260    IF (DBY(30).OR.DBY(31))=0 THEN  PRINT "PROGRAMMING COMPLETE" :  END
    270    PRINT  :  PRINT "***ERROR***ERROR***ERROR***ERROR***" :  PRINT
    280    REM these routines calculate the address of the source and
    290    REM eprom location that failed to program
    300   S1=DBY(25)+256*DBY(27) : S1=S1-1 : D1=DBY(24)+256*DBY(26)
    310    PH0. "THE VALUE ",XBY(S1), :  PH1. " WAS READ AT LOCATION ",S1 :  PRINT
    320    PH0. "THE EPROM READ ",XBY(D1), :  PH1. " AT LOCATION ",D1 :  END
    330    REM these subroutines set up the pulse width
    340   W=.0005 :  RETURN
    350   W=.001 :  RETURN
    360   W=.05 :  RETURN
    370    REM this routine takes the top of stack and returns high, low bytes
    380    POP G1 :  PUSH (G1.AND.0FFH) :  PUSH (INT(G1/256)) :  RETURN