PEEK

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: PEEK
Abbreviation: P, Shift+E
Type: Function
Token code: 194/$C2
Handling routine
in BASIC ROM:
47117–47139
$B80D–B823
List of all BASIC keywords


Remark: This article describes the BASIC command PEEK in BASIC V2 at the Commodore 64.

Typ: Integer-Function
General Programming-Syntax: PEEK(<Memory address>)

The BASIC function PEEK returns the memory contents of the specified address, which must be in the range 0 through 65535. The byte value returned will be in the range from 0 thru 255. If the address given exceeds the limits of the memory map, the BASIC error message ?ILLEGAL QUANTITY  ERROR will occur.

Examples[edit | edit source]

POKE 53280, PEEK(53281)

This sets the screen frame to the same color as the background, by reading the value of the latter and POKEing it into the former.

A = PEEK(53281)

Value of background color is transferred in floating point variable A.


PRINT PEEK(646) AND 15

Display the code for the current color of the cursor in the ranges from 0 until 15.

PRINT PEEK(53281) AND 15

Display the actual color code (in the range 0 thru 15) of the screen background. The AND operator "masks out" bits in the register not relevant to the color code; causing only "valid" to be reported.

10 A = PEEK(197): IF A=64 THEN 10
20 PRINT "THE PUSHED KEY HAS GOT THE VALUE: "; A
40 IF A=46 THEN END
30 IF A<>0 THEN 10

This program reports the key code of whichever key on the Keyboard is currently being pressed, or the value 64 if no key is pressed. This example can be stopped using not just the RUN/STOP key as usual, but also by pressing the INST/DEL key (whose keyboard code is 0), by virtue of the Conditional branch in line 30.

B% = PEEK(43) + PEEK(44) * 256

This example shows how to use PEEK to read the "actual" value of a two-byte pointer in the low-byte/high-byte form, in this case the pointer to the beginning of BASIC RAM. The result, 2049 under default conditions, is stored in the integer variable B%.

Also see the BASIC-Command POKE.

BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT