Monday, March 20, 2006

Whatever happened to PEEK and POKE?

Sometimes I'm asked how to PEEK and POKE using Liberty BASIC. The short answer to this is, you can't. The long answer is more complicated.

The original home computers (like the classic Commodore 64, TRS-80, and Apple II models) were designed to be completely open to their owners. Most of the different parts of the computer like the sound, graphics, keyboard and joystick (there were no mice back then) were controlled by mapping them to different memory locations. So, the built-in commands didn't do everything you need? It was common to control the computer's equipment directly by sticking values into the memory locations that control that equipment, and reading the status back out. This was done with POKE and PEEK. This was a lot of fun, and usually useful too.

Since these early machines didn't multitask it was pretty safe to steal control of things away from the BASIC interpreter (which was a much operating system as there was). Then computers started running Windows, the Mac OS, Linux, etc. and allowed more than one program to run at a time. PEEK and POKE became problematic because if one program decided to mess with the screen for example, it might interfere with what other programs need to do their thing. Not only that, but computers today all have different kinds of hardware in them, so even if you could PEEK and POKE the exact memory locations would be different from machine to machine. So that's the bad news. :-(

The good news is that we can still have a power trip. How? Your operating system manages all the hardware for you behind operating system functions. These functions make every computer look more or less the same to the programmer. You can use these functions to do a lot of powerful things and most languages have a way to use them. For example Liberty BASIC programmers can use the CALLDLL command to make Windows API calls, which are the operating system calls of the Windows operating system. These are more complicated in general (and you can fill a whole bookshelf with information about them) than the old fashioned PEEK and POKE, but at least we aren't left without some way to pull rank on our computers. ;-)

So if you're missing the power and coolness of PEEK and POKE, why not try your hand at API calls?

Here is a useful link to get you started:


rich said...

Your post might explain why POKE(write to memory) is not available but why is PEEK(read memory) not available?
The link you provide has little to do with peek and poke but only api and dll with no hint as to how to apply them to peek and poke.

Carl Gundel said...

The post explains that today operating systems manage the resources of the computer. They provide API functions to do the cool things that BASIC programmers once used PEEK and POKE for. It is very unusual to need these old favorites for anything anymore.

Ratika said...

Hi, I would like to know about any Peek Basic programmer in Bangalore, India.

Kindly let me know.

andax said...

An application programming interface (API) is an interface implemented by a software program. This software program unfortunately is rarely public domain (or similiar licenced) software and even more confusing than the plain old PEEK, POKE and CALL commands. These have been documented in the computer manuals. Did you ever read in your new computers official manual documentation of a posixly correct API? I bet you did not.
Compared to PEEK and POKE, the API is one more idiosyncratic abstraction layer between the user and the computer. I see it as a result of agressive and neurotic marketing over years, combined with baffled money raking core developers, in times of global update-paranoia.