ArticleslgStudy

computer science

PEEK and POKE

PEEK and POKE is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand PEEK and POKE rather than just read about it. In short: 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.

PEEK and POKE — main illustration
PEEK and POKE — illustration

Key takeaways

  • PEEK and POKE belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect PEEK and POKE to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of PEEK and POKE from memory before moving on to harder problems.

Reference excerpt

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. 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 and COMAL also have these commands. These commands are comparable in their roles to pointers in the 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. 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):

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:

A similar example from the Atari 8-bit computers tells the ANTIC display driver to turn all text upside-down:

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.

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

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:

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.

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 little-endian 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. BASIC XL for the Atari 8-bit computers 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 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 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.

INP and OUT Computers based on 8080, Z80, and 8086 processors have two address spaces: memory and ports. The INP function and OUT command allow reading and writing to the input/output port space. They are usually invoked as follows, either at the BASIC prompt or as part of a program:

The portnum and value parameters may contain expressions, as long as the evaluated expressions correspond to valid port numbers or values, respectively.

… excerpt ends here. Continue reading the full article.

Illustrations

PEEK and POKE: PEEK and POKE in line 100 of a Commodore Basic program on a CBM 3016
PEEK and POKE in line 100 of a Commodore Basic program on a CBM 3016

Worked examples

Example 1 — a first encounter with PEEK and POKE

Start with the simplest possible case. Write down what PEEK and POKE claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to PEEK and POKE before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about PEEK and POKE ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of PEEK and POKE

In research
PEEK and POKE appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses PEEK and POKE in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
PEEK and POKE is common in secondary-school and first-year university syllabi. It links to neighbouring topics BASIC commands, Cheating in video games, Computer memory, so understanding it makes those chapters shorter.
In everyday life
Look for PEEK and POKE outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “PEEK and POKE” →

How to study PEEK and POKE in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what PEEK and POKE means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain PEEK and POKE out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is PEEK and POKE in simple terms?

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.

Why does PEEK and POKE matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study PEEK and POKE?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on PEEK and POKE.

Tags

  • BASIC commands
  • Cheating in video games
  • Computer memory
  • Microcomputer software

Keep exploring