PEEK and POKE
   HOME

TheInfoList



OR:

In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes, and development of both hardware and software. Computing has scientific, ...
, PEEK and POKE are commands used in some
high-level programming language In computer science, a high-level programming language is a programming language with strong abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be easier to u ...
s for accessing the contents of a specific memory cell referenced by its memory address. PEEK gets the byte located at the specified memory address. POKE sets the memory byte at the specified address. These commands originated with machine code monitors such as the DECsystem-10 monitor; these commands are particularly associated with the BASIC programming language, though some other languages such as
Pascal Pascal, Pascal's or PASCAL may refer to: People and fictional characters * Pascal (given name), including a list of people with the name * Pascal (surname), including a list of people and fictional characters with the name ** Blaise Pascal, Frenc ...
and COMAL also have these commands. These commands are comparable in their roles to pointers in the
C language C (''pronounced like the letter c'') is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities ...
and some other programming languages. One of the earliest references to these commands in BASIC, if not the earliest, is in Altair BASIC.Altair 8800 BASIC Reference_Manual 1975
Page 68 of PDF The PEEK and POKE commands were conceived in early personal computing systems to serve a variety of purposes, especially for modifying special memory-mapped hardware 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 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 prompt) or in indirect mode (as part of a
program Program, programme, programmer, or programming may refer to: Business and management * Program management, the process of managing several related projects * Time management * Program, a part of planning Arts and entertainment Audio * Programm ...
): integer_variable = PEEK(address) POKE address, value The ''address'' and ''value'' parameters may contain complex expressions, 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 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. For software programs to save and retrieve s ...
, 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 hardware registers of I/O units or support chips such as sound chips and video graphics chips, or even to memory-mapped
register Register or registration may refer to: Arts entertainment, and media Music * Register (music), the relative "height" or range of a note, melody, part, instrument, etc. * ''Register'', a 2017 album by Travis Miller * Registration (organ), th ...
s of the CPU itself (which makes software implementations of powerful machine code monitors and
debugging In computer programming and software development, debugging is the process of finding and resolving '' bugs'' (defects or problems that prevent correct operation) within computer programs, software, or systems. Debugging tactics can involve i ...
/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 VIC-II graphics chip, which will make the screen border turn black: POKE 53280, 0 A similar example from the
Atari 8-bit family The Atari 8-bit family is a series of 8-bit home computers introduced by Atari, Inc. in 1979 as the Atari 400 and Atari 800. The series was successively upgraded to Atari 1200XL , Atari 600XL, Atari 800XL, Atari 65XE, Atari 130XE, Atari 800XE, ...
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 ''Mapping the Atari'', written by Ian Chadwick and published by COMPUTE! Publications in 1983, is a location-by-location explanation of the memory layout of the Atari 8-bit family of home computers. The introduction is by Optimized Systems Sof ...
'', 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 An operating system (OS) is system software that manages computer hardware, software resources, and provides common daemon (computing), services for computer programs. Time-sharing operating systems scheduler (computing), schedule tasks for ef ...
. Concerned about possible legal issues, they renamed the commands EXAM and FILL. There were also BASIC dialects that used the reserved words MEMW and MEMR 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 The question mark (also known as interrogation point, query, or eroteme in journalism) is a punctuation mark that indicates an interrogative clause or phrase in many languages. History In the fifth century, Syriac Bible manuscripts used ...
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 ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because ...
); 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 to read a 16-bit integer at address A, and followed by and 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 Optimized Systems Software (OSS) was a company that produced disk operating systems, programming languages with integrated development environments, and applications primarily for the Atari 8-bit family of home computers. OSS was best known f ...
for the
Atari 8-bit family The Atari 8-bit family is a series of 8-bit home computers introduced by Atari, Inc. in 1979 as the Atari 400 and Atari 800. The series was successively upgraded to Atari 1200XL , Atari 600XL, Atari 800XL, Atari 65XE, Atari 130XE, Atari 800XE, ...
uses a "D" (for "double") prefix: DPEEK and DPOKE The East-German "Kleincomputer" KC85/1 and KC87 calls them DEEK and DOKE. The Sinclair QL has PEEK_W and POKE_W for 16-bit values and PEEK_L and POKE_L for 32-bit values.
ST BASIC Atari ST BASIC (or ST Basic) was the first dialect of BASIC that was produced for the Atari ST line of computers. This BASIC interpreter was bundled with all new STs in the early years of the ST's lifespan, and quickly became the standard BASIC for ...
for the Atari ST uses the traditional names but allows defining 8/16/32 bit memory segments and addresses that determine the size.


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 the relevant cartridges or
Multiface The Multiface was a hardware peripheral released by Romantic Robot for several 1980s home computers. The primary function of the device was to dump the computer's memory to external storage. Pressing a red button on the Multiface activated it. A ...
add-on 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: 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 ''
Your Sinclair ''Your Sinclair'', or ''YS'' as it was commonly abbreviated, was a commercially published and printed British computer magazine for the Sinclair range of computers, mainly the ZX Spectrum. It was in circulation between 1984 and 1993. History Th ...
'' 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 enforce
virtual memory In computing, virtual memory, or virtual storage is a memory management technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which "creates the illusion to users of a very ...
protection schemes to deny external program access to non-shared memory (for example, separate
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 used by the program executed by the accessing Process ( ...
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 In computer architecture, 8-bit integers or other data units are those that are 8 bits wide (1 octet). Also, 8-bit central processing unit (CPU) and arithmetic logic unit (ALU) architectures are those that are based on registers or data buses ...
microcomputer A microcomputer is a small, relatively inexpensive computer having a central processing unit (CPU) made out of a microprocessor. The computer also includes memory and input/output (I/O) circuitry together mounted on a printed circuit board (PC ...
s of the late 1970s and early 1980s. BASIC was often the only language available on those machines (on home computers, usually present in ROM), and therefore the obvious, and simplest, way to program in
machine language In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a ver ...
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 assembler. An example of the generic usage of POKE and PEEK is in
Visual Basic for Windows The original Visual Basic (also referred to as Classic Visual Basic) is a third-generation event-driven programming language from Microsoft known for its Component Object Model (COM) programming model first released in 1991 and declared leg ...
, where DDE can be achieved with the LinkPoke keyword.


See also

* Killer poke * Type-in program * Self-modifying code * Pointer (computer programming)


References

{{Authority control Microcomputer software BASIC commands Cheating in video games Computer memory