Chapter 9: Using PEEK and POKE
|
9.1 |
Introduction
In most cases you'll never need to use PEEK or POKE, but in those special cases where you do, this chapter will guide you through it. This is considered somewhat advanced stuff, so if you don't feel comfortable, skip this chapter. |
9.2 |
What does PEEK and POKE do?
PEEK and POKE are used to modify or read memory. I/O devices are assigned to specific memory addresses, so as you can see, PEEK or POKE can be very dangerous as well as useful. You can use PEEK and POKE to read the status of a printer, write to video memory, and about anything else you can think of. Use POKE when writing to memory, and PEEK when reading from memory. |
9.3 |
DEF SEG does what?
Preceding any PEEK or POKE statements should be a DEG SEG which defines the segment from which to read/write. You only need to define DEF SEG once, unless of course you plan to change the segment. Once you have the segment, you can read/write to an offset, this is probably the most confusing aspect. To help you understand, let's take a look at the most common example, that of screen reads/writes:
SCREEN 0
DEF SEG = &HB800
POKE 100, ASC("A")
A$ = CHR$(PEEK(100)) You can run the example in QBasic to see what you get. If all goes well, you'll see an A on the top of the screen. Since each segment is 64K, you can POKE/PEEK from 0-65535, but as you can verify, POKE 65535, ASC("A") in the above example would serve no purpose. You'll probably notice that &HB800 is the segment for Screen 0 (color). So all we've done is POKE to offset 100, the ascii value "A" and read the value (PEEK) to A$
|
9.4 |
What about BLOAD/BSAVE?
I'll cover this in another chapter, but BLOAD/BSAVE is like a "buffered" PEEK/POKE, in the sense that it reads/writes multiple bytes of information instead of just the one byte for PEEK/POKE. Most people will associate BLOAD and BSAVE with graphics since buffered read/write is not really useful anywhere else. However, there's more to it than that, and much more confusing when you try to save your own data with BSAVE, so I'll explain it in more detail later. |
9.5 |
More information on PEEK/POKE
The best way of learning is by example, there are a few PEEK and POKE examples in MEMORY.ABC and FAQS.ABC
|
|
|