Classic Computer Magazine Archive ANTIC VOL. 2, NO. 1 / APRIL 1983

Voyeur

The Case of the Homely Cousin

by Mark Grice

My students in BASIC programming often ask, "Why do some programmers love to use POKEs and PEEKs?"

It is a valid question, especially considering the ATARI computer. Why use a POKE for your color instead of SETCOLOR? It seems to be unnecessary work. It is not, however. There are several good reasons why a programmer might use POKEs and PEEKs.

To understand the whys and wherefores, let us consider memory. Memory is a lot like money: first, you can never have enough . . . second, it is hard to come buy . . . and third, it is wise to use it sparingly . . . and last, the more you have, the more you want!

The sad truth, friends, is that SETCOLOR and SOUND commands eat up memory. Why? Well, let's look at the BASIC process . . .

First, it is important to understand that BASIC is NOT a compiler, it is an interpreter. Your computer, like all computers, understands only one language . . . machine language. That's it. Period. It is quite incapable of ever learning any other language. The unfortunate part of all this is that humans are not well-equipped to work with machine language. And so we have a stalemate .

Enter the interpreter, in our case, named BASIC. A wonderful person, our interpreter, he understands machine language perfectly; and he almost understands English. At least he understands enough so that it is possible to talk to him if we learn BASIC's language. Then, away we go, we give BASIC a command, he breaks it down into machine-language commands, hands it to the computer, the computer rushes about to do its duty, tells BASIC the result, and BASIC relays the progress report to the programmer. Stupendous! But slow.

Where is all of this leading? You guessed it, POKE and PEEK use less memory, and execute faster than do SETCOLOR and SOUND, etc. Ergo, the first reason for using POKE and PEEK is that it is faster. The second is that it requires less memory. And the third . . . because there are times when there is simply no other choice . . .

I like to think of my computer as being a lot like me. I have a notorious reputation for being absent minded. In fact, as I sit here writing, I see that the biggest item in the room is my "external memory board," a 4' x 6' White Marker Board that I jot reminders on. Whenever someone calls be about something, I write it on my memory board. Then, religiously before I leave at night, I look at the memory board to see what I'm forgetting.

Let's take a fantastic example. I am hard at work, and the phone rings. I answer, and on the phone is the sexy voice of this gorgeous girl I met at a computer convention. She is in town at a hotel, and asks me if I could get away and meet her somewhere. Naturally, I tell her that I am busy at the moment, but that I will get away when I can. She gives me her hotel's name, and her room number, and hangs up.

Immediately, I go to the white board, and jot down the information. Just then, I hear my door buzzer. I go to the door and find the UPS man there. While I am busy, my brother sneaks into the room behind me, goes to the white board, and changes the precious information that I had put there.

At the end of the day, I turn off my computer (a smile playing about my lips), look at the white board and get the information: Holiday Inn; room 234. Of course when I get there, I find not my lovely out-of-town visitor, but rather my sister-in-law's homely cousin that she has been trying to fix me up with for the last eight months!

This rather bizarre example is similar to what happens when the computer first boots up. The term "Boot" is derived from the phrase "Pulling yourself up by your own bootstrap". When it first boots up, the computer stores certain values in parts of its external memory board, much the same as I write important information on my white board. As it goes about its duty, it will look into these "memory locations" and get the information that it needs. For example, 60 times each second, it looks into memory location 710, takes the value it finds there and produces a color for the background in Graphics Mode 0. Suppose you are in a program, and the screen is black with white letters. You know that the area in memory that the computer refers to get the color for this screen is location 710. So what's the numerical value that is equivalent to black? Take a PEEK. It is done like this:

PRINT PEEK(710)

The computer will now look into location 710, see what is there, and print the result. In our example, it will print a 0, since that is the value of black. There is something else rather nifty about this ... you don't ever have to know what the value of a location is in order to use it.

Suppose you did this:

A=PEEK(710)

What good does that do? Well, now you can keep the value for future reference, so that we can restore the original value back to the location when we are done playing around. More on that later.

Okay, so the PEEK command is the "voyeur" of the computer world. What is POKE?

In my example of the sexy out of town visitor, the part of POKE was played by my brother. POKE is the wise-guy who sneaks in when the computer is not looking and changes the value. Turn on your computer, and notice the screen. It is blue. Type in:

POKE 710,0

No doubt all of you scholars out there knew that the screen was going to change to black. Those of you who didn't, take heed. When you type in a POKE command, you are telling the memory (in your best Edgar G. Robinson voice) "All right, you listen, and listen good. I don't care WHAT value WAS in that location, now it's a 0, seee, and if you don't like it ... tough ! "

In less extravagant terms, you are taking a value of 0 and POKING it into that location, thus changing it. When the computer checks that location it grabs the new value and acts accordingly. You may wonder why the computer neither knows, nor cares, what values are stored. It is too busy carrying out its instructions to be bothered (except for hardware registers and their shadows, which we will ignore for the moment).

What happens if I POKE a value there that shouldn't be there? Maybe nothing, or maybe you'll have one very confused little computer.

What happens if I POKE something into a location that the computer never checks? Nothing. Nothing at all. That is why, although you have 48,000 bytes of RAM, you only have a couple hundred memory locations that do anything. All that matters is what the Operating System and company check regularly. To use the previous example, if my brother had changed the information on a scrap of paper that I never bothered to look at, his practical joke wouldn't have succeeded.

Enough of theory. Let's try some examples.

FOR X=1 TO 125:POKE 710,X:NEXT X

I am assuming that we all know what a FOR-NEXT loop is and what it does. This little ditty changes the value in location 710 one hundred and twenty-five times, and, I might add, it does it Pretty Darn Quick.

Remember what I said about storing a value without looking at it and using it later? The time has come to explain that. Hit [SYSTEM RESET] and try this:

10 A=PEEK(710)
20 FOR X=1 TO 125:POKE 710,X:NEXT X
30 POKE 710,A

This I call the "Brings Us Back To Doe" program. Notice what happens . . . We peek into location 710, and store that value in what we call A. Then we change the value of location 710, (and hence the color of the screen) one hundred twenty-five times, and when we're finished, we have the decency and foresight to put everything back the way we found it.

I have been asked in my classes, "Why doesn't the value of A change when the value of 710 changes?" Because, we are setting A up before, not during, the change. Let us say that there is a 0 in location 710. In line 10, we say, A is equal to whatever is in 710. So the computer says, "Oh, A is equal to 0." THEN we change the value in location 710. But we never go back and change the value of A. It remains the same. Understand, PEEK is not a dynamic process. It does not continue to happen once you call for it. It only happens when you tell it to.

I understand that this is not the most accurate dissertation on POKEs and PEEKs, but I hope that it may have helped some of you understand it better.