PEEK and POKE
This article needs additional citations for verification. (October 2015) |

In computing, PEEK and POKE are commands used in some high-level programming languages for accessing the contents of a specific memory cell referenced by its memory 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 with machine code monitors such as the DECsystem-10 monitor;[3] these commands are particularly associated with the
One of the earliest references to these commands in BASIC, if not the earliest, is in
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
integer_variable = PEEK(address)
POKE address, value
The address and value parameters may contain
Memory cells and hardware registers
The address locations that are POKEd or PEEKed at may refer either to ordinary memory cells or to
POKE 53280, 0
A similar example from the Atari 8-bit computers tells the ANTIC display driver to turn all text upside-down:
POKE 755, 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 is Mapping the Atari, which starts at location zero and mapped out the entire 64 kB memory of the Atari 8-bit systems location by location.
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 EXAM
and FILL
.[citation needed] There were also BASIC dialects that used the reserved words MEMR
and MEMW
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:
> 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 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 ($). 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.
> 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
16 and 32-bit versions
As most early home 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 as PEEK(A)+256*PEEK(A+1)
to read a 16-bit integer at address A, and POKE A,V
followed by 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.
DPEEK
and DPOKE
. The East-German "Kleincomputer" KC85/1 and KC87 calls them DEEK
and DOKE
.[5]The
A Linux command line peekpoke
[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.
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 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:[7]
POKE 47196,201
In this case, the value 201 corresponds to a RET instruction,[8] so that the game returns from a subroutine early before triggering 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.[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 enforce virtual memory protection schemes to deny external program access to non-shared memory (for example, separate page tables 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
An example of the generic usage of POKE and PEEK is in
See also
References
- ^ "PEEK". Microsoft QuickBasic 4.5 Advisor. Microsoft. 1990. Archived from the original on 2011-05-16. Retrieved 2007-12-28.
- ^ "POKE". Microsoft QuickBasic 4.5 Advisor. Microsoft. 1990. Archived from the original on 2011-05-16. Retrieved 2007-12-28.
- ^ "What is the oldest reference to PEEK, POKE, and USR?". Archived from the original on 14 August 2020. Retrieved 15 August 2020.
- ^ Altair 8800 BASIC Reference_Manual 1975, Page 68 of PDF
- ISBN 3-327-00357-2. 7469332.
- ^ peekpoke https://github.com/apritzel/peekpoke
- ISBN 978-1800182745.
- ^ Steven Vickers; Robin Bradbeer (1982). Sinclair ZX Spectrum BASIC Programming. p. 197.
- ^ See for example, "Pokerama". Your Sinclair. No. 66. June 1991.
{{cite magazine}}
: CS1 maint: year (link)