PEEK and POKE

From Wikipedia, the free encyclopedia

This article is about BASIC statements. Peek & Poke is also the title of an album by White Town
For other uses of "peek", see peek (disambiguation).
For other uses of "poke", see poke (disambiguation).

In computing, PEEK is a BASIC programming language function used for reading the contents of a memory cell at a specified address. The corresponding command to set the contents of a memory cell is POKE.

Contents

[edit] Statement syntax

The PEEK function and POKE command are usually invoked as follows, either in direct mode (entered and executed at the BASIC prompt) or in indirect mode (as part of a program):

   integer_variable = PEEK(address)
   
   POKE address, value

Note that address and value may stand for arbitrarily complex expressions*, as long as the evaluation of those expressions end up as valid memory addresses or values, respectively. A valid address in this context is an address within the computer's total address space or the CPUs addressing range, whichever is the smallest, while a valid value is (typically) an unsigned value between zero and the maximum unsigned number that the minimum addressable unit (memory cell) may hold.†

Example: A typical early 1980s home computer could have 32 KB main memory and an 8-bit microprocessor CPU with a 16-bit address range, leading to the following restrictions on PEEK and POKE parameter values:

   0 <= address <= 32767    ; comment:  32767 = 32K-1 = 2^15-1, 
                            ; i.e. smaller than the CPU's 16-bit address range
                            ; from 0 to 65535 = 64K-1 = 2^16-1
   
   0 <=  value  <=   255    ; comment:    255 =  2^8-1, 
                            ; i.e. the maximum value of an 8-bit byte

(* A slightly complex-looking POKE-statement might look like this: POKERARRAY(42),PEEK(B1ADDR+(ASC(T9TXTS$))-VARRAY(42)) (where combinations of various BASIC functions and variables are used to build the parameters, and the interpreter allows space characters to be left out.) )

(† Plain, straightforward limits on PEEK/POKE parameters—i.e., values restricted by address range/memory and word width—may for various reasons be amended by the BASIC interpreter/compiler designers, e.g. such as to allow for signed input values and/or outside address range/memory space address values; reasons for this could be e.g. to simplify certain operations by hiding unnecessary complexity from the BASIC programmer. In such a scheme, the PEEK and POKE machine code sequences within the interpreter/compiler must compute valid memory access parameters, of course.)

[edit] Memory cells and hardware registers

The address locations POKEd to or PEEKed from may refer either to ordinary memory cells or to memory-mapped hardware registers of I/O units or support chips such as sound chips and video graphics chips, or even to memory-mapped registers of the CPU itself (making possible the software implementation of powerful machine code monitors and debugging/simulation tools). An example of POKE-driven support chip control: the following POKE command, directed at a specific register of the Commodore 64's built-in VIC-II graphics chip, will make the screen border turn black:

   POKE 53280, 0

Different pre/non-PC computers usually differ as to the memory address areas designated for user programs, user data, operating system code and data, and memory-mapped hardware units. Because of all this, PEEK functions and POKE commands are inherently non-portable, meaning that a given sequence of those statements will almost never work on any other system than the one the program was written for.

[edit] POKEs as cheats

In the context of games for many 8-bit computers, it was a usual practice to load games into memory and, before launching them, modify specific memory addresses in order to cheat, getting an unlimited number of lives, immunity, invisibility, etc. Such modifications were performed through POKE sentences. The Commodore 64 and ZX Spectrum also allowed players with the proper cartridges or Multiface add-on to freeze the running program, enter POKEs, and resume.

For instance, with

   POKE 47196, 201

in Knight Lore for the ZX Spectrum, immunity is achieved. Magazines like Microhobby used to publish lists of such POKEs for games. Of course, in order to find them someone had to read the machine code and locate the critical point where the number of lives is decreased, impacts detected, etc. Sometimes the term POKE was used with this specific meaning.

[edit] Generic usage of "POKE"

"POKE" is sometimes used as a generic term to refer to any direct manipulation of the contents of memory, rather than just via BASIC, particularly among people who learned computing on the 8-bit microcomputers of the late 70s and early 80s. BASIC was often the only language available on those machines (on home computers, usually present in ROM), and therefore the obvious, and simplest, way to program in machine language was to use BASIC to POKE the opcode values into memory. Doing much low-level coding like this usually came from lack of access to an assembler.

[edit] See also

In other languages