PEEK and POKE

From Academic Kids

This article is about the BASIC statements. For other meanings of these words, see peek and poke.

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

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.)

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 fuctions 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.

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.

See also

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools