Action: Returns an integer in the range of 0 to 255, which is read from a memory location. The expression is a memory location which must be in the range of 0 to 65535. If it isn't then the BASIC error message ?ILLEGAL QUANTITY occurs.
EXAMPLES of PEEK Function:
1 2 3 4
10 PRINT PEEK(53280) AND 15 (Returns value of screen border color)
5 A%=PEEK(45)+PEEK(46)*256 (Returns address of BASIC variable table)
POKE
TYPE: Statement FORMAT: POKE ,
Action: The POKE statement is used to write a one-byte (8-bits) binary value into a given memory location or input/output register. The is an arithmetic expression which must equal a value in the range of 0 to 65535. The is an expression which can be reduced to an integer value of 0 to 255. If either value is out of its respective range, the BASIC error message ?ILLEGAL QUANTITY occurs.
The POKE statement and PEEK statement (which is a built-in function that looks at a memory location) are useful for data storage, controlling graphics displays or sound generation, loading assembly language sub- routines, and passing arguments and results to and from assembly language subroutines. In addition, Operating System parameters can be examined using PEEK statements or changed and manipulated using POKE statements.
EXAMPLES of POKE Statement:
1 2 3 4 5
POKE 1024, 1 (Puts an "A" at position 1 on the screen) POKE 2040, PTR (Updates Sprite #0 data pointer) 10 POKE RED,32 20 POKE 36879,8 2050 POKE A,B
POS
TYPE: Integer Function FORMAT: POS ()
Action: Tells you the current cursor position which, of course, is in the range of 0 (leftmost character) though position 79 on an 80-character logical screen line. Since the Commodore 64 has a 40-column screen, any position from 40 through 79 will refer to the second screen line. The dummy argument is ignored.
EXAMPLE of POS Function:
1
1000 IF POS(0)>38 THEN PRINT CHR$(13)
PRINT
TYPE: Statement FORMAT: PRINT [][]...
Action: The PRINT statement is normally used to write data items to the screen. However, the CMD statement may be used to re-direct that output to any other device in the system. The in the output list are expressions of any type. If no output-list is present, a blank line is printed. The position of each printed item is determined by the punctuation used to separate items in the output-list.
The punctuation characters that you can use are blanks, commas, or semicolons. The 80-character logical screen line is divided into 8 print zones of 10 spaces each. In the list of expressions, a comma causes the next value to be printed at the beginning of the next zone. A semicolon causes the next value to be printed immediately following the previous value. However, there are two exceptions to this rule:
1) Numeric items are followed by an added space. 2) Positive numbers have a space preceding them.
When you use blanks or no punctuation between string constants or variable names it has the same effect as a semicolon. However, blanks between a string and a numeric item or between two numeric items will stop output without printing the second item.
If a comma or a semicolon is at the end of the output-list, the next PRINT statement begins printing on the same line, and spaced accordingly. If no punctuation finishes the list, a carriage-return and a line feed are printed at the end of the data. The next PRINT statement will begin on the next line. If your output is directed to the screen and the data printed is longer than 40 columns, the output is continued on the next screen line.
There is no statement in BASIC with more variety than the PRINT statement. There are so many symbols, functions, and parameters associated with this statement that it might almost be considered as a language of its own within BASIC; a language specially designed for writing on the screen.
Actions: The PRINT# statement is used to write data items to a logical file. It must use the same number used to OPEN the file. Output goes to the device-number used in the OPEN statement. The expressions in the output list can be of any type. The punctuation characters between items are the same as with the PRINT statement and they can be used in the same ways. The effects of punctuation are different in two significant respects.
When PRINT# is used with tape files, the comma, instead of spacing by print zones, has the same effect as a semicolon. Therefore, whether blanks, commas, semicolons or no punctuation characters are used between data items, the effect on spacing is the same. The data items are written as a continuous stream of characters. Numeric items are followed by a space and, if positive, are preceded by a space.
If no punctuation finishes the list, a carriage return and a line feed are written at the end of the data. If a comma or semicolon terminates the output list, the carriage-return and line-feed are suppressed. Regardless of the punctuation, the next PRINT# statement begins output in the next available character position. The line-feed will act as a stop when using the INPUT# statement, leaving an empty variable when the next INPUT# is executed. The line-feed can be suppressed or compensated for as shown in the examples below.
The easiest way to write more than one variable to a file on tape or disk is to set a string variable to CHR$(13), and use that string in between all the other variables when writing the file.
10 OPEN 1,1,1,"TAPE FILE" 20 R$=CHR$(13) (By Changing the CHR$(13) to 30 PRINT#1,1;R$;2;R$;3;R$;4;R$;5 CHR$(44) you put a "," between 40 PRINT#1,6 each variable. CHR$(59) would 50 PRINT# 1,7 put a ";" between each variable.)
Action: The READ statement is used to fill variable names from constants in DATA statements. The data actually read must agree with the variable types specified or the BASIC error message ?SYNTAX ERROR will result.(*) Variables in the DATA input-list must be separated by commas.
A single READ statement can access one or more DATA statements, which will be accessed in order (see DATA), or several READ statements can access the same DATA statement. If more READ statements are executed than the number of elements in DATA statements(s) in the program, the BASIC error message ?OUT OF DATA is printed. If the number of variables specified is fewer than the number of elements in the DATA statement(s), subsequent READ statements will continue reading at the next data element. (See RESTORE.)
NOTE: The ?SYNTAX ERROR will appear with the line number from the DATA statement, NOT the READ statement. |
EXAMPLES of READ Statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
110 READ A,B,C$ 120 DATA 1,2,HELLO
100 FOR X=1 TO 10: READ A(X):NEXT 200 DATA 3.08, 5.19, 3.12, 3.98, 4.24 210 DATA 5.08, 5.55, 4.00, 3.16, 3.37
(Fills array items (line 1) in order of constants shown (line 5))
1 READ CITY$,STATE$,ZIP 5 DATA DENVER,COLORADO, 80211
REM
TYPE: Statement FORMAT: REM []
Action: The REM statement makes your programs more easily understood when LISTed. It's a reminder to yourself to tell you what you had in mind when you were writing each section of the program. For instance, you might want to remember what a variable is used for, or some other useful information. The REMark can be any text, word, or character including the colon (:) or BASIC keywords.
The REM statement and anything following it on the same line-number are ignored by BASIC, but REMarks are printed exactly as entered when the program is listed. A REM statement can be referred to by a GOTO or GOSUB statement, and the execution of the program will continue with the next higher program line having executable statements.
EXAMPLES of REM Statement:
1 2 3 4
10 REM CALCULATE AVERAGE VELOCITY 20 FOR X= 1 TO 20 :REM LOOP FOR TWENTY VALUES 30 SUM=SUM + VEL(X): NEXT 40 AVG=SUM/20
RESTORE
TYPE: Statement FORMAT: RESTORE
Action: BASIC maintains an internal pointer to the next DATA constant to be READ. This pointer can be reset to the first DATA constant in a program using the RESTORE statement. The RESTORE statement can be used anywhere in the program to begin re-READing DATA.
100 FOR X=1 TO 10: READ A(X): NEXT 200 RESTORE 300 FOR Y=1 TO 10: READ B(Y): NEXT
4000 DATA 3.08, 5.19, 3.12, 3.98, 4.24 4100 DATA 5.08, 5.55, 4.00, 3.16, 3.37
(Fills the two arrays with identical data)
10 DATA 1,2,3,4 20 DATA 5,6,7,8 30 FOR L= 1 TO 8 40 READ A: PRINT A 50 NEXT 60 RESTORE 70 FOR L= 1 TO 8 80 READ A: PRINT A 90 NEXT
RETURN
TYPE: Statement FORMAT: RETURN
Action: The RETURN statement is used to exit from a subroutine called for by a GOSUB statement. RETURN restarts the rest of your program at the next executable statement following the GOSUB. If you are nesting subroutines, each GOSUB must be paired with at least one RETURN statement. A subroutine can contain any number of RETURN statements, but the first one encountered will exit the subroutine.
EXAMPLE of RETURN Statement:
1 2 3 4 5 6 7
10 PRINT"THIS IS THE PROGRAM" 20 GOSUB 1000 30 PRINT"PROGRAM CONTINUES" 40 GOSUB 1000 50 PRINT"MORE PROGRAM" 60 END 1000 PRINT"THIS IS THE GOSUB":RETURN
RIGHT$
TYPE: String Function FORMAT: RIGHT$ (,)
Action: The RIGHT$ function returns a sub-string taken from the right most end of the argument. The length of the sub-string is defined by the argument which can be any integer in the range of 0 to 255. If the value of the numeric expression is zero, then a null string ("") is returned. If the value you give in the argument is greater than the length of the then the entire string is returned.
EXAMPLE of RIGHT$ Function:
1 2 3 4 5 6
10 MSG$="COMMODORE COMPUTERS" 20 PRINT RIGHT$(MSG$,9) RUN
COMPUTERS
RND
TYPE: Floating-Point Function FORMAT: RND ()
Action: RND creates a floating-point random from 0.0 to 1.0. The computer generates a sequence of random numbers by performing calculations on a starting number, which in computer jargon is called a seed. The RND function is seeded on system power-up. The argument is a dummy, except for its sign (positive, zero, or negative).
If the argument is positive, the same "pseudorandom" sequence of numbers is returned, starting from a given seed value. Different number sequences will result from different seeds, but any sequence is repeatable by starting from the same seed number. Having a known sequence of "random" numbers is useful in testing programs.
If you choose a argument of zero, then RND generates a number directly from a free running hardware clock (the system "jiffy clock"). Negative arguments cause the RND function to be re-seeded with each function call.
EXAMPLES of RND Function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
220 PRINT INT(RND(0)*50) (Return random integers 0-49)
100 X=INT(RND(1)*1000)+1 (Random integers from 1-1000)
100 X=INT(RND(1)*150)+100 (Random numbers from 100-249)
100 X=RND(1)*(U-L)+L (Random numbers between upper (U) and lower (L) limits)
RUN
TYPE: Command FORMAT: RUN []
Action: The system command RUN is used to start the program currently in memory. The RUN command causes an implied CLR operation to be performed before starting the program. You can avoid the CLeaRing operation by using CONT or GOTO to restart a program instead of RUN. If a is specified, your program will start on that line. Otherwise, the RUN command starts at first line of the program. The RUN command can also be used within a program. If the you specify doesn't exist, the BASIC error message UNDEF'D STATEMENT occurs.
A RUNning program stops and BASIC returns to direct mode when an END or STOP statement is reached, when the last line of the program is finished, or when a BASIC error occurs during execution.
EXAMPLES of RUN Command:
1 2 3 4 5
RUN (Starts at first line of program)
RUN 500 (Starts at line-number 500) RUN X (Starts at line X, or UNDEF'D STATEMENT ERROR if there is no line X)
SAVE
TYPE: Command FORMAT: SAVE [""][,][,]
Action: The SAVE command is used to store the program that is currently in memory onto a tape or diskette file. The program being SAVED is only affected by the command while the SAVE is happening. The program remains in the current computer memory even after the SAVE operation is completed until you put something else there by using another command. The file type will be "prg" (program). If the is left out, then the C64 will automatically assume that you want the program saved on cassette, device number 1. If the is an , then the program is written onto disk. The SAVE statement can be used be used in your programs and execution will continue with the next statement after the SAVE is completed.
Programs on tape are automatically stored twice, so that your Commodore 64 can check for errors when LOADing the program back in. When saving programs to tape, the and secondary are optional. But following a SAVE with a program name in quotes ("") or by a string variable (---$) helps your Commodore 64 find each program more easily. If the file-name is left out it can NOT be LOADed by name later on.
A secondary address of I will tell the KERNAL to LOAD the tape at a later time, with the program currently in memory instead of the normal 2048 location. A secondary address of 2 will cause an end-of-tape marker to follow the program. A secondary address of 3 combines both functions.
When saving programs onto a disk, the must be present.
EXAMPLES of SAVE Command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
SAVE (Write to tape without a name)
SAVE"ALPHA",1 (Store on tape as file-name "alpha")
SAVE"ALPHA",1,2 (Store "alpha" with end-of-tape marker)
SAVE"FUN.DISK",8 (SAVES on disk (device 8 is the disk))
SAVE A$ (Store on tape with the name A$)
10 SAVE"HI" (SAVEs program and then move to next program line)
SAVE"ME",1,3 (Stores at same memory location and puts an end-of-tope marker on)
SGN
TYPE: Integer Function FORMAT: SGN ()
Action: SGN gives you an integer value depending upon the sign of the argument. If the argument is positive the result is 1, if zero the result is also 0, if negative the result is -1.
EXAMPLE of SGN Function:
1 2
90 ON SGN(DV)+2 GOTO 100, 200, 300 (jump to 100 if DV=negative, 200 if DV=0, 300 if DV=positive)
SIN
TYPE: Floating-Point Function FORMAT: SIN ()
Action: SIN gives you the sine of the argument, in radians. The value of COS(X) is equal to SIN(x+3.14159265/2).
EXAMPLE of SIN Function:
1 2
235 AA=SIN(1.5):PRINT AA .997494987
SPC
TYPE: String Function FORMAT: SPC ()
Action: The SPC function is used to control the formatting of data, as either an output to the screen or into a logical file. The number of SPaCes given by the argument are printed, starting at the first available position. For screen or tape files the value of the argument is in the range of 0 to 255 and for disk files up to 254. For printer files, an automatic carriage-return and line-feed will be performed by the printer if a SPaCe is printed in the last character position of a line. No SPaCes are printed on the following line.
EXAMPLE of SPC Function:
1 2 3 4 5 6
10 PRINT"RIGHT "; "HERE &"; 20 PRINT SPC(5)"OVER" SPC(14)"THERE" RUN
RIGHT HERE & OVER THERE
SQR
TYPE: Floating-Point Function FORMAT: SQR ()
Action: SQR gives you the value of the SQuare Root of the argument. The value of the argument must not be negative, or the BASIC error message ?ILLEGAL QUANTITY will happen.
Action: Returns a completion STATUS for the last input/output operation which was performed on an open file. The STATUS can be read from any peripheral device. The STATUS (or simply ST) keyword is a system defined variable-name into which the KERNAL puts the STATUS of I/O operations. A table of STATUS code values for tape, printer, disk and RS-232 file operations is shown below:
ST Bit Position ST Numeric Value Cassette Read Serial bus R/W Tape Verify + Load 0 1 time out write 1 2 time out read 2 4 short block short block 3 8 long block long block 4 16 unrecoverable read error any mismatch 5 32 checksum error checksum error 6 64 end of file EOI 7 -128 end of tape device not present end of tape
EXAMPLES of STATUS Function:
1 2 3 4 5 6 7 8 9 10 11
10 OPEN 1,4:OPEN 2,8,4,"MASTER FILE,SEQ,W" 20 GOSUB 100:REM CHECK STATUS 30 INPUT#2,A$,B,C 40 IF STATUS AND 64 THEN 80:REM HANDLE END-OF-FILE 50 GOSUB 100:REM CHECK STATUS 60 PRINT#1,A$,B;C 70 GOTO 20 80 CLOSE1:CLOSE2 90 GOSUB 100:END 100 IF ST > 0 THEN 9000:REM HANDLE FILE I/O ERROR 110 RETURN
STEP
TYPE: Statement FORMAT: [STEP ]
Action: The optional STEP keyword follows the expression in a FOR statement. It defines an increment value for the loop counter variable. Any value can be used as the STEP increment. Of course, a STEP value of zero will loop forever. If the STEP keyword is left out, the increment value will be + 1. When the NEXT statement in a FOR loop is reached, the STEP increment happens. Then the counter is tested against the end-value to see if the loop is finished. (See FOR statement for more information.)
NOTE: The STEP value can NOT be changed once it's in the loop.
EXAMPLES of STEP Statement:
1 2
25 FOR XX=2 TO 20 STEP 2 (Loop repeats 10 times) 35 FOR ZZ=0 TO -20 STEP -2 (Loop repeats 11 times)
STOP
TYPE: Statement FORMAT: STOP
Action: The STOP statement is used to halt execution of the current program and return to direct mode. Typing the key on the keyboard has the same effect as a STOP statement. The BASIC error message ?BREAK IN LINE nnnnn is displayed on the screen, followed by READY. The "nnnnn" is the line-number where the STOP occurs. Any open files remain open and all variables are preserved and can be examined. The program can be restarted by using CONT or GOTO statements.
EXAMPLES of STOP Statement:
1 2 3 4 5 6
10 INPUT#1,AA,BB,CC 20 IF AA=BB AND BB=CC THEN STOP 30 STOP (If the variable AA is -1 and BB is equal to CC then:) BREAK IN LINE 20 BREAK IN LINE 30 (For any other data values)
STR$
TYPE: String Function FORMAT: STR$ ()
Action: STR$ gives you the STRing representation of the numeric value of the argument. When the STR$ value is converted to each variable represented in the argument, any number shown is followed by a space and, if it's positive, it is also preceded by a space.
Action: This is the most common way to mix a BASIC program with a machine language program. The machine language program begins at the location given in the SYS statement. The system command SYS is used in either direct or program mode to transfer control of the microprocessor to an existing machine language program in memory. The memory-location given is by numeric expression and can be anywhere in memory, RAM or ROM.
When you're using the SYS statement you must end that section of machine language code with an RTS (ReTurn from Subroutine) instruction so that when the machine language program is finished, the BASIC execution will resume with the statement following the SYS command.
EXAMPLES of SYS Statement:
1 2 3 4
SYS 64738 (Jump to System Cold Start in ROM)
10 POKE 4400,96:SYS 4400 (Goes to machine code location 4400 and returns immediately)
TAB
TYPE: String Function FORMAT: TAB ()
Action: The TAB function moves the cursor to a relative SPC move position on the screen given by the argument, starting with the left-most position of the current line. The value of the argument can range from 0 to 255. The TAB function should only be used with the PRINT statement, since it has no effect if used with PRINT# to a logical file.
Action: Returns the tangent of the value of the expression in radians. If the TAN function overflows, the BASIC error message ?DIVISION BY ZERO is displayed.
EXAMPLE of TAN Function:
1 2 3 4
10 XX=.785398163: YY=TAN(XX):PRINT YY
1
TIME
TYPE: Numeric Function FORMAT: TI
Action: The TI function reads the interval Timer. This type of "clock" is called a "jiffy clock." The "jiffy clock" value is set at zero (initialized) when you power-up the system. This 1/60 second interval timer is turned off during tape I/O.
EXAMPLE of TI Function:
1
10 PRINT TI/60 "SECONDS SINCE POWER UP"
TIME$
TYPE: String Function FORMAT: TI$
Action: The TI$ timer looks and works like a real clock as long as your system is powered-on. The hardware interval timer (or jiffy clock) is read and used to update the value of TI$, which will give you a TIme $tring of six characters in hours, minutes and seconds. The TI$ timer can also be assigned an arbitrary starting point similar to the way you set your wristwatch. The value of TI$ is not accurate after tape I/O.
EXAMPLE of TI$ Function:
1 2 3 4
1 TI$ = "000000": FOR J=1 TO 10000: NEXT: PRINT TI$
000011
USR
TYPE: Floating-Point Function FORMAT: USR ()
Action: The USR function jumps to a User callable machine language SubRoutine which has its starting address pointed to by the contents of memory locations 785-786. The starting address is established before calling the USR function by using POKE statements to set up locations 785-786. Unless POKE statements are used, locations 785-786 will give you an ?ILLEGAL QUANTITY error message.
The value of the argument is stored in the floating-point accumulator starting at location 97, for access by the Assembler code, and the result of the USR function is the value which ends up there when the subroutine returns to BASIC.
EXAMPLES of USR Function:
1 2 3
10 B=T*SIN(Y) 20 C=USR(B/2) 30 D=USR(B/3)
VAL
TYPE: Numeric Function FORMAT: VAL ()
Action: Returns a numeric VALue representing the data in the argument. If the first non-blank character of the string is not a plus sign (+), minus sign (-), or a digit the VALue returned is zero. String conversion is finished when the end of the string or any non-digit character is found (except decimal point or exponential e).
EXAMPLE of VAL Function:
1 2 3
10 INPUT#1, NAM$, ZIP$ 20 IF VAL(ZIP$) 96699 THEN PRINT NAM$ TAB(25) "GREATER PHILADELPHIA"
VERIFY
TYPE: Command FORMAT: VERIFY [""][,]
Action: The VERIFY command is used, in direct or program mode, to compare the contents of a BASIC program file on tape or disk with the program currently in memory. VERIFY is normally used right after a SAVE, to make sure that the program was stored correctly on tape or disk.
If the number is left out, the program is assumed to be on the Datassette which is device number 1. For tape files, if the is left out, the next program found on the tape will be com- pared. For disk files (device number 8), the file-name must be present. If any differences in program text are found, the BASIC error message ?VERIFY ERROR is displayed.
A program name can be given either in quotes or as a string variable. VERIFY is also used to position a tape just past the last program, so that a new program can be added to the tape without accidentally writing over another program.
EXAMPLES of VERIFY Command:
VERIFY (Checks 1st program on tape) PRESS PLAY ON TAPE OK SEARCHING FOUND VERIFYING
1 2
9000 SAVE "ME",8: 9010 VERIFY "ME",8 (Looks at device 8 for the program)
WAIT
TYPE: Statement FORMAT: WAIT ,[,]
Action: The WAIT statement causes program execution to be suspended until a given memory address recognizes a specified bit pattern. In other words WAIT can be used to halt the program until some external event has occurred. This is done by monitoring the status of bits in the input/ output registers, The data items used with WAIT can be any numeric expressions, but they will be converted to integer values. For most programmers, this statement should never be used. It causes the program to halt until a specific memory location's bits change in a specific way. This is used for certain I/O operations and almost nothing else.
The WAIT statement takes the value in the memory location and performs a logical AND operation with the value in mask-1. If there is a mask-2 in the statement, the result of the first operation is exclusive-ORed with mask-2. In other words mask-1 "filters out" any bits that you don't want to test. Where the bit is 0 in mask-1, the corresponding bit in the result will always be 0. The mask-2 value flips any bits, so that you can test for an off condition as well as an on condition, Any bits being tested for a 0 should have a I in the corresponding position in mask-2.
If corresponding bits of the and operands differ, the exclusive-OR operation gives a bit result of 1. If corresponding bits get the same result the bit is 0. It is possible to enter an infinite pause with the WAIT statement, in which case the and keys can be used to recover. Hold down the key and then press . The first example below WAITs until a key is pressed on the tape unit to continue with the program. The second example will WAIT until a sprite collides with the screen background.
EXAMPLES of WAIT Statement:
1 2 3
WAIT 1,32,32 WAIT 53273,6,6 WAIT 36868,144,16 (144 & 16 are masks. 144=10010000 in binary and 16=10000 in binary. The WAIT statement will halt the program until the 128 bit is on or until the 16 bit is off)