04.03.2013 Views

spectrum graphics - OpenLibra

spectrum graphics - OpenLibra

spectrum graphics - OpenLibra

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

to the computer world.<br />

Whatever form of counting you<br />

use to program your computer,<br />

decimal, hex or binary, it is the<br />

binary form which is important,<br />

and the binary form is the one<br />

stored in the memory. We use<br />

the other forms because binary<br />

is so unwieldly for we humans,<br />

so we let the computer do the<br />

conversion.<br />

As each address, or byte, in<br />

the computer's memory can only<br />

hold 8 binary digits, bits, what<br />

happens if you wish to store a<br />

number like 12345 decimal,<br />

which has more than 8 bits? The<br />

computer takes the 1 6 bit form<br />

of the number, and cuts it in half<br />

ie 00 11000000111001<br />

becomes 00110000001110<br />

01. It then stores each half<br />

separately. The second half, the<br />

low byte, is stored first, and the<br />

first half, the high byte, in the<br />

following address.<br />

This is where decimal, which<br />

is more familiar to humans,<br />

becomes awkward to use in<br />

relation to computers. The low<br />

byte, BIN 0011 1001 , is 57<br />

decimal and the high byte, BIN<br />

001 1 0000, is 48 decimal. At<br />

first glance there seems to be<br />

very little connection between<br />

57 and 48 and 12345. In fact,<br />

you can find the decimal by<br />

multiplying the high byte by 256<br />

and adding the low byte.<br />

256 x 48<br />

ADD<br />

TOTAL<br />

= 12288<br />

57<br />

12345<br />

This lack of an obvious connection<br />

between a number and its<br />

low and high bytes in decimal is<br />

one of the reasons why many<br />

machine code programmers<br />

prefer to work in hex, which is<br />

less unwieldly than binary and<br />

more convenient than decimal<br />

for programming. But before I go<br />

on to talk about hex, I am going<br />

to give you some homework. If<br />

you plan todoany machine code<br />

programming, you will save<br />

yourself a lot of looking up in<br />

tables if you learn by heart the<br />

binary forms of the decimal<br />

numbers 0 to 15, so try to<br />

memorise this little table.<br />

1 100 12<br />

1101 13<br />

1110 14<br />

1111 15<br />

Hex<br />

In hex you have 16 digits<br />

0,1 ,2,3,4,5,6,7 ,8,9,A.B,C,D,<br />

E,F. The column system for<br />

counting is used exactly as<br />

before, but instead of bringing in<br />

a new column after 9, as in<br />

decimal, you carry on until you<br />

reach F, and then introduce the<br />

new column.<br />

HEX<br />

0<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

A<br />

B<br />

C<br />

D<br />

E<br />

OF<br />

10<br />

BINARY AND HEX<br />

DECIMAL<br />

0<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

Again, the number reached in<br />

hex is not ten, but one-oh hex,<br />

and is equal to 1 6 decimal. Hex<br />

numbers are expressed as 1 OH<br />

or 0010 H,<br />

Each column in hex is worth<br />

1 6 decimal times its right hand<br />

neighbour. The number 12345<br />

decimal is 3039h, and is broken<br />

down as follows:<br />

4096 256<br />

3 0<br />

4096 x3 -<br />

16x3 =<br />

1 x 9 =<br />

4096<br />

2<br />

TOTAL<br />

256<br />

A<br />

16<br />

3<br />

16<br />

C<br />

1<br />

9<br />

12288 +<br />

48 +<br />

9<br />

12345<br />

A hex number containing some<br />

of the letter digits can be converted<br />

as follows:<br />

4096 x2 = 8192 +<br />

BIN DECIMAL 256 x10 = 2560 +<br />

0000 0 16x12 = 192 +<br />

0001 1 1x15= 15<br />

0010 2<br />

001 1 3 TOTAL 10959<br />

0100 4<br />

0101 5 You will remember that we<br />

0110 6 discovered earlier that the high<br />

011 1 7 and low bytes of 12345 were<br />

1000 8 48 and 57 respectively. We<br />

1001 9 have since seen that the hex<br />

1010 10 form of 12345 is 3039h. The<br />

101 1 11 hex form of48dis30hand57d<br />

1<br />

is 39 h. You can see at a glance<br />

what the high and low bytes of a<br />

hex number are, without doing<br />

any calculations. From this, it<br />

follows that there is a direct con<br />

Figure 7. Binary, hex and decimal comparisons<br />

thumb on each hand — and<br />

everyone, including primitive<br />

man who invented counting,<br />

learns to count on their fingers.<br />

If we had three extra fingers per<br />

BIN 001 1 = 03h = 3d<br />

8IN0000 = OOh = Od<br />

BIN001 10000 = 30h = 48d<br />

BIN 001 1 = 03h = 3d<br />

BIN 1001 = 09h = 9d<br />

BIN 001 1 1001 = 39h = 57d<br />

BIN 001 10000001 1 1000 = 3039h = 12345d<br />

nection between the binary and<br />

hex forms of a number which is<br />

much more obvious than the<br />

connection between the<br />

decimal and any other form.<br />

(See figure 1 ).<br />

Clearly, this makes life much<br />

easier for the machine code programmer.<br />

By using hex he can<br />

avoid all the calculations re<br />

quired to find the high and low<br />

bytes when working in decimal.<br />

Also, when he comes to type in<br />

the machine code listing, most<br />

of the numbers are shorter than<br />

the equivalent decimal ones so<br />

there is a saving ifi time too. In<br />

fact, after you have become<br />

familiar with hex you begin to<br />

wonder why decimal is the<br />

universally popular system. I<br />

suspect that is has something to<br />

do with the fact that we are all<br />

born with a built in decimal<br />

calculator four fingers and a<br />

hand I believe that we should all<br />

be working away happily in hex,<br />

and the genius who suggested<br />

that you could have a very good<br />

counting system with only ten<br />

digits would be quietly led away<br />

by men in white coats.<br />

Meanwhile, computer programmers<br />

need to familiarize<br />

themselves with binary and hex.<br />

You simply need to become as<br />

expert with binary and hex as<br />

you are with decimal, and practice<br />

is the only way. You can<br />

help yourself by learning another<br />

conversion table (figure 2). It is<br />

very similar to the first one, but<br />

will allow you to convert between<br />

the three systems. In fact,<br />

you have very little extra to learn<br />

because decimal and hex figures<br />

are the same until you reach 10<br />

decimal.<br />

Can you do sums in hex?<br />

ZX COMPUTING DECEMBER,JANUARY 1985 55

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!