Classic Computer Magazine Archive COMPUTE! ISSUE 51 / AUGUST 1984 / PAGE 28

THE BEGINNER'S PAGE

Robert Alanso, Assistant Editor

Printing And Asking

In most versions of BASIC there are usually many ways to accomplish a task. For example, to clear the screen Atari users have the option of printing either a control character or CHR$(125). On any Commodore computer, the user has a similar option, but the CHR$ would have to be (147). Beginners usually decide that they prefer PRINTing the control codes between quotes. The reason for this choice is that the CHR$ command and the associated ASC command are often misunderstood.

The CHR$ function is really a very easy function to use and to understand. Let's say that you wanted to print the letter A on your computer's screen. You could simply type PRINT "A" and hit the RETURN key. However, you could also PRINT CHR$(65). The result would be the same. Although it is always nice to know that you have different ways to do things, you probably are better off just printing the A on the screen.

The real value of the CHR$ command is that it allows you to print special control codes such as cursor movement and screen clearing commands. It is far more confusing, for example, to have embedded commands (such as the reverse-video heart in a Commodore listing) than to have the same commands as CHR$. There are usually CHR$ codes that allow you to change the color of the cursor and even ones that let you ring the internal bell of the computer. On the Apple and Adam home computers you can easily have the bell ring by just PRINTing a few CHR$(7)'s.

ASC The Computer

The reverse of the CHR$ command is ASC. ASC will let you find out what decimal number represents a given character. If, for example, you want to know the number that you would have to include with a CHR$ to PRINT a comma on the screen, you could just ask the computer via ASC. The correct syntax for the command is PRINT ASC(","). You would get 44 as the answer. If you then PRINTed CHR$(44), the computer would print out a comma. An easy way to remember what each function does is to remember that ASC is used for asking the computer for the correct number and the other is for giving the computer the right number.

Secret Coding

Program 1 demonstrates the characters immediately available on your home computer. When you run the program, keep in mind that it will print out all of the available characters as well as control codes. The control codes will affect the appearance of the output to the screen. For example, once the loop reaches 125 in the Atari, the screen will be cleared.

One reason you might want to use CHR$ within your programs is that you might want to conceal words when someone uses one of your programs. In a game of Hangman you might want all the words placed in DATA statements in their ASCII numerical format. This will prevent the user from cheating. It is quite confusing for a snooping user to come across a long list of seemingly random and meaningless numbers. Only you will know!

Program 2 is an example of how you might encode a sentence so that only you know what it means. If you take a close look at lines 50–70, you'll notice that it takes a lot more space to store a sentence in this method than it would to

Table 1:

Atari Special CHR$ Codes

CHR$Atari Effect
27ESC
28Cursor Up
29Cursor Down
30 Cursor Left
31Cursor Right
125Clear Screen
155RETURN
253Buzzer

Table 2:

Commodore Special CHR$ Codes

CHR$Commodore Effect
13RETURN
14Switch to Lowercase
17Cursor Down
ISCursor Home
29Cursor Right
142Switch to Uppercase
145Cursor Up
147Clear Screen
157Cursor Left

simply type in the letters. Although it does take up extra memory, you will soon appreciate the potential that CHR$ codes have for creating fun and educational quiz programs.

As you can see, the program is straightforward. Line 10 starts everything by initializing a FOR-NEXT loop. The number 29 corresponds to the number of characters, including spaces and punctuation, that the sentence has. There are exactly 29 numbers in the DATA statements. The READ A command in line 20 gets one number in from the DATA statements for each pass through the FOR-NEXT loop. Line 30 is used for printing the characters on your screen. The CHR$ function is used here for converting the numbers to their appropriate letter representation. A semicolon is placed after the command PRINT CHR$(A) so that the characters are printed next to each other instead of down the left-hand side of the screen. The semicolon eliminates the carriage return that is executed at the end of each PRINT operation.

Line 35 is just a delay loop, and line 40 sends the computer back to line 10 to go through the next number in the loop. If you wanted to use this routine in one of your programs, you could easily make it a subroutine. Just add a line 45 with the instruction RETURN and you can then access the routine by GOSUBing to it from your main program. To modify the message length, just increase or decrease the 29 in line 10. The message can be changed by just typing the right numbers into the DATA statements. To find out which numbers to place in the DATA statements just type PRINT ASC("X"), where the X stands for the letter that you need to know about. If you typed PRINT ASC("X"), for example, you would get 88 as the answer.

There are usually tables of the letters and control codes that each computer can print in each computer's user's manual. You can use these tables to help you find the letters and symbols you need for your program. The following tables should get you started on the Atari and Commodore computers.

Program 1 : chr$ Display

10 FOR X = 0 TO 255
20 PRINT CHR$(X);
30 FOR DE = 1 TO 100 : NEXT DE
40 NEXT X

Program 2: Secret Message

10 FOR X = l TO 29 : REM BEGIN LOOP
20 READ A : REM FETCH FIRST NUMBER
30 PRINT CHR$(A); : REM PRINT CORRESPONDING CHARACTER
35 FOR D=l TO 50 : NEXT D : REM DELAY LOOP
40 NEXT X
50 DATA 84, 72, 69, 32, 67, 72, 82, 36, 32
60 DATA 70, 85, 78, 67, 84, 73, 79, 78, 32
70 DATA 67, 65, 78, 32, 66, 69, 32, 70, 85, 78, 33