
So far every number has been in tens, because that is how people count. The machine does not. Underneath, everything in the Einstein is a row of switches that are on or off, and the numbers that are round to the machine - 256, 32768, 65536 - look arbitrary in tens.
Written in sixteens they are 100, 8000 and 10000. When you get to the sections that talk to the hardware, you will want to count the machine's way. SuperForth will do it for you.
HEX switches SuperForth to base sixteen. DECIMAL switches back.
Base sixteen needs sixteen digits, so after 9 it borrows letters: A is ten, B eleven, up to F for fifteen. FF is fifteen sixteens and fifteen: 255.
The switch affects both directions at once: how SuperForth reads the numbers you type, and how it prints the ones it has.
255 HEX . DECIMAL
reads 255 in tens, switches, and prints it in sixteens: FF.
HEX FF DECIMAL .
does the reverse, and prints 255.
Get into the habit of putting DECIMAL on the same line. Nothing on the screen tells you which base you are in, and a SuperForth left in hex will quietly misread every number you type for the rest of the session.
HEX and DECIMAL are just shorthand. The base lives in a variable called BASE, and you can store any number in it:
5 2 BASE ! . DECIMAL
prints 101: five, in binary.
There is a nice joke hidden in here. Whatever base you are in, BASE @ . prints 10. Of course it does: sixteen in sixteens is "one-nought", and so is two in twos.
In S5 you met the range -32768 to 32767. The machine itself has no idea about minus signs; it has sixteen switches, which can show 65536 different patterns, and it is a matter of choice whether you read the top half of them as negative numbers or as the numbers from 32768 to 65535.
. reads them as negative. U. reads them as big:
-1 U.
prints 65535. They are the same sixteen switches, all on. In hex it is obvious: FFFF.
255 HEX . DECIMAL
HEX FF DECIMAL .
5 2 BASE ! . DECIMAL
-1 U.
HEX -1 U. DECIMAL
Starting from
A fresh boot, with SuperForth just started.
What you should see
255 HEX . DECIMAL
FF ok
HEX FF DECIMAL .
255 ok
5 2 BASE ! . DECIMAL
101 ok
-1 U.
65535 ok
HEX -1 U. DECIMAL
FFFF ok

In base ten, FACE is a word SuperForth does not know, and it says FACE?. In hex, FACE is a perfectly good number, and it goes on the stack without a murmur. So do BED, DEAD and ADD. If you name a word of your own using only the letters A to F, it will still work, because SuperForth looks in the dictionary before it tries to read a number - but a misspelt one will no longer be caught.
HEX 10 . and then, without switching back, 10 DECIMAL . Explain both answers.HEX FACE DECIMAL . You typed something positive-looking and got a negative number. Why? What would U. have printed?HEX 1000 DECIMAL . will print before you try it.HEX and then BASE @ . What did you expect? What did you get? Remember to type DECIMAL.17.1 Define BINARY to switch to base two, and use it to print 10 in binary.
17.2 Define H. ( n -- ) to print a number in hex and leave the base as it found it, whatever that was.
17.3 What is the largest number you can write with two hex digits? With four? Check with SuperForth, and say which of . and U. you needed for the second.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
| Every number you type comes out wrong | You are still in hex, or binary. Type DECIMAL. |
FF? | You typed a hex number while in base ten. |
| A misspelt word is accepted silently, in hex | Its letters were all A to F, so it was read as a number. |
| A big hex number prints as negative | . reads the top half of the range as negative. Use U. |
HEX and DECIMAL change the base for typing and printing alike, and BASE is the variable behind them, so any base is possible. U. prints a number without a sign, which shows that -1 and 65535 are the same thing seen two ways. Always put the base back.
S18, Loading An Extension - the disc is full of words SuperForth does not know yet. How to see what is there, and how to bring it in.