NUMBERS
This issue I thought I might explain some
mathematical manipulations that have been used in previous columns.
It is all very simple so you will not need to book in at your local
college for an '0' level maths course!
If you did not already know, the computer contains a section of
memory that you can either read or write to called random access
memory (RAM) and memory that cannot be written to, called read only
memory (ROM).
STORING NUMBERS IN MEMORY
RAM can be pictured as a series of little boxes, or pigeonholes,
each containing a number from 0 to 255. Each number gives the
computer a piece of information. As you may know from past articles,
memory is made up of millions of little switches (bits) in blocks of
eight (this is why your machine is termed 8-bit). Each switch has
two conditions, either on or off. Mathematically, any combination of
eight with two states means two to the power of eight — 256 (0 to
255).
Supposing we want to store numbers greater than 255? For this we
must use two memory locations. One is termed the low byte and stores
the numbers 0 to 255, equivalent to the units (0 to 9) in decimal.
The second is the high byte and stores the multiples of 256's,
equivalent to the tens in decimal. What if we want to store 255?
This can be stored just in the low byte, as can any number from 0 to
255 but if we had 256 we would store it
as 1 in the high byte and clear the low byte to zero.
As an example, I will pick a number at random — 57344. This needs to
be split into multiples of 256 so first I find the high byte by
dividing this number by 256. The result is 224. Any remainder will
be less than 256 and will be stored in the low byte. To find the
remainder I multiply 224 by 256 and subtract it from 57344. In this
case the remainder is zero. If you want to include this in your own
programs then it will be as follows,
10
NUMBER = 57344
20 HIGHBYTE = NUMBER/256
30 LOWBYTE =NUMBER-256*HIGHBYTE
I should
point out here that mathematical equations are worked out in a
certain order irrespective of how they appear in a program line. In
order of priority they are:
Brackets
of, e.g. a third of six division
multiplication
addition
subtraction
In line
30, therefore, the HIGHBYTE is multiplied by 256 before it is
subtracted from NUMBER.
WHY STORE NUMBERS?
Why do we need to use all this at all? Well, within the RAM are
certain locations called POINTERS. A pointer is a signpost. It
contains a number which will point to another location in memory. So
what is the point of that I hear you pun! Well, let's have an
example. Location 756 is just such a pointer. It holds a number
that, when multiplied by 256 will send the computer to a location
where all the characters are stored. By sheer coincidence, this
number just happens to be 224. Now, suppose we stored another
character set at 57856. By changing the number stored in location
756 we can send the computer to 57856 for a different character set.
This number is, of course, 57856/256 (226 exactly).
Now for your homework and a bit of magic. Set up your computer in a
GRAPHICS 1 screen and print something on it using PRINT #6; "TEXT"
(or ?#6; "TEXT" for short. As usual, you will see that the
characters are in upper case, so clear the screen. Print POKE
756,226 and press RETURN. Now try printing to the screen and see
what happens.
OTHER EXAMPLES
Another often used example is to lower the top of RAM to protect
memory. Location 106 holds a number which relates to the amount of
RAM available. This number must be multiplied by 256. Each unit is
called a 'PAGE' of memory and four pages = 1K of RAM (4 times 256 =
1024 = 1K).
Let us pretend that we wish to protect 4K of memory (the protected
size must be in multiples of 256). First we must find out how many
pages are available by PEEKing location 106, then subtract 16 (the
number of 256's in 4K) from this total and POKEing the new number
into the location, i.e.
10
PAGE = PEEK(106)
20 NEWPAGE =PAGE-16
30
POKE 106,NEWPAGE
An easier
way to do this is by
10
POKE 106, PEEK(106)-16
Make sure
that you reset the RAM top before a graphics call. this will ensure
that the new details for the display list are properly stored.
USING OFFSETS
In a previous article, I explained about the IOCB and gave some
details about offsets. Rather than POKE to several different
locations, having to remember each one, why not take one as a base
and POKE to the base plus an offset. For example, we know that the
character set starts at 57344. To change the full character set by
POKEing each individual location would take 1024 lines of POKE
LOCATION, DATA. The easiest way would be to take 57344 as a base and
add one each time then POKE in the DATA. This is called
incrementing. To do this properly, the offset or increment would
have to start off at zero, not 1, otherwise the first POKE would be
at 57345. We could change the base to 57343 and start the offset at
1, but when you looked up this location much later it would not make
immediate sense (it is a floating point logarithm routine!).
Unfortunately, the character set is in ROM and we cannot change it.
We can store a new character set in RAM though. It only needs the
pointer to be changed to indicate this new location and your
characters automatically change. Listing 1 is a small routine to do
just that.
Some points to note. I used a location suitable for this demo. I
used a flag of -1 to make sure the program finished without an
error. This is not strictly necessary but it does save the annoyance
of inputting too much data. Also, lines 100 and 110 could be
combined as line 100 (the computer automatically keeps track of the
data to be read), but I did this for clarity. Notice how the space
character has been changed. Even if you cannot see the characters,
type POKE 756,224 and press RETURN to get the old set back. Try new
DATA and run it again.
That is all for this time. Since I have given you some hints on
graphics modes and how to store them, why not read the next
thrilling installment and find out how to draw some pictures?
Don't forget that you can write with any problems. Include a s.a.e.
if you want a reply. Write to Mark Hutchinson, 1 Hollymount,
Erinvale, Finaghy, Belfast, BT10 OGL
|
|
|