PEEK and POKE
Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

, PEEK is a BASIC
BASIC
BASIC is a family of general-purpose, high-level programming languages whose design philosophy emphasizes ease of use - the name is an acronym from Beginner's All-purpose Symbolic Instruction Code....

 programming language extension used for reading the contents of a memory cell at a specified address
Memory address
A digital computer's memory, more specifically main memory, consists of many memory locations, each having a memory address, a number, analogous to a street address, at which computer programs store and retrieve, machine code or data. Most application programs do not directly read and write to...

. 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, also known as immediate mode is a computing term referring to the input of textual commands 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 a specific...

 (entered and executed at the BASIC prompt) or in indirect mode (as part of a program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

):

integer_variable = PEEK(address)

POKE address, value

The address and value parameters may contain complex expression
Expression (programming)
An expression in a programming language is a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence 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 address space
Address space
In computing, an address space defines a range of discrete addresses, each of which may correspond to a network host, peripheral device, disk sector, a memory cell or other logical or physical entity.- Overview :...

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

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 CPU 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.The hardware registers inside a central processing unit are called processor registers....

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. Inputs are the signals or data received by the system, and outputs are the signals or data sent from it...

 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 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 storage available as part of a CPU or other digital processor. Such registers are addressed by mechanisms other than main memory and can be accessed more quickly...

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 locations on the machine, with options to load and save memory contents from/to secondary storage.Machine code monitors became something of a...

s and debugging
Debugging
Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected. Debugging tends to be harder when various subsystems are tightly coupled, as changes in one may cause bugs to emerge...

/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 introduced by Commodore International in January 1982.Volume production started in the spring of 1982, with machines being released on to the market in August at a price of US$595...

'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 microchip tasked with generating Y/C/composite video graphics and DRAM refresh signals in the Commodore 64 and C128 home computers.Succeeding MOS's original VIC , the VIC-II was one of the two chips...

 graphics chip, which will make the screen border turn black:
POKE 53280, 0

Pre and non-PC
IBM PC compatible
IBM PC compatible computers are those generally similar to the original IBM PC, XT, and AT. Such computers used to be referred to as PC clones, or IBM clones since they almost exactly duplicated all the significant features of the PC architecture, facilitated by various manufacturers' ability to...

 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 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 common 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 introduced by Commodore International in January 1982.Volume production started in the spring of 1982, with machines being released on to the market in August at a price of US$595...

, ZX Spectrum
ZX Spectrum
The ZX Spectrum is an 8-bit personal home computer released in the United Kingdom in 1982 by Sinclair Research Ltd...

 and Amstrad CPC
Amstrad CPC
The Amstrad CPC is a series of 8-bit home computers produced by Amstrad between 1984 and 1990. It was designed to compete in the mid-1980s home computer market dominated by the Commodore 64 and the Sinclair ZX Spectrum, where it successfully established itself primarily in the United Kingdom,...

 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 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 computer game developed and released by Ultimate Play The Game in 1984. The game is the third in the Sabreman series, following on from his adventures in Sabre Wulf and Underwurlde. Unlike the earlier games in the series it used Ultimate's filmation engine to achieve a 3D look...

for the ZX Spectrum
ZX Spectrum
The ZX Spectrum is an 8-bit personal home computer released in the United Kingdom in 1982 by Sinclair Research Ltd...

, 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 before triggering collision detection.

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
In computing, virtual memory is a memory management technique developed for multitasking kernels. This technique virtualizes a computer architecture's various forms of computer data storage , allowing a program to be designed as though there is only one kind of memory, "virtual" memory, which...

 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. Virtual addresses are those unique to the accessing process...

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 BBC Micro home/personal computer, mainly by Sophie Wilson. It is a version of the BASIC programming language adapted for a U.K...

, 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 Ltd. was a British computer company established in Cambridge, England, in 1978. The company produced a number of computers which were especially popular in the UK. These included the Acorn Electron, the BBC Micro, and the Acorn Archimedes...

 machines, did not feature the keywords PEEK and POKE but used the question mark
Question mark
The question mark , is a punctuation mark that replaces the full stop at the end of an interrogative sentence in English and many other languages. The question mark is not used for indirect questions...

 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 integer variable W%
> ?W% = 42 : REM store constant 42; equivalent of 'POKE W%, 42'
> PRINT ?W% : REM print the byte pointed to by W%; equivalent of 'PRINT PEEK(W%)'
42


32-bit values could be POKEd and PEEKed using the exclamation mark
Exclamation mark
The exclamation mark, exclamation point, or bang, or "dembanger" is a punctuation mark usually used after an interjection or exclamation to indicate strong feelings or high volume , and often marks the end of a sentence. Example: “Watch out!” The character is encoded in Unicode at...

 symbol (!), known as pling, with the least significant byte first (little-endian). 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
78 12


Strings of text could be PEEKed and POKEd in a similar way using the Dollar sign
Dollar sign
The dollar or peso sign is a symbol primarily used to indicate the various peso and dollar units of currency around the world.- Origin :...

 ($). The end of the string is marked with the Carriage return
Carriage return
Carriage return, often shortened to return, refers to a control character or mechanism used to start a new line of text.Originally, the term "carriage return" referred to a mechanism or lever on a typewriter...

 character (&0D in ASCII
ASCII
The American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text...

); when read back, this terminating character is not returned. Offsets cannot be used with the dollar sign.


> DIM S% 20 : REM reserve 20 bytes of memory pointed to by S%
> $S% = "MINCE PIES" : REM store string 'MINCE PIES', terminated by &0D
> PRINT $(S% + 6) : REM retrieve string, termined by &0D started at S% + 6 bytes
PIES

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
The first widely adopted 8-bit microprocessor was the Intel 8080, being used in many hobbyist computers of the late 1970s and early 1980s, often running the CP/M operating system. The Zilog Z80 and the Motorola 6800 were also used in similar computers...

 microcomputer
Microcomputer
A microcomputer is a computer with a microprocessor as its central processing unit. They are physically small compared to mainframe 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 storage medium used in computers and other electronic devices. Data stored in ROM cannot be modified, or can be modified only slowly or with difficulty, so it is mainly used to distribute firmware .In its strictest sense, ROM refers only...

), and therefore the obvious, and simplest, way to program in machine language was to use BASIC to POKE opcode
Opcode
In computer science engineering, 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 contained in DATA statements 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 programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given 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 event-driven programming language and integrated development environment from Microsoft for its COM programming model...

 for Windows, where DDE
Dynamic Data Exchange
Dynamic Data Exchange is a technology for interprocess communication under Microsoft Windows or OS/2.- Overview :Dynamic Data Exchange was first introduced in 1987 with the release of Windows 2.0 as a method of interprocess communication so that one program can communicate with or control another...

 can be achieved with the LinkPoke keyword.

Cheats for 8-bit video games were sometimes referred to as pokes (see "POKEs as cheats" above).
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK