POKE and PEEK

from Wikipedia, the free encyclopedia

POKE ( English to poke = stuck, poke) is an instruction of the BASIC programming language that is used to transfer a value specified as a number ( an octet or byte for 8-bit architectures ) to the specified memory address in the address space of the processor (the CPU ) save. Its counterpart is the PEEK interpreter function ( English to peek = look, peek, look up), which is used to read from an address; the read value is transferred to the BASIC program as a return value .

The POKE instruction can be used, for example, to manipulate the content of a memory cell in the computer's main memory . In addition, be with POKE and PEEK hardware control registers of peripheral devices of the computer for the BASIC program available unless the computer system with memory mapped I / O works.

The PEEK and POKE commands were introduced by Bill Gates and Paul Allen in 1975 in the Altair BASIC programming language .

function

The POKE command calls a routine of the BASIC interpreter , which carries out write access with the two parameters for address and value . The peek function evaluates its parameter as an address, carries out read access to this address and transfers the result as a return value to the BASIC program.

format
POKE Adresse, Wert
Wert = PEEK(Adresse)

The maximum value of the address depends on the width of the address bus . The maximum value depends on the width of the data bus . For 8-bit architectures, typical values ​​are 0 to 65535 for a 16-bit address and 0 to 255 for an 8-bit value. If the underlying computer system has gaps in its memory allocation and no addressing errors - exception handling is implemented, a POKE instruction to an unused address does nothing, while the corresponding PEEK function delivers an undefined return value.

use

In the era of home computers , the POKE command was often used, as this is the only way for BASIC programs on many systems to use certain graphics or sound capabilities of the computer, which are supported by the hardware used, but not by the im BASIC interpreter built-in commands are supported. The BASIC program receives access to the hardware control register and controls the hardware directly; a task that, according to contemporary terminology, would fall to a device driver .

example
POKE 53265, PEEK(53265) OR 32
Set by reading using PEEK, modifying using OR 32 and writing back using POKE Bit 5 (the one with the value 2 5  = 32) in register 17 of the peripheral module VIC II (the video display controller ) in a Commodore 64 ; there the VIC II (through memory mapped I / O ) has the base address D000 16  = 53248, register 17 is therefore at address 53248 + 17 = 53265. The set bit 5 of this register causes the bitmap graphics mode to be activated. The combination with OR has the effect that all read bits are written back unchanged - except for bit 5, which is set independently of the status. Analog to this, bit 5 with POKE 53265, NOT(PEEK(53265)) NOR 32or POKE 53265, PEEK(53265) AND NOT(32)erased state independently and with POKE 53265, NOT(PEEK(53265)) XNOR 32or POKE 53265, PEEK(53265) XAND NOT(32)state-dependent switch ( toggle be).

With POKE instructions, specific data of the operating system can be changed in a targeted manner in order to achieve effects desired by the user - for example changing the key repetition . Another example from this category for the PEEK function is to recognize the mere pressing of the Shift key (without another key) when the operating system does not offer any other means.

example
POKE 650, 128
Activate key repetition for all keys of a Commodore 64. At address 650 (a normal main memory address in the area of ​​the operating system management data) the keyboard query part of the operating system looks up how the key repetition should work. The value 128 causes all keys to be repeated; Alternatives are no key is repeated and only cursor and delete keys and space are repeated.
example
IF PEEK(653) AND 1 THEN …
Query of the shift indicator bit (bit 0 (with the value 2 0  = 1) of the byte at address 653) of the operating system keyboard query of a Commodore 64. The part after THEN is only executed when the shift key is currently pressed.

Another application is a BASIC program which uses a subroutine created in machine language for its task . Instead of loading this subroutine (sometimes only a few bytes) as a separate binary file from the mass storage (in the home computer era often simple cassette recorders or data sets ), the memory image of the subroutine is stored as a sequence of numbers in the BASIC program. During the initialization phase, the BASIC program writes this memory image in a loop using POKE instructions to the desired target memory addresses so that the subroutine can then be called there. It is also known to patch (modify) machine language programs using targeted POKE instructions after loading from the mass storage device and before the program is started - especially for cheating computer games. In the relevant scene, for example, cheats are known with which the player character is invulnerable or at least significantly more "life" is available. The combination of value and address is individual for each program or game (determined for example using reverse engineering methods ) and is sometimes referred to as a POKE itself (e.g. "POKE list" in the sense of listing cheats ).

In order to know as a programmer which combinations of addresses and values ​​fulfill a desired purpose, it is helpful to study documentation (operating instructions and data sheets) as well as other literature (including articles in specialist journals) on the peripheral components, the operating system, etc.

The use of POKE instructions often reduces the readability and comprehensibility of the program text; At the same time, it is the reason why such programs are hardly portable between different computer models and their different chipsets - POKE commands are often not transferable between different models or series of the same manufacturer. The examples above for keyboard control for the Commodore 64 can be transferred to the Commodore VC 20 , as the operating systems are very similar in this regard. The video display controllers of both computers differ not only in their position in the memory allocation plan, but also in their internal structure - here: there is no bitmap graphics mode on the VC 20 - so that the example for activating the bitmap graphics mode on the VC 20 does not Has correspondence.

Since administrative data of the operating system can also be overwritten with the POKE command, careless use often does not result in a proper error message , but in a computer crash  - with the potential for destruction of the program or the data in the memory. Unwanted optical or acoustic effects can also occur if errors occur in the programming of the hardware control register. At best, nothing happens. The Troubleshooting is greatly complicated by the effects mentioned.

Today's meaning

The use of POKE is obsolete on today's systems and using current BASIC variants. Today's CPUs only use the MMU for addressing , which abstracts the logical address from the physical one and prevents direct memory manipulation through special protection mechanisms ( memory protection violation ). In addition, with few exceptions, modern operating systems no longer have a static memory allocation plan in which certain content can always be found at the same addresses. Hardware resources are then not accessed via fixed addresses, but via device drivers , which in turn read and write to memory areas provided for this purpose.

Web links

POKE in the C64-Wiki (with application examples)

Individual evidence

  1. POKE. In: Microsoft QuickBasic 4.5 Advisor. Microsoft, 1990, accessed February 27, 2014 .
  2. PEEK. In: Microsoft QuickBasic 4.5 Advisor. Microsoft, 1990, accessed February 27, 2014 .
  3. N.Montfort, P.Baudoin, J.Bell, I.Bogot, J.Douglass and others. a. 10PRINT CHR $ (205.5 + RND (1)); : GOTO 10 Cambridge, MIT Press (2013) ISBN 978-0-262-01846-3 p. 171 ( Online. (PDF 50 MB) Accessed on February 26, 2014 (English). )
  4. POKE. In: C64 Wiki. Retrieved April 14, 2015 .
  5. Commodore 64 memory map. Retrieved April 16, 2015 .