PEEK, POKE & CHR$ for Beginners (4K)

Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n6 (Volume 2, Issue 6)
Date: June 1981
Pages: 14-15

Most of the BASIC commands on your ZX80/MicroAce are pretty clear–SAVE, LOAD, GO TO and RUN do just what they say they do. PEEK and POKE are a little tougher to understand, but as you’ll see, they also do just what they say they do. CHR$ just translates the computer’s character codes into the characters themselves.

As we found last month, your computer stores everything in memory locations, named by unique addresses. The ZX80 requires an address to understand a PEEK command, so all PEEK commands are of the form: PEEK(address). This tells the computer to look in the specified address. Adding a PRINT command before PEEK will make it display what it finds there.

Typing in PRINT PEEK(address) (NL) as an immediate mode command (without a line number) so the machine executes it right away, gives an answer on the screen that is a number from 0 to 255. Your computer stores characters in 8 bits, or 1 byte, so 255 is the largest number it can hold in 1 location. These numbers correspond to the character code set found on pp.75-78 of the ZX80 4K manual and pp.55-56 of the MicroAce manual. For example, if the answer is 38, that is the character representation in the address you PEEKed. If you are in a BASIC area, the character stored at that address is A. If it’s a machine language area, 38 will be a ML command or data. PEEK looks to see what’s in the computer’s memory at a given address.

To try this out, clear your computer by typing NEW (NL) and type 9876 PRINT A*B. PEEK addresses in the immediate mode in order starting with the first address for BASIC programs, 16424. (Type with no line number: PRINT PEEK(16424), then PRINT PEEK (16425), and so on.) Don’t start with the first RAM address, 16384, because you will see system variables which won’t make any sense to you. Look up the answers you get in the character code table. They will translate to PRINT A*B because you PEEKed the beginning of memory where that line resides. The first 2 bytes, or addresses, won’t translate to the line number for reasons explained below.

The computer can also translate the character code for you. The BASIC command CHR$ will change the code into a character according to the character code table when you write it in the form CHR$(number) and the number is in the range 0-255.

Putting these 2 ideas together, let’s write a program to look into memory and tell us what character, token or keyword is there. Set up a FOR-NEXT loop to increment from the starting address as many as you would like to see (10 in the example below). Then use the CHR$ command to change the PEEKed code into characters and keywords:

10 FOR I=0 TO 10
20 LET S=PEEK(16424+I)
30 PRINT 16424+I,S,CHR$(S)
40 NEXT I

Line 30 prints each address, character code and code translation. The commas separate the answers into columns for easy reading. You’ll see code where line numbers should be, but they won’t form the line numbers. This is because the ZX80 stores line numbers as pure binary code, not as character codes. When it tries to translate the binary, it comes up with garbage using CHR$. To translate them yourself, multiply the first byte’s code by 256 and add the second. In our program, the first 2 codes are 0 and 10, and (0x256)+10=10, or our first line number. Change all the line numbers to 1000s (1000, 2000, etc.) and the first 2 codes are 3 and 232. (3×256)+232=1000. Try this for different line numbers.

POKE, on the other hand, puts something into an address. POKE commands are of the form: POKE address,number. The address is where you want the computer to put the number after the comma. This number should also be in the range 0-255 to represent character code or machine language code.

To our original PEEK program, now add:

50 POKE 16426,10
60 PRINT PEEK(16426),CHR$(PEEK(16426))

The computer prints the same information as before. Line 50 puts a new character code into the first address of the program after the line number (all line numbers use up 2 addresses). Line 60 prints what the computer now finds at that address and what character that code translates into. When you go back to the program listing, you’ll see that the translation for 10 (a graphics character) has replaced the first word, FOR, of our first BASIC statement because you put that value into that location. This trick can put characters unavailable from the keyboard into a program. Our Big Characters program (Dec. 80) used this fact to insert the graphics characters to form the big letters (some of which aren’t available from the keyboard) into the program in a specific order. To do this yourself, you need to know the first location of the characters you want to insert and begin poking new codes from there on.

POKE commands also allow you to enter machine language codes from BASIC into consecutive memory locations. Because your computer only understands ML commands and data in context, poking a value into a single byte will probably not get you anywhere. Poking a series of values can form a program, as we saw in our analysis of Sinclair‘s available memory program (May 81).

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.