Understanding and Using PEEK and POKE

See all articles from Sinc-Link v1 n2
Author(s): Stan Piotrowski
Publication: Sinc-Link
Pub Details: Sinc-Link v1 n2 (Volume 1, Issue 2)
Pages: 6-10

All the memory used by computers is composed of numbers which are called addresses which is the same as line numbers in Basic. In the ROM memory, the addresses used by the computer are from 0 to about 8100. From 8192 to 16383 is the “Ghost area” of the ZX-81 where there is no memory. From 16383 to 16513 is the memory used by the computer to keep track of how much memory your computer has, or how many lines you are able to use on the screen, etc. (your manual gives specific uses of these addresses). From 16514 to 32768 (for 16K RAM packs) is the available memory to be used by you in your programs. The ROM memory addreses are “burned” in with their specific numbers that tell the computer how to print the letter “A” for example and as such cannot be changed by you. The system variables that uses addresses 16383 to 16513 can, in some instances, be changed by you without any crashes.

10 PRINT PEEK 16388
20 PRINT PEEP 16389
RUN & ENTER

The first number on the screen will be 0 and the second number will be 128. Without going into full detail on machine code, in some instances the addresses hold an arithmetic number (as opposed to a number that is a machine code command). The peculiar thing about 2 addresses that have numbers to be used in arithmetic form, is the way they hold them. The greatest number that any address can hold is 255 and the lowest is 0. Therefore, any address can hold one number from 0 to 255 or 256 possible numbers (0 is a number). 256 is not very high so the computer sientist enable us to count higher by using the combination of 2 addresses – the first address would contain a number from 0 to 256 and likewise the second address. But, the second address with a number would be multiplied by 256 and added to the first number. In the above program 16388 contains 0 (the first address contains the “Low” byte) and the second address (the “High” byte) contains 128. Therefore, 256*128 = 32768 + 0 = 32768. This is the last address for RAMTOP. In some programs you are asked to change Ramtop by lowering it so that you can place machine code routines above it. These are the addresses used by the computer to keep track of Ramtop so by POKEing different values into these addresses you “fool” the computer into thinking there is only room in memory from 16514 to the address you POKEd, for Basic. (Only machine code can be entered above Ramtop). Therefore, if you want to reserve 1000 Bytes above Ramtop for machine code routines you can work it out mathematically:

1.  32768 - 1000            :obtain value of new Ramtop
    = 31768
2.  INT (31768/256)         :value for address 16389 (remember
    = 124                    High byte second, low byte first)
3.  124*256 = 31744
4.  31768 - 31744           :value for address 16388
    = 24
Now, by direct command, POKE 16388,24
                        POKE 16389,124
                        New
                        ENTER

You have now reserved 1000 bytes above Ramtop for your machine code routines. To verify this: PRINT PEEK 16388 + 256 * PEEK 16389 and your answer will be 31768. As far as the computer is concerned the top of available memory is 31768.

Turn the computer off then on again and by direct command: PRINT PEEK 16388 – 256 * PEEK 16389 and the computer will now tell you the end of available memory (Ramtop) is at address 32768. By the way, 32768 + 16514 = approximately 16000 which is 16K RAM.

If you noticed in some commercial programs, the first line number (which may contain machine code) is the number “0”. In Basic you cannot enter this as a line number nor can you delete a line that is 0. You can alter the first line number or make your first line number a 0 by POKEing an address:

1 REM ZX-81 COMPUTER

BY DIRECT COMMAND: POKE 16510,0 then LIST 0. You will see that the first line number in your program is now a zero. If you POKE 16510,10 your first line number will be 10.

Another system variable address you can change with discretion is 16418. This address holds the number of lines you are able to print on. PRINT PEEK 16418 and your answer will be 2. When the computer is first turned on, it places 2 at the above location which allows you to print or use line numbers 0 to 21 but you can use the full 24 lines (0 to 23) of screen. The number 2 is there so that there is room for scroll and INPUT commands and for space to give error code reports when your program stops, such as 9/100 which means STOP statement executed at line number 100. Whenever you think of the lines on the screen such as PRINT AT 10,0 you know that the computer will PRINT something about the centre of the screen counting down from the top of the screen: 0, 1, 2, etc. The number at address 16418 counts from the bottom where in this case it means you cannot print on the bottom 3 lines: 0, 1, 2. You can change this number anywhere from 0 to 24. e.g.

10  POKE 16418, 10
20 PRINT AT 18,6; "ZX-81 COMPUTER"
30 SCROLL
40 PRINT TAB 10; "HELLO"
50 GOTO 30
RUN and ENTER

Note where the SCROLLing begins.

To use the full 24 line screen yur program cannot contain a SCROLL or INPUT command or the program may crash.

10  POKE 16418,0
20 CLS
30 FOR I = 0 to 23
40 PRINT "32 inverse spaces"
50 PRINT AT I, (RND*31); "inverse period"
60 NEXT I
70 PRINT AT 10,14; "inverse<, inverse +, inverse>"
80 IF INKEY$ = "s" THEN GOTO 20
90 GOTO 80
RUN and ENTER

(You need more than 1K of RAM for this program).

Press “S” and the programs starts again.

So you can see that by POKEing different values into an address you can alter something (in the above cases they were all arithmetic values) and if you learn Machine Code Language you can alter commands or anything else (or you can cause the computer to crash if you are not careful).

In some programs you may see machine code routines which can enhance or make a program run faster. Typically, a machine code program can run from 20 to 50 times as fast. For example;

10  LET A=10       : in this basic program line, the number of bytes used is 18.
                    - 4 for the line number
                    - 1 for LET
                    - 1 for A
                    - 1 for =
                    -10 for 10 (5 bytes per number)
                    - 1 for the end-of-line

M/C: LD A, 10        - uses 2 bytes.  (This means Load A with 10 which is identical to the
                       above Basic line).

Not only does machine code use less bytes but because you are working directly with the CPU your program does not have to be interpreted from Basic to machine code and back to basic then check for syntax errors all of which are time consuming.

The following are 2 machine code loader programs that you can use to POKE machine code into a REM statement or above RAMTOP.

Decimal

10  LET X=16514         :if above RAMTOP then change this value to the first address above
RAMTOP value.
20 INPUT Y
30 POKE X, Y
40 SCROLL
50 PRINT X, Y
60 LET X=X+1
70 GOTO 20

ENTERING HEX NUMBERS

10  LET X=16514
20 INPUT Y$
30 POKE 16*CODE Y$ + CODE Y$(2) - 476
40 SCROLL
50 PRINT 16* CODE Y$ + CODE Y$ - 476
60 LET X = X+1
70 GOTO 20

These loader programs enable us to quickly POKE machine code routines instead of laboriously POKEing them separately such as:

POKE 16514,23
POKE 16515,82
POKE 16516,20
etc.

To see the difference in speed in Basic compared to Machine Code try these 2 routines.

BASIC               MACHINE CODE

10 PRINT "D" 1 REM 1234567 POKE 16514,62
20 GOTO 10 POKE 16515,53
POKE 16516,205
POKE 16517,8
POKE 16518,8
POKE 16519,24
POKE 16520,249

The following are short machine code routines placed as the first statement in your program.

All machine code routines in the following example are placed in REM statements.

Inverse of all characters on screen:

1  REM 12345678901234567890    (20 bytes placed in REM statement).

:enter the decimal loader with X=16514 then INPUT the following numbers;

42,12,64,6,23,43,35,126,254,118,32,3,16,248,201,198,128,119,24,242

(of course you do not enter the commas)

Delete the above loader program BUT NOT THE REM STATEMENT and enter the following example program:

10  PRINT AT 6,8; "ZX-81 COMPUTER"; AT 9,8; "INVERSE DEMO"
20 PAUSE 50
30 RAND USR 16514
40 PAUSE 50
50 RAND USR 16514
60 STOP

(RAND USR is the same as RUN AND ENTER in Basic).

Delete Lines 20, 30, 40, 50 and enter the following Lines:

20  FOR I = 1 TO 10
30 RAND USR 16514
40 NEXT I

You can see where you might be able to enhance a program that you made up by adding a program line of RAND USR 16514 to get the inverse and then to change it back use of the USR command again.

SCROLL ROUTINES

You will have noticed by now how slow the SCROLL command on the ZX-81 is and the longer the program, the longer it takes to print on the screen. Also, after you have printed and scrolled numerous times, it seems to take forever to clear the screen. In the ZX-81 there is no Basic command to Scroll down. The following are machine code routines for Scrolling up or down.

1  REM  (46 bytes such as 46 x X's.

Enter the Decimal Loader Program and INPUT the following numbers:

42, 12, 64, 229, 17, 33, 0, 25,  209, 1, 214, 2, 237, 176, 201, 42,  16, 64, 17, 67, 0,
237, 82, 229, 17, 33, 0, 237, 82, 209, 1, 181, 2, 237, 184, 42, 12, 64, 6, 32, 35, 54, 0,
16, 251, 201.

After entering the above numbers, delete the loader program leaving the REM statement.

Here is a short Basic program to use the scroll routines:

10  PRINT AT 10,10; "ZX-81 COMPUTER"
20 IF INKEY$="U" THEN RAND USR 16514
30 IF INKEY$="D" THEN RAND USE 16529
40 GOTO 20
RUN & ENTER

When you press U the Print statement will move Up and by press D the statement will move down.

To make further use of the Scroll routines in a program you could do the following:

Delete all the lines except your REM statement containing the machine code for the Scroll routines. Enter the following short Basic Program.

10  LET A=16514
20 LET B=16529
30 LET X=20 :the letters are assigned in order to save bytes in memory
40 LET Y=0 since a letter takes only 1 byte whereas each number takes 5
bytes; multiply that by the number of times you will be us-
ing the routines in a program and it will be a considerable
saving in bytes.
50 PRINT AT X,Y; "THIS IS A DEMONSTRATION IN"
60 RAND USR A
70 RAND USR A
80 FOR I = 1 TO 50
90 NEXT I
100 PRINT AT X, Y; "THE USE OF THE SCROLL ROUTINE"
110 FOR I = 1 TO 10
120 RAND USR A
130 NEXT I
140 STOP

Note the use of the PRINT AT statement. In the Basic Sinclair Scroll the statement will automotically print at the bottom of the screen but in machine code Scroll you must specify this.

Note also, lines 80 and 90. You could have used PAUSE 50 but the FOR/NEXT loop as used here is a short timing loop that does the same thing only it is a smoother transition instead of the jerky PAUSE.

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