This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "PEEK and POKE" – news ·newspapers ·books ·scholar ·JSTOR(October 2015) (Learn how and when to remove this message) |
Incomputing,PEEK and POKE are commands used in somehigh-level programming languages for accessing the contents of a specificmemory cell referenced by itsmemory address. PEEK gets the byte located at the specified memory address.[1]POKE sets the memory byte at the specified address.[2] These commands originated withmachine code monitors such as theDECsystem-10 monitor;[3] these commands are particularly associated with theBASIC programming language, though some other languages such asPascal andCOMAL also have these commands. These commands are comparable in their roles topointers in theC language and some other programming languages.
One of the earliest references to these commands in BASIC, if not the earliest, is inAltair BASIC.[4] The PEEK and POKE commands were conceived in early personal computing systems to serve a variety of purposes, especially for modifying specialmemory-mappedhardware registers to control particular functions of the computer such as the input/output peripherals. Alternatively programmers might use these commands to copy software or even to circumvent the intent of a particular piece of software (e.g. manipulate a game program to allow the user to cheat). Today it is unusual to control computer memory at such a low level using a high-level language like BASIC. As such the notions ofPEEK andPOKE commands are generally seen as antiquated.
The termspeek andpoke are sometimes used colloquially in computer programming to refer to memory access in general.
The PEEK function and POKE commands are usually invoked as follows, either indirect mode (entered and executed at the BASICprompt) or in indirect mode (as part of aprogram):
integer_variable=PEEK(address)POKEaddress,value
Theaddress andvalue parameters may containexpressions, as long as the evaluated expressions correspond to valid memory addresses or values, respectively. A validaddress in this context is an address within the computer'saddress space, while a validvalue is (typically) an unsigned value between zero and the maximum unsigned number that the minimum addressable unit (memory cell) may hold.
The address locations that are POKEd or PEEKed at may refer either to ordinary memory cells or tomemory-mappedhardware registers ofI/O units or support chips such assound chips and video graphics chips, or even to memory-mappedregisters of the CPU itself (which makes software implementations of powerfulmachine code monitors anddebugging/simulation tools possible). As an example of a POKE-driven support chip control scheme, the following POKE command is directed at a specific register of theCommodore 64's built-inVIC-II graphics chip, which will make the screen border turn black:
POKE53280,0
A similar example from theAtari 8-bit computers tells theANTIC display driver to turn all text upside-down:
POKE755,4
The difference between machines, and the importance and utility of the hard-wired memory locations, meant that "memory maps" of various machines were important documents. An example isMapping the Atari, which starts at location zero and mapped out the entire 64 kB memory of the Atari 8-bit systems location by location.
North Star Computers, a vendor from the early 1980s, offered their own dialect of BASIC with their NSDOSoperating system. Concerned about possible legal issues, they renamed the commandsEXAM
andFILL
.[citation needed] There were also BASIC dialects that used the reserved wordsMEMR
andMEMW
instead.
BBC BASIC, used on theBBC Micro and otherAcorn Computers machines, did not feature the keywordsPEEK andPOKE but used thequestion mark symbol (?), known asquery in BBC BASIC, for both operations, as a function and command. For example:
>DIMW%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 theexclamation mark symbol (!), known aspling, with the least significant byte first (little-endian). In addition, the address could be offset by specifying either query or plingafter the address and following it with the offset:
>!W%=&12345678:REM ampersand (&) specifies hexadecimal>PRINT~?W%,~W%?3:REM tilde (~) prints in hexadecimal7812
Strings of text could be PEEKed and POKEd in a similar way using theDollar sign ($). The end of the string is marked with theCarriage return character (&0D inASCII); when read back, this terminating character is not returned. Offsets cannot be used with the dollar sign.
>DIMS%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, excluding &0D terminator, and starting at S% + 6 bytesPIES
As most earlyhome computers used 8-bit processors, PEEK or POKE values are between 0 and 255. Setting or reading a 16-bit value on such machines requires two commands, such asPEEK(A)+256*PEEK(A+1)
to read a 16-bit integer at address A, andPOKEA,V
followed byPOKEA+1,V/256
to store a 16-bit integer V at address A.
Some BASICs, even on 8-bit machines, have commands for reading and writing 16-bit values from memory.BASIC XL for theAtari 8-bit computers uses a "D" (for "double") prefix:DPEEK
andDPOKE
. The East-German "Kleincomputer" KC85/1 and KC87 calls themDEEK
andDOKE
.[5]
TheSinclair QL hasPEEK_W
andPOKE_W
for 16-bit values andPEEK_L
andPOKE_L
for 32-bit values.ST BASIC for the Atari ST uses the traditional names but allows defining 8/16/32 bit memory segments and addresses that determine the size.
A Linux command linepeekpoke
[6] utility has been developed mainly for ARM based single board computers.peekpoke
is a Linux command line tool to read from and write to system memory. Its main use is to talk to hardware peripherals from userland: to read or manipulate state, and to dump registers.
In the context of games for many 8-bit computers, users could load games into memory and, before launching them, modify specific memory addresses in order tocheat, getting an unlimited number of lives, immunity, invisibility, etc. Such modifications were performed using POKE statements. TheCommodore 64,ZX Spectrum andAmstrad CPC also allowed players with one of the relevant cartridges (such asAction Replay orMultiface) to freeze the running program, enter POKEs, and resume.
For example, inKnight Lore for theZX Spectrum, immunity can be achieved with the following command:[7]
POKE47196,201
In this case, the value 201 corresponds to aRET instruction,[8] so that the game returns from a subroutine early before triggeringcollision detection.
Magazines such asYour Sinclair 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.[9]
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 enforcevirtual memory protection schemes to deny external program access to non-shared memory (for example, separatepage tables for each application, hence inaccessible memory spaces).
"POKE" is sometimes used to refer to any direct manipulation of the contents of memory, rather than just via BASIC, particularly among people who learned computing on the8-bitmicrocomputers of the late 1970s and early 1980s. BASIC was often the only language available on those machines (onhome computers, usually present inROM), and therefore the obvious, and simplest, way to program inmachine language was to use BASIC to POKE theopcode values into memory. Doing much low-level coding like this usually came from lack of access to anassembler.
An example of the generic usage of POKE and PEEK is inVisual Basic for Windows, whereDDE can be achieved with theLinkPoke keyword.
{{cite magazine}}
: CS1 maint: year (link)