SECTION 3

direct modeprogram mode
[continue/next section][MAIN/Introduction][table of contents]

3. Getting Started in Basic


TABLE OF CONTENTS

3.1 BASIC PROGRAMMING LANGUAGE

3.1.1 Direct Mode
3.1.2 Program Mode

3.2 USING THE KEYBOARD

3.2.0.1 C128 Keyboard Lay-Out
3.2.1 Keyboard Character Sets
3.2.2 Using the Command Keys
3.2.2.1 Return
3.2.2.2 Shift
3.2.2.3 Shift Lock
3.2.2.4 Moving the cursor
3.2.2.4.1 Using the four Arrow Cursor keys
3.2.2.4.2 Using the CRSR keys
3.2.2.5 INST/DEL
3.2.2.5.1 Insterting characters
3.2.2.5.2 Deleting characters
3.2.2.5.3 Using INSerT and DELete together
3.2.2.6 Control
3.2.2.7 Run/Stop
3.2.2.8 Restore
3.2.2.9 CLR/Home
3.2.2.10 Commodore key
3.2.3 Function Keys
3.2.4 Displaying Graphic Characters
3.2.5 Rules for Typing BASIC Language Programs

3.3 GETTING STARTED - THE PRINT COMMAND

3.3.1 Printing Numbers
3.3.2 Using the Question Mark to Abbreviate the PRINT Command
3.3.3 Printing Text
3.3.4 Printing in Different Colors
Table 3-1. Available Colors in 40- and 80-Column Formats.
3.3.5 Using the Cursor Keys Inside Quotes with the PRINT Command

3.4 BEGINNING TO PROGRAM

3.4.1 What a Program Is
3.4.2 Line Numbers
3.4.3 Viewing your Program - The LIST Command
3.4.4 A Simple Loop - The GOTO Command
3.4.5 Clearing the Computer's Memory - The NEW Command
3.4.6 Using Color in a Program

3.5 EDITING YOUR PROGRAM

3.5.1 Erasing a Line from a Program
3.5.2 Duplicating a Line
3.5.3 Replacing a Line
3.5.4 Changing a Line

3.6 MATHEMATICAL OPERATIONS

3.6.1 Addition and Subtraction
3.6.2 Multiplication and Division
3.6.3 Exponentiation
3.6.4 Order of Operations
3.6.5 Using Parentheses to Define the Order of Operations

3.7 CONSTANTS, VARIABLES AND STRINGS

3.7.1 Constants
3.7.2 Variables
3.7.3 Strings

3.8 SAMPLE PROGRAM

3.9 STORING AND REUSING YOUR PROGRAMS

3.9.0.1 Disk Handling Parameters
3.9.1 Formatting a Disk - The HEADER Command
3.9.2 SAVEing on Disk
3.9.3 SAVEing on Cassette
3.9.4 LOADing from Disk
3.9.5 LOADing from Cassette Tape
3.9.6 Other Disk-Related Commands
3.9.6.1 Verifying a Program
3.9.6.2 Displaying Your Disk Directory

3.1 BASIC PROGRAMMING LANGUAGE

The BASIC programming language is a special language that lets you communicate with your Commodore 128. Using BASIC is one means by which you instruct your computer what to do.

BASIC has its own vocabulary (made up of commands, statements and functions) and its own rules of structure (called syntax). You can use the BASIC vocabulary and syntax to create a set of instructions called a program, which your computer can then perform or "run".

Using BASIC, you can communicate with your Commodore 128 in two ways: within a program, or directly (outside a program).

3.1.1 Direct Mode

Your Commodore 128 is ready to accept BASIC commands in direct mode as soon as you turn on the computer. In the direct mode, you type commands on the keyboard and enter them into the computer by pressing the {return} key. Most BASIC commands in your Commodore 128 can be used in direct mode as well as in a program.

3.1.2 Program Mode

In program mode you enter a set of instructions that perform a specific task. Each instruction is contained in a sequential program line. A statement in a program may be as long as 160 characters; this is equivalent to four full screen lines in 40-column format, and two full screen lines in 80-column format.

Once you have typed a program, you can use it immediately by typing the RUN command and pressing the {return} key. You can also store the program on disk or tape by using the DSAVE (or SAVE) command. Then you recall it from the disk or tape by using the DLOAD (or LOAD) command. This command copies the program from the disk or tape and places that program in the Commodore 128's memory. You can then use or "execute" the program again by entering the RUN command. All these commands are explained later in this section. Most of the time you will be using you computer with programs, including programs you yourself write, and commercially available software packages. The only time you operate in direct mode is when you are manipulating or editing your programs with commands such as LIST, LOAD, SAVE and RUN. As a rule, the difference between direct mode and operation within a program is that direct mode commands have no line numbers.

3.2 USING THE KEYBOARD

Shown below is the keyboard of the Commodore 128 Personal Computer.

3.2.0.1 C128 Keyboard Lay-Out

[ASCII IMG]

Note: Outlined key areas can be used in C64 Mode.

Using BASIC is essentially the same in both C64 and C128 modes. Most of the keys, and many of the commands you will learn, can be used to program BASIC in either mode. The keys that are shaded in the diagram above can be used in C64 mode. In C128 mode you can use all of the keys on the keyboard.

3.2.1 Keyboard Character Sets

The Commodore 128 keyboard offers two different sets of characters:

  • Uppercase letters and graphic characters
  • Upper- and lowercase letters

In 80-column format, both character sets are available simultaneously. This gives you a total of 512 different characters that you can display on the screen. In 40-column format you can use only one character set at a time.

When you turn on the Commodore 128 in 40-column format, the keyboard normally is using the uppercase/graphic character set. This means that everything you type is in capital letters. To switch back and forth between the two character sets, press the {shift} key and the {C=} key (the COMMODORE key) at the same time. To practice using the two character sets turn on your computer and press several letters or graphic characters. Then press the {shift} key and the {C=}(Commodore) key. Notice how the screen changes to upper- and lowercase characters. Press {shift} and {C=} again to return to the uppercase and graphic character set.

3.2.2 Using the Command Keys

COMMAND keys are keys that send messages to the computer. Some command keys (such as {return}) are used by themselves. Other keys such as {shift}, {ctrl}, {C=} and {restore}) are used with other keys. The use of each of the command keys is explained below. The keys used in C128 mode are described in Section 5.

3.2.2.1 Return

When you press the {return} key, what you have typed is sent to the Commodore 128 computer's memory. Pressing the {return} key also moves the cursor (the small flashing rectangle that marks where the next character you type will appear) to the next line.

At times you may misspell a command or type in something the computer does not understand. Then, when you press the {return} key, you probably will get a message like SYNTAX ERROR on the screen. This is called an "Error Message". Appendix A lists the error messages and tells how to correct the errors.

3.2.2.2 Shift

There are two {shift} keys on the bottom row of the keyboard. One key is the one on the left and the other on the right, just as on a standard typewriter keyboard.

The {shift} key can be used in three ways:

  1. With the upper/lowercase character set, the {shift} key is used like the shift key on a regular typewriter. When the {shift} key is hold down, it lets you print capital letters or the top characters on double-character keys.
  2. The {shift} key can be used with some of the other command keys to perform special functions.
  3. When the keyboard is set for the uppercase/graphic character set, you can use the {shift} key to print the graphic symbols or characters that appear on the right of the front face of certain keys. See paragraph 3.2.4 at the end of this section for more details.
3.2.2.3 Shift Lock

When you press this key down, it locks into place. Then, whatever you type will either be a capital letter, or the top character of a double-character key. To release the lock, press down on the {shift lock} key again.

3.2.2.4 Moving the cursor

In C128 mode, you can move the cursor by using either the four arrow keys located just above the top right of the main keyboard, or the two keys labeled {crsr}, at the right of the bottom row of the main keyboard.

3.2.2.4.1 Using the four Arrow Cursor keys

In C128 mode, the cursor can be moved in any direction simply by using the arrow key in the top row that points in the direction you want to move the cursor. (These keys cannot be used in C64 mode).

3.2.2.4.2 Using the CRSR keys

In both C128 and C64 mode, you can use the two keys on the right side of the bottom row of the main keyboard to move the cursor:

  • Pressing the {crsr up/down} key alone moves the cursor down.
  • Pressing the {crsr up/down} and {shift} keys together moves the cursor up.
  • Pressing the {crsr left/right} key alone moves the cursor right.
  • Pressing the {crsr left/right} and {shift} keys together moves the cursor left.

You don't have to keep tapping a cursor key to move more than one space. Just hold the key down and the cursor continues to move, release it when it reaches the position you want.

Notice that when the cursor reaches the right side of the screen, it "wraps", or starts again at the beginning of the next row. When moving left, the cursor will move along until it reaches the edge of the screen, then it will jump up to the end of the preceding line.

You should try to become very familiar with the cursor keys, because moving the cursor makes your programming much easier. With a little practice you will find that you can move the cursor almost without thinking about it.

3.2.2.5 Inst/Del

This is a dual purpose key. INST stands for INSerT, and DEL for DELete.

3.2.2.5.1 Inserting Characters

You must use the {shift} key with the {inst/del} key when you want to insert characters in a line. Suppose you left some characters out of a line like this:

WHILE WERE OUT_

To insert the missing characters, first use the cursor keys to move the cursor back to the error. like this:

WHILE_WERE OUT

Then, while you hold down the {shift} key, press the {inst/del} key until you have enough space to add the missing characters:

WHILE_    WERE OUT

Notice that {inst} doesn't move the cursor; it just adds space between the cursor and the character to its right. To make the correction type the missing {space}, {y}, {o} and {u} like this:

WHILE YOU_WERE OUT
3.2.2.5.2 Deleting characters

When you press the {del} key, the cursor move one space to the left and erases the character that is there and moves any characters to the right of the cursor one position to the left. This means that when you want to delete something, you move the cursor just to the right of the character you want to DELete. Suppose you have made a mistake in typing, like this:

PRINT "ERROER"_

You wanted to type the word ERROR, not ERROER. To delete the incorrect E that precedes the final R, position the cursor on the final R. When you press the {del} key, the R automatically moves over one space to the left. You now have the correct wording like this:

PRINT "ERROR"
3.2.2.5.3 Using INSerT and DELete together

You can use the INSerT and DELete functions together to fix incorrect characters. First, move the cursor one space after the incorrect characters and press the {inst/del} key by itself to delete the incorrect characters.

Next, press the {shift} key and the {inst/del} key together to add any necessary space.

3.2.2.6 Control

The {ctrl} key is used with other keys to do special task called control functions. To perform a control function, hold down the {ctrl} key while you press some other key. A full list of control sequences is given in ASCII, CHR$ and ESC codes. Control functions are often used in prepackaged software such as a word processing system.

One control function that is used often is setting the character and cursor color. To select a color, hold down the {ctrl} key while you press a number key ({1} throught {8}), on the top row of the main keyboard. There are eight more colors available to you; these can be selected with the {C=} key, as explained later.

3.2.2.7 Run/Stop

This is a dual function key. Under certain conditions you can use the RUN function of this key by pressing the {shift} and {run/stop} key together. It is also possible to use the STOP function of the key to halt a program or a printout by pressing this key while the program is running. However, in most prepackaged programs, the STOP function of the {run/stop} key is intentionally disabled (made unusable). This is done to prevent the user from trying to stop a program that is running before it reaches its normal end point. If the user were able to stop the program, valuable data could be lost.

3.2.2.8 Restore

The {restore} key is used with the {run/stop} key to return the computer to its standard condition. Most prepackaged programs disable the {restore} key for the same reason the disable the STOP function of the {run/stop} key: to prevent losing valuable data.

3.2.2.9 CLR/Home

CLR stands for CLeaR. HOME refers to the upper left corner of the screen, which is called the HOME position. If you press this by itself the cursor returns to the HOME position. When you use the {shift} key with the {clr/home} key, the screen CLeaRs and the cursor returns to the HOME position.

3.2.2.10 Commodore key

The {C=} key (known as the {commodore} key) has a number of functions, including the following ones:

  1. When used with the {shift} key, the {C=} key lets you switch between uppercase/graphics mode and upper-/lowercase text modes.
  2. When you're in either mode, the {C=} key acts as a shift to let you type graphics symbols pictured on the LEFT front of each key. Just hold down the {C=} and press teh graphic key you want.
  3. When you want to change the color you are typing in to one of the 8 colors listed on the BOTTOM row of the face of the color keys (number keys {1} through {8} on the main keyboard): press {C=} and the color you want.
  4. When you want to slow down a scrolling program display, hold down the {C=} key. The display scrolling speed slows down considerably. When you release this key, the screen scrolling resumes at normal speed.
  5. If you hold down the {C=} key while turning on the computer, you immediately access C64 mode.

3.2.3 Function Keys

The four keys above the numeric keypad (marked F1, F3, F5 and F7 on the top and F2, F4, F6 and F8 on the front) are called function keys. In both C128 and C64 modes, you can program the function keys. (See the KEY command descriptions in Section 5, paragraph 5.9.2, of Chapter II and in Section 17, paragraph 17.53, Chapter V, BASIC 7.0 Encyclopaedia.) These keys are often used by prepackaged software to allow you to perform a task with a single keystroke.

3.2.4 Displaying Graphic Characters

To display the graphic symbol on the right front of a key, hold down the {shift} key while you press the key that has the graphic character you want to print. You can display the right side graphic character only when the keyboard is in the uppercase/graphics character set (one normal character set usually available at power-up).

To display the graphic character on the left front face of a key, hold down the {C=} key while you press the key that has the graphic character you want. You can display the left graphic character while the keyboard is in either character set.

3.2.5 Rules for Typing BASIC Language Programs

You can type and use BASIC language programs even without knowing BASIC. You must type carefully, however, because a typing error may cause the computer to reject your information. The following guidelines will help minimize errors when typing or copying a program listing.

  1. Spacing between words is not critical; e.g. typing FORT=1TO10 is the same as typing FORT=1 TO 10. However, a BASIC keyword itself must not be broken up by spaces (see Section 20, paragraph 20.1, of the BASIC 7.0 Encyclopaedia in Chapter V for a list of BASIC keywords).
  2. Any characters can be typed inside quotation marks. Some characters have special functions when placed inside quotation marks, These functions are explained later in this guide.
  3. Be careful with punctuation marks. Commas, colons and semicolons also have special properties, explained later in this guide.
  4. Always press the {return} key after completing a numbered line.
  5. Never type more than 160 characters in a program line. Remember, this is the same as four full screen lines in 40-column format, or two full screen lines in 80-column format. See Section 8 for more details on 40- and 80-columns formats.
  6. Distinguish clearly between the letter {l} and the numeral {1} and between the capital letter {O} and the numeral {0} (zero).
  7. The computer ignores anything following the letters REM on a program line. REM stands for REMark. You can use the REM statement to put comments in you program that tell anyone listing the program what is happening at a specific point.

Follow these guidelines when you type the examples and programs shown in this section.

3.3 GETTING STARTED - THE PRINT COMMAND

The PRINT command tells the computer to display information on the screen. You can print both numbers and text (letters), but there are special rules for each case, described in the following paragraphs.

3.3.1 Printing Numbers

To print numbers, use the PRINT command followed by the number(s) you want to print. Try typing this on you Commodore 128:

PRINT 5

Then press the {return} key. Notice the number 5 is now displayed on the screen.

Now type this and press {return}:

PRINT 5,6

In this PRINT command, the comma tells the Commodore 128 that you want to print more than one number. When the computer finds commas in a string of numbers in a PRINT statement, the output is displayed to the nearest tenth column.

If you don't want all the extra spaces, use a semicolon (;) in your PRINT statement instead of a comma. The semicolon tells the computer to print the numbers next to each other. A number when printed has either a space or a minus sign preceding it and a skip character after it. Type these examples and see what happens:

PRINT 5;6 {return}
PRINT 100;-200;300;-400;500 {return}

3.3.2 Using the Question Mark to Abbreviate the PRINT Command

You can use a quotation mark (?) as an abbreviation for the PRINT command. Many of the examples in this section use the ? symbol in place of the word PRINT. In fact, most of the BASIC commands can be abbreviated. The abbreviations for BASIC commands can be found in Appendix K of this Guide.

3.3.3 Printing Text

Now that you know how to print numbers, it's time to learn how to print text. It's actually very simple. Any words or characters you want to display are typed on the screen, with a quote symbol at each end of the string of characters. String is the BASIC name for any set of characters surrounded by quotes. The quote character is obtained by pressing "SHIFT" and the number {2} key on top of the main keyboard (not the {2} in the numeric keypad). Try these examples:

? "COMMODORE 128" {return}
? "4*5" {return}

Notice that when you press {return}, the computer displays the character within the quotes on the screen. Also note that the second example did not calculate 4*5 since it was treated as a string and not a mathematical calculation. If you want to calculate the result of 4*5, use the following command:

? 4*5 {return}

You can PRINT any string you want by using the PRINT command and surrounding the printed characters with quotes. You can combine text and calculations in a single PRINT command like this:

? "4*5 = "4*5 {return}

See how the computer PRINTs the characters in quotes, makes the calculation and PRINTs the result. It doesn't matter whether the text or calculation comes first. In fact, you can use both several times in one PRINT command. Type the following statement:

? 4*(2+3)" is the same as "4*5 {return}

Notice that even spaces inside the quotation marks are printed on the screen. Type:

? "   OVER HERE" {return}

3.3.4 Printing in Different Colors

The Commodore 128 is capable of displaying 16 different colors on the screen. You can change colors easily. All you do is hold down the {ctrl} key and press a numbered key between one and eight on the top row of the main keyboard. Notice that the cursor changes color according to the numbered key you pressed. All the succeeding characters are displayed in the color you selected. Hold down the {C=} key and press a numbered key between one and eight, and eight additional colors are displayed on the screen.

Table 3-1 list the colors available in C128 mode, for both 40-column and 80-column screen formats. The tables also show the key sequence (CONTROL key plus number key, or {C=} key plus number key) used to specify a given color.

Table 3-1. Available Colors in 40- and 80-Column Formats.

CONTROL + Color           {C=} +  Color

  1       Black           1      Orange
  2       White           2      Brown
  3       Red             3      Light Red
  4       Cyan            4      Dark Grey
  5       Purple          5      Middle Grey
  6       Green           6      Light Green
  7       Blue            7      Light Blue
  8       Yellow          8      Light Grey
Colors in 40-Column Format

CONTROL + Color           {C=} +  Color

  1       Black           1       Dark Purple
  2       White           2       Brown
  3       Dark Red        3       Light Red
  4       Light Cyan      4       Dark Cyan
  5       Light Purple    5       Middle Grey
  6       Dark Green      6       Light Green
  7       Dark Blue       7       Light Blue
  8       Light Yellow    8       Light Grey
Colors in 80-Column Format

3.3.5 Using the Cursor Keys Inside Quotes with the PRINT Command

When you type the cursor keys inside quotation marks, graphic characters are shown on the screen to represent the keys. These characters will NOT be printed on the screen when you press {return}. Try typing a question mark ({?}), open quotes ({shift}ed {2} key); then press either of the down cursor keys 10 times, enter the words "DOWN HERE", and close the quotes. The line should look like this:

?"QQQQQQQQQQDOWN HERE"

Now press {return}. The Commodore 128 prints 10 blank lines, and on the eleventh line, it prints "DOWN HERE". As this example shows, you can tell the computer to print anywhere on your screen by using the cursor control keys inside quotation marks.

[continue with next part]

[top of document]

page URL: www.bigfoot.com/~c128page/c128sg/sect-03a.htm
contact: c128page@bigfoot.com