CHAPTER 1
DIRECT AND INDIRECT MODE


The Sorcerer uses Standard BASIC in two different ways: the direct mode and the indirect mode. In the direct mode, the Sorcerer executes each statement or command as soon as you hit RETURN. The results of arithmetical or logical operations appear on the screen, and they are also saved in memory for later use. However, the Sorcerer forgets each instruction after execution.

Example:

You type:

PRINT 1+2

Sorcerer replies:

3
READY

The instruction PRINT 1+2 has been forgotten.

Example:

You type:

A=1+2+3:PRINT A

Sorcerer replies:

6
READY

The variable A and that it has been assigned the value 6 are stored in memory. Thereafter, until A is assigned another value, each time you use the instruction PRINT A in direct mode (and do not leave direct mode), the Sorcerer responds as above.

In direct mode, you can use the Sorcerer like a pocket calculator.

Example:

Total this list of household expenses:


rent...............$250.00
food...............$119.72
gas................$ 36.17
electricity........$ 8.05
telephone..........$ 21.30

You type:

PRINT 250.00+119.72+36.17+8.05+21.30

Sorcerer replies:

435.24
READY

(Notice that the Sorcerer responds with the proper number of decimal places. While no coincidence, this is not the way Standard BASIC handles all numbers. You will find out more about numbers in Chapter 5.)

When the Sorcerer is prepared to accept direct mode commands, we say that it is at the command level. The Sorcerer tells you that it is in this condition by giving the READY prompt.


INDIRECT MODE


In the indirect mode, the Sorcerer stores all the commands and statements of a program in its memory. It will not execute any of these instructions until you tell it to run the program (by typing RUN RETURN ). Every line of instruction must begin with a line number, which must be an integer from 0 to 65529. This number serves five purposes.

  1. It tells the Sorcerer that the instruction is in indirect mode, rather than direct mode.
  2. It tells the Sorcerer where to put the instruction in its memory.
  3. It gives the Sorcerer the sequence in which the instructions are to be executed.
  4. It enables you to branch or skip from one part of the program to another.
  5. It enables you to call a given instruction from the memory so that you can make corrections or changes in the program.

Example:

          line
number operands or data
You type: 10 PRINT 1 + 2
20 PRINT 3 + 4
command

The Sorcerer stores these instructions in memory.

Now type: RUN (Note: this is a command in direct mode.)

Sorcerer replies:

3
7
READY

The program remains in memory. You can run it again, or change it if you like.

You type: 15 PRINT 2 + 3

This inserts a new line (line number 15) between lines 10 and 20.

Now type: RUN

Sorcerer replies:

3
5
7
Ready

As you have just seen, typing the direct mode command RUN causes the Sorcerer to run the program currently in memory. To clear a program out of memory, give the command NEW.

Example:

You type: NEW

Sorcerer replies:

READY

You are now ready to write a new program. First we will introduce a new instruction, the LET statement (also called an assignment statement).

Example:

You type:

10 LET A=1
20 LET B=2

Here, A and B are variables. You will learn more about variables in Chapter 5. For now, it is enough to know that a variable is the name of a storage location in memory. The LET statement tells the Sorcerer what value to assign to the variable.

The most general LET statement for numerical variables looks like this:

[ LET] < name of variable> = < numerical expression >

(Don't let this = sign confuse you. BASIC uses the = sign in two ways. In an assignment statement it does not mean "is the same as." It is just a shorthand expression for "is assigned the value of." In other contexts (which we discuss later) the = sign has the familiar mathematical meaning.)

The numerical expression can contain variables as well as numbers -- even the variable whose value is being assigned.

Examples:


The assignment
statement means
LET A = 1 put the number 1 into storage location A
LET A = B + 1 find the number in storage location B, add
1 to it, and store the result in location A
LET A = A + 1 find the number currently in location A, add
1 to it and put the result back into location A

NOTE

LET A = A + 1 does not mean "A is equal to A+1." Here you see that the = sign used as an assignment operator is not the same as the mathematical = sign and, in fact, makes no sense in the mathematical context. A cannot equal A+1 .· It can be assigned the value of A+1.


Now to continue our program:

You type:

30 LET C=A+B
40 PRINT C
RUN

Sorcerer replies:

3
READY

You type:

50 PRINT C+1
RUN

Sorcerer replies:

3
4
READY

The word LET is always optional. These two lines are equivalent:

30 LET C=A+B
and
30 C=A+B

You must assign a variable a value for it to have one. This is called initialization. If you give no value to a variable, the Sorcerer gives it the value of 0. If you have not assigned a value to X, and tell the Sorcerer to PRINT X, it responds with 0. In many other versions of BASIC, trying to print an uninitialized variable causes an error message, usually accompanied by a break in the program.


NOTE

We recommend that you not rely on the Sorcerer to do your initiallzation for you. Assign your own values to variables before using them the first time, or you may introduce unexpected errors into your programs.


So far you have seen how to instruct the Sorcerer to print the value of a numerical expression (such as 1+2, C or C+1). The Sorcerer also prints text. The instruction for this is:

PRINT "< text >''

The expression inside the quote marks is called a string. You will learn more about strings in Chapter 9. For now, it is enough to know that a string can be any combination of letters, numbers, spaces, punctuation marks or mathematical symbols (in fact, any of the characters that your Sorcerer will print on screen), provided that:

  1. None of the characters is a quote mark (") or at symbol (@)
  2. There are no more than 255 characters in all (including spaces)

Now give the command NEW to clear out the old program. Besides erasing the old program from memory, the command NEW resets the values of all variables to zero.

You can print a blank line by giving the instruction:

<line number> PRINT

Example:

You type:

10 PRINT "THIS IS A TEST"
20 PRINT
30 PRINT "I AM STILL TESTING"
40 PRINT
50 PRINT "I AM NOW TIRED OF TESTING"
RUN

Sorcerer replies:

THIS IS A TEST

I AM STILL TESTING

I AM NOW TIRED OF TESTING

Your programs will be easier for you and for others to understand if you include remarks and explanations at key points. Do this with the REM statement:

< line number > REM < your remarks >

Example:

5 REM THIS PROGRAM SHOWS HOW
10 REM TO PRINT A BLANK LINE
15 PRINT "BELOW THIS LINE IS A BLANK LINE"
20 PRINT
25 PRINT "ABOVE THIS LINE IS A BLANK LINE"

The Sorcerer knows that a REM statement doesn't instruct it to do anything, but just contains your remarks and comments. Whenever it sees a REM statement it skips to the next line. When the program is run, the REM statements do not execute. However, the REM statements remain in the Sorcerer's memory and you can call them back with the LIST command. In direct mode, type the command LIST. (Don't forget to hit RETURN .) The Sorcerer prints your program back on the screen. More on how to do this in the next chapter.

Generally speaking, program in Standard BASIC with the SHIFT LOCK key depressed. Most BASIC program listings that you will find in various computing publications are written in all caps. The Sorcerer does not care whether you give it instructions in upper or lower case; it translates all instructions from lower to upper case. Remarks and literals are not affected (that is, they remain exactly as typed).


NOTE

When the Sorcerer is in the monitor, however, you must type in all instructions with capitals. You know that you are in the monitor when you get the > prompt.


Lower case letters are, of course, part of our printed language, and the Sorcerer acknowledges them when part of a PRINT statement (within quotes) or as a string INPUT (more on this in Chapter 9).

Example:

You type:

10 REM lower case demonstration
20 PRINT "I CAN PRINT CAPITALS."
30 PRINT "I can also print small letters."
RUN

Sorcerer replies:

I CAN PRINT CAPITALS.
I can also print small letters.


NOTE

You can write more than one statement on a line if you separate the statements by colons (:). As long as there are no more than 64 characters in the line all told, it can contain as many statements as you please. They all have the same line number and the Sorcerer executes them in order from left to right when it comes to the line. But notice that if one of the statements is a REM, any following statements on the same line are just considered part of the remark and do not execute.


Example:

10 PRINT "ONE":PRINT:PRINT "TWO":PRINT:PRINT "THREE"

This produces the same result as the following program:

10 PRINT "ONE"
20 PRINT
30 PRINT "TWO"
40 PRINT
50 PRINT "THREE"

Try entering and running each to verify this for yourself.


Table of Contents | Prev | Next

The Trailing Edge