Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
PEEK and POKE
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Commands in some high-level programming languages}} {{for|the White Town album|Peek & Poke (album)}} {{refimprove|date=October 2015}} [[File:Monitor Commodore CBM 3016 with BASIC program-0309.jpg|thumb|PEEK and POKE in line 100 of a [[Commodore BASIC|Commodore Basic]] program on a [[CBM 3016]]]] In [[computing]], '''PEEK and POKE''' are commands used in some [[high-level programming language]]s for accessing the contents of a specific [[Memory cell (computing)|memory cell]] referenced by its [[memory address]]. PEEK gets the byte located at the specified memory address.<ref name="PEEK">{{cite web|url = http://www.qbasicnews.com/qboho/qckpeek.shtml|title = PEEK|work = Microsoft QuickBasic 4.5 Advisor|access-date = 2007-12-28|year = 1990|publisher = Microsoft|archive-url = https://web.archive.org/web/20110516170043/http://www.qbasicnews.com/qboho/qckpeek.shtml|archive-date = 2011-05-16|url-status = dead}}</ref> POKE sets the memory byte at the specified address.<ref name="POKE">{{cite web|url = http://www.qbasicnews.com/qboho/qckpoke.shtml|title = POKE|work = Microsoft QuickBasic 4.5 Advisor|access-date = 2007-12-28|year = 1990|publisher = Microsoft|archive-url = https://web.archive.org/web/20110516170056/http://www.qbasicnews.com/qboho/qckpoke.shtml|archive-date = 2011-05-16|url-status = dead}}</ref> These commands originated with [[machine code monitor]]s such as the [[PDP-10|DECsystem-10]] monitor;<ref>{{cite web|url=https://retrocomputing.stackexchange.com/questions/15872/what-is-the-oldest-reference-to-peek-poke-and-usr|title=What is the oldest reference to PEEK, POKE, and USR?|access-date=15 August 2020|archive-date=14 August 2020|archive-url=https://web.archive.org/web/20200814233608/https://retrocomputing.stackexchange.com/questions/15872/what-is-the-oldest-reference-to-peek-poke-and-usr|url-status=live}}</ref> these commands are particularly associated with the [[BASIC]] programming language, though some other languages such as [[Pascal (programming language)|Pascal]] and [[COMAL (programming language)|COMAL]] also have these commands. These commands are comparable in their roles to [[Pointer (computer programming)|pointers]] in the [[C (programming language)|C language]] and some other programming languages. One of the earliest references to these commands in BASIC, if not the earliest, is in [[Altair BASIC]].<ref>[https://archive.org/stream/bitsavers_mitsMITSAl_6669937/MITS_AltairBASIC_1975#page/n3/mode/1up Altair 8800 BASIC Reference_Manual 1975], Page 68 of PDF</ref> The PEEK and POKE commands were conceived in early personal computing systems to serve a variety of purposes, especially for modifying special [[memory-mapped I/O|memory-mapped]] [[hardware register]]s 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 of ''PEEK'' and ''POKE'' commands are generally seen as antiquated. The terms ''peek'' and ''poke'' are sometimes used colloquially in computer programming to refer to memory access in general. ==Statement syntax== The PEEK function and POKE commands are usually invoked as follows, either in [[direct mode]] (entered and executed at the BASIC [[command line interpreter|prompt]]) or in indirect mode (as part of a [[computer program|program]]): <syntaxhighlight lang="qbasic"> integer_variable = PEEK(address) POKE address, value </syntaxhighlight> The ''address'' and ''value'' parameters may contain [[expression (programming)|expression]]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]], 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 that are POKEd or PEEKed at may refer either to ordinary memory cells or to [[memory-mapped I/O|memory-mapped]] [[hardware register]]s of [[input/output|I/O]] units or support chips such as [[sound chip]]s and video graphics chips, or even to memory-mapped [[processor register|register]]s of the CPU itself (which makes software implementations of powerful [[machine code monitor]]s and [[debugging]]/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 the [[Commodore 64]]'s built-in [[MOS Technology VIC-II|VIC-II]] graphics chip, which will make the screen border turn black: <syntaxhighlight lang="qbasic">POKE 53280, 0</syntaxhighlight> A similar example from the [[Atari 8-bit computers]] tells the [[ANTIC]] display driver to turn all text upside-down: <syntaxhighlight lang="qbasic">POKE 755, 4</syntaxhighlight> 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 is ''[[Mapping the Atari]]'', which starts at location zero and maps out the entire 64 kB address space, location by location. =={{anchor|EXAM|FILL}}PEEK and POKE in other BASICs== [[North Star Computers]], a vendor from the early 1980s, offered their own dialect of BASIC with their NSDOS [[operating system]]. Concerned about possible legal issues, they renamed the commands <code>EXAM</code> and <code>FILL</code>.{{Citation needed|date=October 2015}} There were also BASIC dialects that used the reserved words <code>MEMR</code> and <code>MEMW</code> instead. [[BBC BASIC]], used on the [[BBC Micro]] and other [[Acorn Computers]] machines, did not feature the keywords ''PEEK'' and ''POKE'' but used the [[question mark]] symbol (?), known as ''query'' in BBC BASIC, for both operations, as a function and command. For example: <syntaxhighlight lang="bbcbasic"> > 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 </syntaxhighlight> 32-bit values could be POKEd and PEEKed using the [[exclamation mark]] symbol (!), known as ''pling'', with the least significant byte first ([[Endianness#Little-endian|little-endian]]). In addition, the address could be offset by specifying either query or pling ''after'' the address and following it with the offset: <syntaxhighlight lang="bbcbasic"> > !W% = &12345678 : REM ampersand (&) specifies hexadecimal > PRINT ~?W%, ~W%?3 : REM tilde (~) prints in hexadecimal 78 12 </syntaxhighlight> Strings of text could be PEEKed and POKEd in a similar way using the [[Dollar sign]] ($). The end of the string is marked with the [[Carriage return]] character (&0D in [[ASCII]]); when read back, this terminating character is not returned. Offsets cannot be used with the dollar sign. <syntaxhighlight lang="bbcbasic"> > 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, excluding &0D terminator, and starting at S% + 6 bytes PIES </syntaxhighlight> =={{anchor|DPEEK|DPOKE|DEEK|DOKE}}16 and 32-bit versions== As most early [[home computer]]s 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 as {{code|2=basic|PEEK(A)+256*PEEK(A+1)}} to read a [[Endianness|little-endian]] 16-bit integer at address A, and {{code|2=basic|POKE A,V}} followed by {{code|2=basic|POKE A+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 the [[Atari 8-bit computers]] uses a "D" (for "double") prefix: <code>DPEEK</code> and <code>DPOKE</code>. The East-German "Kleincomputer" KC85/1 and KC87 calls them <code>DEEK</code> and <code>DOKE</code>.<ref name="Kühnel_1987_Kleincomputer">{{cite book |editor-first1=Rainer |editor-last1=Erlekampf |editor-first2=Hans-Joachim |editor-last2=Mönk |author-first=Claus |author-last=Kühnel |pages=218, 232, 236 |title=Mikroelektronik in der Amateurpraxis |trans-title=Micro-electronics for the practical amateur |chapter=4. Kleincomputer - Eigenschaften und Möglichkeiten |trans-chapter=4. Microcomputer - Properties and possibilities |publisher={{ill|Militärverlag der Deutschen Demokratischen Republik|de}}, Leipzig |location=Berlin |date=1987 |orig-year=1986 |edition=1 |volume=3 |language=de |isbn=3-327-00357-2 |id=7469332}}</ref> The [[Sinclair QL]] has <code>PEEK_W</code> and <code>POKE_W</code> for 16-bit values and <code>PEEK_L</code> and <code>POKE_L</code> 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 line <code>peekpoke</code> <ref>peekpoke https://github.com/apritzel/peekpoke</ref> utility has been developed mainly for ARM based single board computers. <code>peekpoke</code> 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. ==POKEs as cheats== 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 to [[Cheating in video games#Memory editing|cheat]], getting an unlimited number of lives, immunity, invisibility, etc. Such modifications were performed using POKE statements. The [[Commodore 64]], [[ZX Spectrum]] and [[Amstrad CPC]] also allowed players with one of the relevant cartridges (such as [[Action Replay]] or [[Multiface]]) to freeze the running program, enter POKEs, and resume. For example, in ''[[Knight Lore]]'' for the [[ZX Spectrum]], immunity can be achieved with the following command:<ref>{{cite book | title = 20 Goto 10: 10101001 facts about retro computers | author = Steven Goodwin | year=2023 | isbn=978-1800182745}}</ref> <syntaxhighlight lang="qbasic">POKE 47196,201</syntaxhighlight> In this case, the value 201 corresponds to a [[Return statement|RET instruction]],<ref>{{cite book | title = Sinclair ZX Spectrum BASIC Programming | page=197 | year = 1982 | author1=Steven Vickers | author2=Robin Bradbeer | url = https://archive.org/details/spectrum-48-k-manual-original-manual-de-programacion/page/186/mode/2up}}</ref> so that the game returns from a subroutine early before triggering [[Collision_detection#Video_games|collision detection]]. Magazines such as ''[[Your 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.<ref>See for example, {{cite magazine | magazine = Your Sinclair | issue = 66 | date = June 1991 | title = Pokerama | url = https://archive.org/details/Your_Sinclair_066/page/n7/mode/2up}}</ref> 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 enforce [[virtual memory]] protection schemes to deny external program access to non-shared memory (for example, separate [[page table]]s for each application, hence inaccessible memory spaces). ==Generic usage of POKE== "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 the [[8-bit]] [[microcomputer]]s of the late 1970s and early 1980s. BASIC was often the only language available on those machines (on [[home computers]], usually present in [[read-only memory|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 [[assembly language|assembler]]. <!-- Note: Link to assembly language here instead of assembler, since the former article is a better one for explaining the difference between machine code and assembly. --Wernher --> An example of the generic usage of POKE and PEEK is in [[Visual Basic for Windows]], where [[Dynamic Data Exchange|DDE]] can be achieved with the '''LinkPoke''' keyword. ==See also== * [[Killer poke]] * [[Type-in program]] * [[Self-modifying code]] * [[Pointer (computer programming)]] == References == {{reflist}} {{Authority control}} [[Category:Microcomputer software]] [[Category:BASIC commands]] [[Category:Cheating in video games]] [[Category:Computer memory]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Anchor
(
edit
)
Template:Authority control
(
edit
)
Template:Citation needed
(
edit
)
Template:Cite book
(
edit
)
Template:Cite magazine
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:For
(
edit
)
Template:Refimprove
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)