PEEK and POKE

Last updated
PEEK and POKE in line 100 of a Commodore Basic program on a CBM 3016 Monitor Commodore CBM 3016 with BASIC program-0309.jpg
PEEK and POKE in line 100 of a Commodore Basic program on a CBM 3016

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 BASIC programming language, though some other languages such as Pascal and COMAL also have these commands. These commands are comparable in their roles to pointers in the C language and some other programming languages.

Contents

One of the earliest references to these commands in BASIC, if not the earliest, is in Altair BASIC. [4] 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):

integer_variable=PEEK(address)POKEaddress,value

The address and value parameters may contain 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, 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 registers of the CPU itself (which makes software implementations of powerful machine code monitors 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 VIC-II graphics chip, which will make the screen border turn black:

POKE53280,0

A similar example from the Atari 8-bit family tells the ANTIC 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 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:

>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 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 hexadecimal7812

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.

>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

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 POKEA,V followed by POKEA+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 family uses a "D" (for "double") prefix: DPEEK and DPOKE. The East-German "Kleincomputer" KC85/1 and KC87 calls them DEEK and DOKE. [5]

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 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 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:

POKE47196,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 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 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 8-bit microcomputers 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 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, where DDE can be achieved with the LinkPoke keyword.

See also

Related Research Articles

Applesoft BASIC is a dialect of Microsoft BASIC, developed by Marc McDonald and Ric Weiland, supplied with the Apple II series of computers. It supersedes Integer BASIC and is the BASIC in ROM in all Apple II series computers after the original Apple II model. It is also referred to as FP BASIC because of the Apple DOS command used to invoke it, instead of INT for Integer BASIC.

In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are commonly represented in a computer as a group of binary digits (bits). The size of the grouping varies so the set of integer sizes available varies between different types of computers. Computer hardware nearly always provides a way to represent a processor register or memory address as an integer.

Tiny BASIC is a family of dialects of the BASIC programming language that can fit into 4 or fewer KBs of memory. Tiny BASIC was designed in response to the open letter published by Bill Gates complaining about users pirating Altair BASIC, which sold for $150. Tiny BASIC was intended to be a completely free version of BASIC that would run on the same early microcomputers.

<span class="mw-page-title-main">Bally Astrocade</span>

The BallyAstrocade is a second-generation home video game console and simple computer system designed by a team at Midway, at that time the videogame division of Bally. It was originally announced as the "Bally Home Library Computer" in October 1977 and initially made available for mail order in December 1977. But due to production delays, the units were first released to stores in April 1978 and its branding changed to "Bally Professional Arcade". It was marketed only for a limited time before Bally decided to exit the market. The rights were later picked up by a third-party company, who re-released it and sold it until around 1984. The Astrocade is particularly notable for its very powerful graphics capabilities for the time of release, and for the difficulty in accessing those capabilities.

<span class="mw-page-title-main">Atari BASIC</span> Dialect of the BASIC programming language

Atari BASIC is an interpreter for the BASIC programming language that shipped with the Atari 8-bit family of 6502-based home computers. Unlike most American BASICs of the home computer era, Atari BASIC is not a derivative of Microsoft BASIC and differs in significant ways. It includes keywords for Atari-specific features and lacks support for string arrays, for example.

In computer architecture, 64-bit integers, memory addresses, or other data units are those that are 64 bits wide. Also, 64-bit central processing units (CPU) and arithmetic logic units (ALU) are those that are based on processor registers, address buses, or data buses of that size. A computer that uses such a processor is a 64-bit computer.

Integer BASIC is a BASIC interpreter written by Steve Wozniak for the Apple I and Apple II computers. Originally available on cassette for the Apple I in 1976, then included in ROM on the Apple II from its release in 1977, it was the first version of BASIC used by many early home computer owners.

Commodore BASIC, also known as PET BASIC or CBM-BASIC, is the dialect of the BASIC programming language used in Commodore International's 8-bit home computer line, stretching from the PET (1977) to the Commodore 128 (1985).

<span class="mw-page-title-main">Locomotive BASIC</span>

Locomotive Basic is a proprietary dialect of the BASIC programming language written by Locomotive Software on the Amstrad CPC and the later Locomotive BASIC-2 as a GEM application on the Amstrad PC1512 and 1640. It was the main descendant of Mallard BASIC, the interpreter for CP/M supplied with the Amstrad PCW.

Sinclair BASIC is a dialect of the programming language BASIC used in the 8-bit home computers from Sinclair Research, Timex Sinclair and Amstrad. The Sinclair BASIC interpreter was written by Nine Tiles Networks Ltd.

The Atari Microsoft BASIC and Atari Microsoft BASIC II variants of the 6502-version of Microsoft BASIC ported to the Atari 8-bit machines. The first version, released 1981, required 32 KB of RAM and was supplied on floppy disk. The second version, released the next year, had most of the code on a ROM cartridge with additional functions on an optional floppy.

BASIC-PLUS is an extended dialect of the BASIC programming language that was developed by Digital Equipment Corporation (DEC) for use on its RSTS/E time-sharing operating system for the PDP-11 series of 16-bit minicomputers in the early 1970s through the 1980s.

<span class="mw-page-title-main">TI BASIC (TI 99/4A)</span> Programming language for TI-99 home computers

TI BASIC is an ANSI-compliant interpreter for the BASIC programming language built into the 1979 Texas Instruments TI-99/4 home computer and its improved 1981 version, the TI-99/4A.

<span class="mw-page-title-main">Vilnius BASIC</span> Dialect of the BASIC programming language

Vilnius BASIC, sometimes known as BK BASIC, is a dialect of the BASIC programming language running on the Elektronika BK-0010-01/BK-0011M and UKNC computers. It was developed at Vilnius University, located in Lithuania which was a republic of the Soviet Union at the time.

MBASIC is the Microsoft BASIC implementation of BASIC for the CP/M operating system. MBASIC is a descendant of the original Altair BASIC interpreters that were among Microsoft's first products. MBASIC was one of the two versions of BASIC bundled with the Osborne 1 computer. The name "MBASIC" is derived from the disk file name MBASIC.COM of the BASIC interpreter.

Galaksija BASIC was the BASIC interpreter of the Galaksija build-it-yourself home computer from Yugoslavia. While being partially based on code taken from TRS-80 Level 1 BASIC, which the creator believed to have been a Microsoft BASIC, the extensive modifications of Galaksija BASIC—such as to include rudimentary array support, video generation code and generally improvements to the programming language—is said to have left not much more than flow-control and floating point code remaining from the original.

Systems Programming Language, often shortened to SPL but sometimes known as SPL/3000, was a procedurally-oriented programming language written by Hewlett-Packard for the HP 3000 minicomputer line and first introduced in 1972. SPL was used to write the HP 3000's primary operating system, Multi-Programming Executive (MPE). Similar languages on other platforms were generically referred to as system programming languages, confusing matters.

SCELBAL, short for SCientific ELementary BAsic Language, is a version of the BASIC programming language released in 1976 for the SCELBI and other early Intel 8008 and 8080-based microcomputers like the Mark-8. Later add-ons to the language included an extended math package and string handling. The original version required 8 kB of RAM, while the additions demanded at least 12 kB.

<span class="mw-page-title-main">BASIC interpreter</span> Interpreter that enables users to enter and run programs in the BASIC language

A BASIC interpreter is an interpreter that enables users to enter and run programs in the BASIC language and was, for the first part of the microcomputer era, the default application that computers would launch. Users were expected to use the BASIC interpreter to type in programs or to load programs from storage.

Acorn System BASIC and Atom BASIC are two closely related dialects of the BASIC programming language developed by Acorn Computers for their early microcomputers like the Acorn System 3 and Acorn Atom. Developed in-house, they have a number of significant idiosyncrasies compared to most BASIC dialects of the home computer era.

References

  1. "PEEK". Microsoft QuickBasic 4.5 Advisor. Microsoft. 1990. Archived from the original on 2011-05-16. Retrieved 2007-12-28.
  2. "POKE". Microsoft QuickBasic 4.5 Advisor. Microsoft. 1990. Archived from the original on 2011-05-16. Retrieved 2007-12-28.
  3. "What is the oldest reference to PEEK, POKE, and USR?". Archived from the original on 14 August 2020. Retrieved 15 August 2020.
  4. Altair 8800 BASIC Reference_Manual 1975, Page 68 of PDF
  5. Kühnel, Claus (1987) [1986]. "4. Kleincomputer - Eigenschaften und Möglichkeiten" [4. Microcomputer - Properties and possibilities]. In Erlekampf, Rainer; Mönk, Hans-Joachim (eds.). Mikroelektronik in der Amateurpraxis[Micro-electronics for the practical amateur] (in German). Vol. 3 (1 ed.). Berlin: Militärverlag der Deutschen Demokratischen Republik  [ de ], Leipzig. pp. 218, 232, 236. ISBN   3-327-00357-2. 7469332.
  6. peekpoke https://github.com/apritzel/peekpoke