A byte counts to 255. That covers plenty: a colour, a letter, a life count, the position of something across a screen.
It does not cover a score. Nobody wants a game where 255 points is the most you can get. It does not cover the number of a place in memory, either, and a machine has thousands of those. So the machine does what you do when you run out of fingers, and it did in Unit 1. It uses more.
Sixteen switches
Put two bytes side by side and you have sixteen switches. The place values carry on doubling past 128, exactly as before: 256, 512, 1024, on up to 32768 for the sixteenth switch.
Turn all sixteen on:
SHOW BIN 111111111111111165535
That is 65535, and it is the other number that haunts these machines. If 255 is “the most a byte holds”, 65535 is the largest unsigned number two bytes hold. Counting zero as well gives 65536 possible values. A top address of 65535 means 65536 numbered places, not 65535.
Hex was ready for this
Sixteen switches is unreadable as binary. In hex it is four digits, because each hex digit is four switches, and sixteen is four fours:
SHOW HEX FFFF65535
Four digits, and you can read the switches straight off them. That is why programmers
write addresses in hex. $FFFF tells you every switch is on. 65535 tells you nothing.
The high byte and the low byte
Two bytes side by side are still two bytes, and it helps to keep them apart in your head.
The right-hand byte counts ones, up to 255. It is called the low byte. The left-hand byte counts 256s, and it is the high byte. Each step of the high byte is worth a whole low byte’s range.
In hex the split is visible. A two-byte number is four digits, and the left two are the high byte, the right two the low:
SHOW HEX 1234
SHOW HEX 12 * 256 + HEX 344660 4660
Same number both ways. 12 in hex is 18, and 18 × 256 is 4608. 34 in hex is 52.
Add them and you have 4660. The high byte is how many 256s; the low byte is what is left.
Watch the moment the low byte fills up:
SHOW HEX 00FF
SHOW HEX 0100255 256
One more than 255 and the low byte rolls back to 00, while the high byte ticks from 00
to 01. It is the same carry you do on paper when 9 becomes 10, only the column is a whole
byte wide.
Why 64K
Sixteen switches give 65536 different patterns, counting the all-zeros one. When each pattern names one byte-sized place in memory, the addresses run from 0 to 65535. Together they form a 64K address space: the places those addresses can select.
You will see that written as 64K. 1024 is close enough to a thousand that people called it a K, and 65536 is 64 × 1024.
An address space is not a promise of that much RAM. Some addresses can select ROM, and some may have no memory fitted. A machine can also switch banks of memory into the same addresses, like changing which book is open at the same numbered page. That lets it use more memory than those addresses can select at once.
A machine has addresses 0 to 65535. Does that prove it has 64K of RAM?
No. There are 65536 possible addresses, but the address range does not tell you how much RAM is fitted. You need the machine’s memory map to find out what each address selects.
The Spectrum +3 is one example: its processor selects 64K at once, while the machine has 128K of RAM and 64K of ROM. It uses bank switching to choose which parts are visible.
Which of the two bytes sits first in memory is a decision each machine made for itself. It is one of the first things you learn when you meet one, and it belongs there rather than here.
When it’s wrong, see why
- A number will not go past 255. You are storing it in one byte. A score, a position on a long level or an address needs two.
- The value is out by a multiple of 256. The high byte is wrong. Check which byte you put where.
- 65535 plus one came out as 0. It did. Sixteen switches all on, plus one, is seventeen switches. If you keep only sixteen bits, the extra bit is outside the stored value. A language can use more space instead: ordinary BASIC numeric variables do not stop at 65535.
- A four-digit hex number looks like two two-digit ones. It is. Read the left pair as the high byte and the right pair as the low.
What you’ve learnt
- Two bytes are sixteen switches, and they count to 65535.
- In hex that is four digits,
FFFF, and the split into two bytes shows in the digits. - The high byte counts 256s and the low byte counts ones.
- Sixteen-bit byte addresses select 65536 places: a 64K address space.
- The address space and the amount of RAM fitted are different facts.
What’s next
Every number so far has been zero or up. Programs go down too, and the machine has no minus sign. In Unit 4 we see the trick it uses instead.
Sources
The Amstrad ZX Spectrum +3 User Manual, p. 182, explains the address range, RAM and ROM sizes, and paging.