All Topics  
PEEK and POKE

 

   Email Print
   Bookmark   Link






 

PEEK and POKE



 
 
In computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
, PEEK is a BASIC
BASIC

In computer programming, BASIC is a family of high-level programming languages. The Dartmouth BASIC was designed in 1964 by John George Kemeny and Thomas Eugene Kurtz at Dartmouth College in New Hampshire, United States to provide computer access to non-science students....
 programming language function used for reading the contents of a memory cell at a specified address
Memory address

In computer science, a memory address is an identifier for a computer memory location, at which a computer program or a hardware device can store a piece of data and later retrieve it....
. The corresponding command to set the contents of a memory cell is POKE.

rce lang="qbasic"> integer_variable = PEEK(address)

POKE address, value The address and value parameteres may contain complex expression
Expression (programming)

An expression in a programming language is a combination of value s, variables, operator s, and function s that are interpreted according to the particular Order of operations and of association for a particular programming language, which computes and then produces another value....
s, as long as the evaluated expressions correspond to valid memory addresses or values, respectively.






Discussion
Ask a question about 'PEEK and POKE'
Start a new discussion about 'PEEK and POKE'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
, PEEK is a BASIC
BASIC

In computer programming, BASIC is a family of high-level programming languages. The Dartmouth BASIC was designed in 1964 by John George Kemeny and Thomas Eugene Kurtz at Dartmouth College in New Hampshire, United States to provide computer access to non-science students....
 programming language function used for reading the contents of a memory cell at a specified address
Memory address

In computer science, a memory address is an identifier for a computer memory location, at which a computer program or a hardware device can store a piece of data and later retrieve it....
. The corresponding command to set the contents of a memory cell is POKE.

Statement syntax


The PEEK function and POKE command are usually invoked as follows, either in direct mode
Direct mode

Direct mode is a computing term referring to the input of textual Command outside the context of a program. The command would be executed immediately and the results printed on screen, in contrast to programming mode where nothing would be executed until the RUN command was given Some commands could be used in either mode, others were exclu...
 (entered and executed at the BASIC prompt
Command Prompt

Command Prompt may stand for:* Command line interpreter, a kind of text-based user interface* Command Prompt , the command line interpreter in Windows operating systems...
) or in indirect mode (as part of a program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
): integer_variable = PEEK(address)

POKE address, value The address and value parameteres may contain complex expression
Expression (programming)

An expression in a programming language is a combination of value s, variables, operator s, and function s that are interpreted according to the particular Order of operations and of association for a particular programming language, which computes and then produces another value....
s, as long as the evaluated expressions correspond to valid memory addresses or values, respectively. A valid address in this context is an address within the computer's total address space
Address space

In computing, an address space defines a range of discrete addresses, each of which may correspond to a physical or virtual memory register, a Node , peripheral device, disk sector or other logical or physical entity....
 or the CPU
Central processing unit

A central processing unit is an electronic circuit that can execute computer programs. This broad definition can easily be applied to many early computers that existed long before the term "CPU" ever came into widespread usage....
s 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.

For example, a typical early 1980s home computer
Home computer

A home computer was a class of personal computer entering the market in 1977 and becoming common during the 1980s. They were marketed to consumers as accessible personal computers, more capable than video game consoles....
 might have 32 KB
Kilobyte

Kilobyte is a unit of Computer data storage equal to either 1,024 bytes or 1,000 bytes , depending on context.It is abbreviated in a number of ways: KB, kB, K and Kbyte....
 of main memory, and an 8-bit
8-bit

Eight-bit CPUs normally use an 8-bit data bus and a 16-bit address bus which means that their address space is limited to 64 KBs. This is not a "natural law", however, so there are exceptions....
 microprocessor, with a 16-bit
16-bit

16-bit architectureThe HP 2100#Descendants and variants , introduced in 1975, was the world's first 16-bit microprocessor.Prominent 16-bit processors include the PDP-11, Intel 8086, Intel 80286 and the WDC 65C816....
 address range, in which case the allowable values for the PEEK and POKE parameter values would be:

0 <= address <= 32767 ; comment: 32767 = 32K
Binary prefix

In computing, a binary prefix is a set of letters that precede a unit of measure to indicate multiplication by a power of two. In certain contexts in computing, such as computer memory sizes, units of information storage and communication traffic have traditionally been reported in multiples of powers of two....
-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
Byte

A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space....


Memory cells and hardware registers


The address locations POKEd to or PEEKed from may refer either to ordinary memory cells or to memory-mapped
Memory-mapped I/O

Memory-mapped I/O and port I/O are two complementary methods of performing input/output between the Central processing unit and peripheral devices in a computer....
 hardware register
Hardware register

In digital electronics, especially computing, a hardware register stores bits of information, in a way that all the bits can be written to or read out simultaneously....
s of I/O
Input/output

In computing, input/output, or I/O, refers to the communication between an information processing system , and the outside world ? possibly a human, or another information processing system....
 units or support chips such as sound chip
Sound chip

A sound chip is an integrated circuit designed to produce sound . It might be doing this through digital, analog or mixed-mode integrated circuit electronics....
s and video graphics chips, or even to memory-mapped register
Processor register

In computer architecture, a processor register is a small amount of Computer storage available on the CPU whose contents can be accessed more quickly than storage available elsewhere....
s of the CPU itself (making possible the software implementation of powerful machine code monitor
Machine code monitor

A machine code monitor is software built into or separately available for various computers, allowing the user to enter commands to view and change memory address on the machine, with options to load and save memory contents from/to secondary storage....
s and debugging
Debugging

Debugging is a methodical process of finding and reducing the number of computer bugs, or defects, in a computer program or a piece of electronic hardware thus making it behave as expected....
/simulation tools). As an example of POKE-driven support chip control, the following POKE command is directed at a specific register of the Commodore 64
Commodore 64

The Commodore 64 is an 8-bit home computer released by Commodore International in August, 1982, at a price of United States dollar595. Preceded by the Commodore VIC-20 and Commodore MAX Machine, the C64 features 64 kilobytes of Random-access memory with sound and graphics performance that were superior to IBM-compatible computers of tha...
's built-in VIC-II
MOS Technology VIC-II

The VIC-II , specifically known as the MOS Technology 6567/8562/8564 , 6569/8565/8566 , is the integrated circuit tasked with generating S-Video/composite video graphics and dynamic random access memory memory refresh signals in the Commodore 64 and Commodore 128 home computers....
 graphics chip, which will make the screen border turn black: POKE 53280, 0

Different pre/non-PC
IBM PC compatible

IBM PC compatible computers are those generally similar to the original IBM Personal Computer, IBM Personal Computer XT, and IBM Personal Computer/AT....
 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. For these reasons, PEEK functions and POKE commands are inherently non-portable
Porting

In computer science, porting is the process of adapting software so that an executable Computer program can be created for a computing environment that is different from the one for which it was originally designed ....
, meaning that a given sequence of those statements will almost certainly not work on any system other than the one for which the program was written.

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 using POKE statements. The Commodore 64
Commodore 64

The Commodore 64 is an 8-bit home computer released by Commodore International in August, 1982, at a price of United States dollar595. Preceded by the Commodore VIC-20 and Commodore MAX Machine, the C64 features 64 kilobytes of Random-access memory with sound and graphics performance that were superior to IBM-compatible computers of tha...
, ZX Spectrum
ZX Spectrum

The Sinclair ZX Spectrum is an 8-bit personal home computer released in the United Kingdom in 1982 by Sinclair Research Ltd. Referred to during development as the ZX81 Colour and ZX82, the machine was launched as the ZX Spectrum by Sinclair to highlight the machine's colour display, compared with the black-and-white of its predec...
 and Amstrad CPC
Amstrad CPC

The Amstrad CPC is a series of 8-bit home computers produced by Amstrad during the 1980s and early 1990s. "CPC" stands for 'Colour Personal Computer', although it was possible to purchase a CPC with a Green screen display as well as with the standard colour screen ....
 also allowed players with the relevant cartridges or Multiface
Multiface

The Multiface was a hardware peripheral released by Romantic Robot UK Ltd. for several 1980s home computers. The primary function of the device was to core dump the computer's memory to external storage, and featured an iconic 'red button' that could be pressed at any time in order to activate it....
 add-on to freeze the running program, enter POKEs, and resume.

For example, in Knight Lore
Knight Lore

Knight Lore is a Video game developed and released by Ultimate Play The Game in 1984. The game is the third in the Sabre Man series, following on from his adventures in Sabre Wulf and Underwurlde....
 for the ZX Spectrum
ZX Spectrum

The Sinclair ZX Spectrum is an 8-bit personal home computer released in the United Kingdom in 1982 by Sinclair Research Ltd. Referred to during development as the ZX81 Colour and ZX82, the machine was launched as the ZX Spectrum by Sinclair to highlight the machine's colour display, compared with the black-and-white of its predec...
, immunity can be achieved with the following command: POKE 47196, 201 In this case, the value 201 corresponds to a RET instruction, so that the game returns from a subroutine early without reducing the player's number of lives.

Magazines such as Microhobby published lists of such POKEs for games. Such codes were generally identified by reverse-engineering the machine code to locate the memory address containing the desired value that related to, for example, the number of lives, detection of collisions, etc.

Using a 'POKE' cheat is more difficult in modern games, as many include anti-cheat or copy-protection measures that inhibit modification of the game's memory space. Modern operating systems may also enforce virtual memory
Virtual memory

Virtual memory is a computer system technique which gives an application program the impression that it has contiguous working memory , while in fact it may be physically fragmented and may even overflow on to disk storage....
 protection schemes to deny external program access to non-shared memory (for example, separate page table
Page table

A page table is the data structure used by a virtual memory system in a computer operating system to store the mapping between virtual addresses and physical addresses....
s for each application, hence inaccessible memory spaces).

Peek and Poke in BBC BASIC


BBC BASIC
BBC BASIC

BBC BASIC is a programming language, developed in 1981 as a native programming language for the MOS Technology 6502 based Acorn Computers BBC Micro home/personal computer, mainly by Sophie Wilson....
, used on the BBC Micro
BBC Micro

The BBC Microcomputer System, or BBC Micro, was a series of microcomputers and associated peripherals designed and built by Acorn Computers for the BBC Computer Literacy Project, operated by the British Broadcasting Corporation....
 and other Acorn Computers
Acorn Computers

Acorn Computers was a British computer company established in Cambridge, England, in 1978. The company produced a number of computers which were especially popular in the United Kingdom....
 machines, did not feature the keywords PEEK and POKE but used the question mark
Question mark

The question mark , also known as an interrogation point, question point, query, or eroteme, is a punctuation that replaces the Full stop at the end of an interrogative sentence....
 symbol, known as query in BBC BASIC, for both operations, as a function and command. For example:

>DIM W% 4 : REM reserve 4 bytes of memory, pointed to by W% >?W% = 42 : REM equivalent of 'POKE W%, 42' >PRINT ?W% : REM equivalent of 'PEEK(W%)' 42



32-bit values could be POKEd and PEEKed using the exclamation mark
Exclamation mark

An exclamation mark or exclamation point is a punctuation mark: ! It is usually used after an interjection or exclamation to indicate strong feelings or high volume, and often marks the end of a sentence....
 symbol (!), known as pling, with the least significant byte first (little-endian
Endianness

In computing, endianness is the byte ordering used to represent some kind of data. Typical cases are the order in which integer values are stored as bytes in computer memory and the transmission order over a network or other medium....
). In addition, the address could be offset by specifying either query or pling after the address and following it with the offset:

>!W% = &12345678 : REM ampersand (&) specifies hexadecimal >PRINT ~?W%, ~W%?3 : REM tilde (~) prints in hexadecimal 7812

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
8-bit

Eight-bit CPUs normally use an 8-bit data bus and a 16-bit address bus which means that their address space is limited to 64 KBs. This is not a "natural law", however, so there are exceptions....
 microcomputer
Microcomputer

A microcomputer is a computer with a microprocessor as its central processing unit. Another general characteristic of these computers is that they occupy physically small amounts of space when compared to mainframe computer and minicomputers....
s of the late 70s and early 80s. BASIC was often the only language available on those machines (on home computers, usually present in ROM
Read-only memory

Read-only memory is a class of computer storage media used in computers and other electronic devices. Because data stored in ROM cannot be modified , it is mainly used to distribute firmware ....
), and therefore the obvious, and simplest, way to program in machine language was to use BASIC to POKE the opcode
Opcode

In computer technology, an opcode is the portion of a machine language instruction that specifies the operation to be performed. Their specification and format are laid out in the instruction set architecture of the processor in question ....
 values into memory. Doing much low-level coding like this usually came from lack of access to an assembler
Assembly language

An assembly language is a low-level language for programming computers. It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture....
.

An example of the generic usage of POKE and PEEK is in traditional Visual Basic
Visual Basic

'Visual Basic' is the third-generation programming language event-driven programming and integrated integrated development environment from Microsoft for its Component Object Model programming model....
 for Windows, where DDE
Dynamic Data Exchange

Dynamic Data Exchange is a technology for communication between multiple applications under Microsoft Windows or OS/2....
 can be achieved with the LinkPoke keyword.

See also

  • Killer poke
    Killer poke

    This article is about the hardware damage method. For the WCW incident, see The Fingerpoke of Doom.In computer jargon, a killer poke is a method of inducing computer hardware damage on a machine and/or its peripherals by the insertion of invalid values, via e.g....
  • Type-in program
    Type-in program

    A type-in program, or just type-in, is a computer program listing printed in a computer magazine or book, meant to be typed in by the reader in order to run the program on a computer....
  • Self-modifying code
    Self-modifying code

    In computer science, self-modifying code is Code that alters its own Instruction while it is Execution - usually to reduce the instruction path length and improve performance....