Programming in Tiny Basic

1 -- ELF Talks Back

<Prev  Next>

In this chapter you will learn how to understand what your ELF II computer is telling you. Be sure it is turned on, and TINY BASIC is loaded and running (review Chapter 0 if you do not remember how to do this). Type an ESCape or two to get the cursor on a new line with only a colon.

In Chapter 0 I told you not to type the RETURN key yet. Well, now you are ready: try it. You got a new line with its prompt (we call the colon on the left end of the line a prompt because it prompts you to type something in). This does not appear particularly exciting. Type your name, followed by a RETURN. TINY BASIC answers!

You should see, on a new line under your name, the line

and another colon prompt line under that. What does it mean? If you were to look 397 up in the list of error messages in the TINY BASIC user's manual, it would say, "Misspelled keyword." This does not mean you misspelled your name. Since your computer does not know how to spell your name, it could not know whether you mispelled it. In fact, it does not even know what to do with your name. Type some other person's name. You see, you get the same answer. Type HELLO. Same result. The computer does not understand these words, and the response, "!397" is to tell you that the computer does not know what you mean. You may get, from time to time, some message I have not yet told about. I will probably get around to discussing the message a little later, so you might want to write down exactly what you typed and what the message was -- it does not stay on the TV screen very long after you start typing again. Most often, of course, when you get a message you we're not expecting, it is because you did not type exactly what you thought you did.

There are a few words the computer does understand. Type PRINT and a return key. Every time we want to say something to the computer, we must end the message with a Carriage RETURN. TINY BASIC does not look at what you type until you hit the RETURN key. This time there is no error message, only a new line with a prompt. But nothing in particular wAs PRINTed by the computer. This is because there was nothing else on the line that told It to print. Type

You see, if you tell it to print a number, it prints that number on the next line. Try some other numbers: 0, 1, 2,100, 57,12345, 87654. On this last number, you will notice that TINY did not print the same number you typed. The reason is that the number is too big. In fact, any number larger than 32767 is too big. Tell TINY to PRINT 32767, then tell it to PRINT 32768. You see, it puts a minus sign in front of 32768. If you tell TINY to PRINT 32769 it will respond with -32767. As you continue to put in larger numbers, it works backwards, but with a minus sign. See if you can find a number to type in that TINY BASIC will respond with -1.

Notice that if you type in PRINT 65536 the computer responds with 0. As it turns out, because this BASIC is "tiny", it thinks that 65536 is the same number as zero. Numbers greater than 65536 (but less than 98304) are the same as numbers greater than zero. Numbers less than 65536 (but greater than 32767) are the same as numbers less than zero. The problem has a valid mathematical interprelation, but for now you must assume that you cannot use numbers greater than 32767.

As you noticed, however, TINY BASIC can print negative numbers. Tell TINY to PRINT -2 or some, other negative number. Be sure to use the hyphen key (the keytop shows both the hyphen and the equal "=" symbols) and not the underscore key. If you make a typing error, you can ESCape from the line and type it over. Try telling the computer to PRINT a few other negative numbers. What does it show for -32769? Remember, I said that we have to limit our numbers to less than 32768, because strange things happen to larger numbers.

Before we go any farther in working with numbers, I should say some more about correcting mistakes. You will make typing errors. We all make typing errors. I make them all the time. If it is a gross error, we use the ESC key to cancel the line. But if a finger slips on the last digit of a long line, it is a shame to have to type the whole line back in. So we have what is called a backspace. It does not really back up the cursor (remember, this is a TINY BASIC). but it will erase the previous character inside the computer. We use the underscore key for this. The underscore key is usually on the right-hand end of the keyboard (dont confuse it with the hyphen!). If you cannot find the underscore key, use the DEL key instead. Type

PRINT 123_45
Notice that the computer responds with 1245. The "3" was deleted. What do you think will happen if you type two underscores instead of one? Try it. You see, two backspaces back up over two characters. You can back up as far as you like this way. Notice that if you make a mistake while typing a correction, you can back up again. And again, if necessary. TINY does not mind. It is there to obey you. Suppose you tell TINY to back up when there is nothing on the line left to back up over; what would you expect? Try it. Type PRINT then six underscores. The new line prompt is to tell you that you backed up over the colon. Perhaps you might have lost count while backing up, and TINY wants to give you a clean line to try again on.

Well, computers should compute, not just repeat back everything you type in. Let's see if your ELF knows how to add. Type in

Notice that the "+" key also has a semicolon (";") on it. You have to hold the SHIFT key down while typing "+". The answer is (what did you expect?) 5. Try 2-3 and maybe some other addition and subtraction. Notice that we use the same hyphen for subtraction as for negative numbers. For multiplication we use a times symbol which shows up on the screen as a little "x", but on the keyboard it is a star or asterisk "*". Be sure to hold the shift key down for this one too, or it will be a colon instead. Ask your computer to PRINT 2x3 (you type 2*3). The computer does not have a special symbol for divide, so we use the slash "/". Tell the computer to PRINT 24/6 (that is, 24 divided by 6). Since 24 is exactly divisible by 6, we expect (and get) an answer of 4. But suppose we tell the computer to divide two numbers that do not come out exact; what then? Try 8/3. In grammar school you learned that 8/3 is 2.33333. But TINY BASIC is not able to deal with (decimal) fractions so it just throws them away. It told you that 8/3 is 2 and threw away the .33333.

You can tell TINY to do several operations at once, if you like. Try 1+2+3+4+5 and 89*45/15-6. Now tell it to PRINT 3*4-6/2. What should you expect for the answer? In school you learned that you always do the multiplication and division before addition and subtraction, so TINY does the same thing. It multiplied 3 times 4 and got 12, then divided 6 by 2 and got 3, then finally subtracted the three from the 12 to print 9. Suppose you really wanted it to subtract before dividing? Then you must use parentheses to group the operations in the order you desire. For example, to multiply, then subtract, then divide, you type PRINT (3*4-6)/2 and to do the subtraction first you type PRINT 3*(4-6)/2. You see how each one of these gives you a different answer. Any computation inside parentheses is done first. If you like you can have another set of parentheses inside the parentheses. And another. As long as your request does not get more than maybe three lines long on the screen, TINY will accept as many operations and parentheses as you like. Try

PRINT ((((( ((((( ((((( 15 ))))) ))))) )))))
By now you probably have let some typing errors slip through. And TINY (usually) responded with some message like "!73" or "!395" or "!556" or "!560". Each of these numbers identifies a kind of error that TINY knows how to distinguish. For example, error number 73 says that TINY does not know quite what is wrong with your PRINT command. It may actually print some number (probably the wrong one) before saying so. Error number 556 tells you that TINY was expecting to find a number, but but you had typed something else. Error number 560 says you forgot a right parenthesis. If you forget a left parenthesis in a PRINT command you will probably get error number 73. Error number 395 tells you that you typed something between the colon prompt and the command to PRINT.

You now know to tell TINY BASIC to print a number or the result of some computation on numbers. But suppose you want to print

TINY BASIC can do this also. You tell it to do so by commanding:
Notice the quotation marks around the message. Any time you want TINY to print anything other than numbers (including the results of computations), you need quotation marks around what is printed. Actually what you really want is for TINY to show the message, then do the computation, and print the result. Type in
Notice the space before the second quotation mark and the semicolon after it. The semicolon tells TINY that there is more to be printed on the same line. So TINY prints the message you put in quotation marks, then adds 2+3 and prints the result on the same line. Anything inside quotation marks is printed exactly the way you type it, but if there are no quotation marks, TINY BASIC will perform the specified computations and print the result. You could type
PRINT "2+3=";2+3
and TINY would respond with
Notice that TINY BASIC prints exactly what you tell it to print. even if what it prints is not true:
PRINT "2+3=7"
even though that is not true. Just because something has been printed by a computer does not make it true. The computer only does obediently what people tell it to do. If your electric bill says you owe $5000 for last month's electricity to your home, it is not the computer's fault. Some person told the computer the wrong thing. When your computer prints the wrong ansvmrs, you can be sure it is because you told it to do so. Most of the time you and I and everyone else in this business spends on computers is and will be trying to figure out what it is we told the computer to do that we did not really want to tell it to do. And part of the fun is finding the problem and fixing it. It is kind of like a puzzle, only often much harder.

Now you know how to tell the computer to print messages on the screen, and to compute formulas and print the results. In the next chapter we will see how to tell the computer to do these things later.

Back to Top
Continue with: 2 -- Running Programs