Very basic BASIC – simple tips for entering strings with INPUT (Commodore 64 BASIC)

When using Commodore 64 BASIC V2, you more than often realize that even performing very simple tasks, like entering strings, may require some thinkering. Although this BASIC interpreter is good overall, you may just need to work out your own way of improving its behaviour on certain situations. I’m not talking about expanding this BASIC – I am just referring to some simple tricks that can make your life easier.

The INPUT instruction is quite flexible, but it may not suit all of your needs. For instance, try the following program – it’s a very minimal program. Enter the input string as shown below.

10 input a$
20 print a$

run
? hello, there!
?extra ignored
hello

ready.

 

As you can see, even such a simple program – supposed to print your input on the screen – may cause trouble.

The extra ignored error is due to the meaning of the comma character for the input statement. The comma is not seen as a punctuation character – so as a part of the input string – but as a separator of two strings. So, when using the comma, you are actually entering two strings on a single command line. But, on line 10, the program only reads one string, which is to be assigned to the variable A$. As a result, there is a surplus string on your input, A$ will be assigned to the first string and you will get an error.

On the other hand, this feature allows for quick entering of multiple strings on a single command line. Have a look at the following picture.

 

Back to our problem. We just want to input a string with punctuation signs. How can we do that? Well, you may try to type a double commas character before entering your input message. That will work, but you will enter into the “quote mode”. That’s not of much trouble, but problems will come up if you happen to need editing your typing.

 

Suppose we made a typo, and wrote HALLO instead of HELLO. Since we are in quote mode, if trying to use cursor control keys, the cursor will not move. Instead, the matching control characters will be printed. You can just type another double commas character, then do your editing – you have just escaped the quote mode. But, we can do something better.

Before entering the input string, if you type two double commas, and then you delete one, you will have a double comma at the beginning of the command line – but without being in quote mode. That just allows to use punctuation characters on the input string, and to edit it with ease.

However, having to type two double commas and delete one on each input entry is somewhat uncomfortable. But, we can automate that inside the program. All that is needed is the use of the keyboard buffer.

The keyboard buffer is used to store keypresses that cannot be processed at the current moment. For instance, type something like “hello” while a loop is running. You will see that the word “hello” will be promptly shown as soon as the loop is exited.

 

The keyboard buffer can hold up to ten characters. It starts from location 631 decimal. Location 198 tells the system how many characters the keyboard buffer holds. So, if we poke the keyboard buffer with the ASCII codes of a word, then we poke location 198 with the lenght of that word, it will be printed. The following picture shows a simple example in direct mode.

 

We can add something like that to our previous program, so that the INPUT statement will work the way we need:

 

You can download this short program here.

The lines:

5 poke 631,34:poke 632,34:poke 633,20
8 poke 198,3

have been added. Of course, those lines can be merged into one single line, but I coded two separate lines for better readability.

The ASCII codes 34 and 20 are for double commas and delete respectively.

As you can see, it is now possible to use punctuation and edit the input string easily at the same time. Please notice that the double commas need NOT to be closed.

However, this program does have a limit. If you happen to use the cursor up/down keys while entering the string, the extra ignored error may happen again. But, as long as you don’t escape the current command line, you will be safe.

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert