While it’s entertaining to display messages on the LCD using immediate mode, it’s much more useful to write programs that can control the LCD. Type in the following:
>NEW
>10 FOR I = 1 TO 1000
>20 PRINT #3, "I="; I
>30 NEXT
>40 END
>_
The NEW command clears out any program already in memory and prepares for you to enter a new one. Individual program instructions are entered using a line number. The interpreter uses line numbers to sort the program instructions. When you run the program, the instructions execute in line number order. Now let's run the program:
>RUN
There’s a blur of activity on the display and it finally shows:
I=1000
The problem is the program has executed so quickly that all the intermediate display commands, which show values counting from 1 to 1000, are almost instantaneously overwritten with new contents. So, we’ll alter the program to slow it down so we can see it running:
>25 FOR J = 1 TO 100: NEXT
>LIST
10 FOR I = 1 TO 1000
20 PRINT #3, "I="; I
25 FOR J = 1 TO 100: NEXT
30 NEXT
40 END
>
The LIST command instructs the interpreter to list the program. Notice the interpreter has sorted the program instructions and that line 25 that is shown between lines 20 and 30, not after line 40. Because you’ll need to edit your program during development and debugging, it’s a good idea to enter line numbers in steps of ten which leaves space for new lines to be inserted.
The new line we entered causes the program to pause for a short time before continuing—it's a do-nothing loop that just delays for a little while. When you run the program a second time, again using RUN, you’ll see the display change much more slowly, allowing you to read the intermediate values.
Now there’s a new problem: the display is easily read, but it’s taking too long to run through all 1,000 values. You can stop the program at any time by typing Ctrl+C (that is, holding the Ctrl button while pressing C), and if you try it now the interpreter responds with:
>RUN
BREAK IN LINE 25
>
The line number in the message will be 10, 20, 25, or 30, depending on which line was executing when you interrupted, or “broke into,” the program.
Rather than count from 1 to 1000, we’ll just count to 50:
>10 FOR I = 1 TO 50
>LIST
10 FOR I = 1 TO 50
20 PRINT #3, "I="; I
25 FOR J = 1 TO 100: NEXT
30 NEXT
40 END
>_
You can see that the original line 10 has been replaced with the newly typed line 10. If you want to delete a line entirely, just type its line number and press return.
In this first tutorial we'll be introducing how to create, manipulate, and execute BASIC programs. Each line of text that makes up the program starts with a line number that identifies where in the program that line should be placed, as opposed to a text editor or word processing programs where you position the cursor to identify where text should be placed.
Type in what you see below. After each line, press the Return key.
>NEW
>10 PRINT "'Twas busy, and the server tones,"
>20 PRINT "Did beep and buzz all day,"
>30 PRINT "And Java was okay."
>_
NEW clears out any program that exists in the memory of the computer. If you followed the instructions in the previous section and entered a program, this will erase that program and start a new one for you. Because you started each line with a number, the computer places these lines into its permanent memory used for storing programs.
To show the program stored in the computer’s memory type LIST:
>LIST 10 PRINT "'Twas busy, and the server tones," 20 PRINT "Did beep and buzz all day," 30 PRINT "And Java was okay." >_
As you can see, the computer remembers what you typed in. Now, type the following line:
>25 PRINT "All satisfied were the Netscape users," >_
Typing LIST again, you’ll notice the computer inserted that line in proper numerical order:
>LIST 10 PRINT "'Twas busy, and the server tones," 20 PRINT "Did beep and buzz all day," 25 PRINT "All satisfied were the Netscape users," 30 PRINT "And Java was okay." >_
The point of programming is not to store a program, but to execute it! You instruct the computer to execute your program using RUN which causes the computer to go through your program and execute the commands that you typed in, one by one, in line number order. Using the program we typed in above, our program uses the PRINT command to display some lines of text. (This is a rather amusing parody of Lewis Carrol’s Jabberwocky, of course) Your screen should now look something like this:
>RUN 'Twas busy, and the server tones, Did beep and buzz all day, All satisfied were the Netscape users, And Java was okay. >_
The RUN command can also be told which line to start the program at. Usually, it just starts executing from the first line of the program. But if you want to skip ahead and start at a later point, type the line number you want to start at after the RUN command:
>RUN 25 All satisfied were the Netscape users, And Java was okay. >_
Nobody writes perfect programs on the first attempt, so we need some way of changing an existing program without having to rewrite it. To change a line of code, simply type a line with the same line number, and the new content will replace the old:
>25 PRINT "All satisfied were the Opera users," >_
If you list the program again, and it will show that we replaced line 25:
>LIST 10 PRINT "'Twas busy, and the server tones," 20 PRINT "Did beep and buzz all day," 25 PRINT "All satisfied were the Opera users," 30 PRINT "And Java was okay." >_
If you need to remove a program line completely, type the line number and press Return. So, to remove line 20 from our program completely:
>20 >LIST 10 PRINT "'Twas busy, and the server tones," 25 PRINT "All satisfied were the Opera users," 30 PRINT "And Java was okay." >_
The NEW command erases the current program from memory, should you want to start from scratch. Use this command carefully; if you need to store your program, list it out and use HyperTerminal to capture and save it to a file.
The interpreter has 26 32-bit integer variables, A through Z.
FOR var = expr1 TO expr2
statements
NEXT
Runs a for-loop over var from expr1 to expr2 inclusive. STEP is not supported. If expr1 > expr2, the loop is not executed at all and continues after a matching NEXT.
IF expr THEN statement
IF expr statement
If expr is non-zero, execute statement
WHILE expr
statements
END WHILE | WEND
Runs statements while expr is non-zero. If expr is zeo on entry to the while loop, the loop is not executed and continues after the END WHILE.
REPEAT
statements
UNTIL expr
Runs statements until expr is zero. The loop iterates at least once.
EXIT FOR
Exits the innermost for loop and transfers control to the statement after the matching NEXT.
EXIT WHILE
Exits the innermost while loop and transfers control to the statement after the matching END WHILE.
EXIT REPEAT
Exits the innermost repeat loop and transfers control to the statement after the matching UNTIL.
GOTO line
Transfers control to the line numbered line.
GOSUB line
Transfers control to the subroutine starting at the line numbered line.
RETURN
Returns from the innermost subroutine.
[ LET ] var = expr
Assigns expr to var.
END
Ends program execution.
STOP
Stops program execution. Use CONTINUE to continue.
REM comment
' comment
Disregards comment and continues to the next line.
AUTO line
Starts automatically displaying line numbers on program entry
TRACE ON | OFF
Traces program execution. Currently unimplemented.
LIST [ # device]
Displays the current progam on device.
PRINT [ # device ] (expr | string) [; | ,] (expr | string)...
Prints expr or string to device device. You can use modifiers HEX and BIN to print values in hex and binary, e.g.
PRINT HEX(10), BIN(20)
POKE addr, expr
Pokes expr into the byte at address addr.
DPOKE addr, expr
Pokes expr into the word at address addr.
DIM var(expr)
Allocates expr elements from RAM and places the base address in var. Adjusts TOP.
RESET
Resets the processor.
READ var, var...
Read into var from DATA statements.
RESTORE [ line ]
Restore data pointer to line line or start of program if line is not given.
DATA value, value...
Data values.
RANDOMIZE expr
Set random number seed to expr.
Usual precedence applies. Also provided are IMP, EQV for logical implication and equivalence, BIC to clear bits, and BIT to test bits.
e.g.
PRINT BIT 4 prints 16 (BIT n is 1 shifted left n places).
PRINT 31 BIT 4 prints -1 (bit 2 of 15 is true)
PRINT 9 BIT 2 prints 0 (bit 2 of 9 is 0)
PRINT 3 BIC 1 prints 2 (3 AND NOT 1)
PRINT NOT 1 prints -2
TRUE is -1, FALSE is 0.
TOP is the first free address that is not used by the interpreter. It is moved by DIM.
RND generates a random number
ABS returns the magnitude of its argument
SGN returns the sign of its argument, or zero if the argument is zero,
PEEK returns the byte at the argument address.
DPEEK returns the word at the argument address.
!addr returns the 32-bit word at addr.
@addr returns the 16-bit word at addr.
?addr returns the byte word at addr.
You can assign using !, @, and ?, e.g.
!10 = 0 ' poke four zeroes into addresses 10, 11, 12, and 13