6
Advanced Techniques
PEEK and POKE Alternatives
Jerry White
This tutorial shows a quick and easy way to select random numbers using PEEK and POKE to increase speed. The technique is also demonstrated as an alternative to the SOUND command.

When writing a BASIC program, it is often necessary to find the fastest possible method to achieve a desired result. When speed is important, a machine language subroutine is usually the best alternative. In many cases, however, using PEEK and POKE instructions instead of conventional routines can significantly increase the speed.

In each of the four example routines below, RAM location 540 is used as a timer. The term jiffy is used to denote 1/60 second. Location 540 counts backwards until it reaches zero. When the number 255 is POKEd into this location, it will take 4¼ seconds to count back to zero.

Each routine begins with a GRAPHICS 0 command to clear the screen. You might want to try mode 2 later on to see how the elapsed time of each routine is affected. Standard text mode was chosen so the routines could be listed on the screen and the elapsed time displayed.

Time tests 1 and 2 show two ways to select a random number between 0 and 255. The first method is the conventional way. For demonstration purposes, the random number was selected ten times.

The second listing provides an alternative method which is four times faster. Our number is selected with a PEEK at location 20. This is also a jiffy counter, but unlike location 540, this one counts forward until it reaches 255. It is then reset to 0 and continues counting normally. This method of selection is only useful when a single random number is required. For example, to return a decision on a 50 percent probability, check location 20 for less than, or for equal to, 127. This method would not be effective if more than one number is needed within a short period of time. It is, however, an excellent alternative in most cases, and is much faster the conventional method because the multiplication is eliminated.

To obtain a truly random integer between 0 and 255, PEEK location 53770. Try the following one-line program to see the random number generator in action:

10 ? PEEK(53770):GOTO 10

Time test routines 3 and 4 loop through the 256 pitches of Atari's undistorted sound. Test 3 uses the conventional SOUND The execution time was 123 jiffies, or just over two seconds. Test 4 uses the POKE command. The difference was 17/60 second.

There are many situations where the PEEK and POKE commands can be used to speed up your BASIC programs. There are also things that could not be done at all in Atari BASIC were it not for and POKE.

Atari BASIC Time Test 1
5 GRAPHICS 0:LIST 
10 POKE 540,255:FOR TEST=1 TO 10:X=RND(0)*256:NEXT TEST:TIME=PEEK(540)
20 PRINT :PRINT "TIME=";255-TIME;" 60th of a second."
TIME=16 60ths of a second

Listing. Atari BASIC Time Test 1.
Download (Saved BASIC) / Download (Listed BASIC)

Atari BASIC Time Test 2
5 GRAPHICS 0:LIST 
10 POKE 540,255:FOR TEST=1 TO 10:X=PEEK(20):NEXT TEST:TIME=PEEK(540)
20 PRINT :PRINT "TIME=";255-TIME;" 60th of a second."
TIME=4 60ths of a second

Listing. Atari BASIC Time Test 2.
Download (Saved BASIC) / Download (Listed BASIC)

Atari BASIC Time Test 3
5 GRAPHICS 0:LIST 
10 POKE 540,255:FOR TEST=0 TO 255:SOUND 0,TEST,10,2:NEXT TEST:TIME=PEEK(540)
20 PRINT :PRINT "TIME=";255-TIME;" 60th of a second."
TIME=123 60ths of a second

Listing. Atari BASIC Time Test 3.
Download (Saved BASIC) / Download (Listed BASIC)

Atari BASIC Time Test 4
5 GRAPHICS 0:LIST :SOUND 0,0,0,0:POKE 53761,162
10 POKE 540,255:FOR TEST=0 TO 255:POKE 53760,TEST:NEXT TEST:TIME=PEEK(540)
20 PRINT :PRINT "TIME=";255-TIME;" 60th of a second."
TIME=106 60ths of a second

Listing. Atari BASIC Time Test 4.
Download (Saved BASIC) / Download (Listed BASIC)


Return to Table of Contents | Previous Section | Next Section