Your place is saved automatically on this browser.
Start with the number system you already know: decimal
Most of us learn to count in decimal. Decimal is base 10, which means that it has ten digits:
0 1 2 3 4 5 6 7 8 9When we run out of single digits, we add another position. The value of a decimal number depends on the position of each digit. For example, 347 means:
3 hundreds = 3 × 100
4 tens = 4 × 10
7 units = 7 × 1
347 = 300 + 40 + 7This idea of place value is the key to understanding binary and hexadecimal. The only thing that changes is the base.
Binary: base 2
A digital computer is built from circuits that naturally represent two states. We can think of those states as off/on, false/true, low/high, or simply 0 and 1. Binary is therefore base 2 and uses only two digits:
0 1In binary, each position is worth twice the position to its right. Reading from right to left, the place values are:
... 128 64 32 16 8 4 2 1Consider the eight-bit binary value:
%01000010The 1 bits are in the 64 and 2 positions:
0×128 + 1×64 + 0×32 + 0×16 + 0×8 + 0×4 + 1×2 + 0×1
= 64 + 2
= 66 decimalThe percent sign is commonly used by assemblers to make it obvious that a number is binary. We will verify Odin syntax when we need to enter binary constants; for now, use the notation as a visual aid.
Hexadecimal: base 16
Hexadecimal, usually shortened to 'hex', is base 16. It needs sixteen digits. Decimal already gives us ten symbols, 0 to 9, so hexadecimal continues with the letters A to F:
0 1 2 3 4 5 6 7 8 9 A B C D E FThe letters are simply digits. In hexadecimal, A means ten, B means eleven, and so on up to F, which means fifteen.
In the Odin source we have been using a dollar sign to identify hexadecimal numbers. Therefore $42 means '42 in hexadecimal', not forty-two in decimal.
Converting hexadecimal to decimal by hand
You do not need to memorise every hexadecimal value. For a two-digit hex number, multiply the left digit by 16 and add the right digit.
$2A
2 × 16 = 32
A = 10
32 + 10 = 42 decimalAnother example:
$42
4 × 16 = 64
2 = 2
64 + 2 = 66 decimalConverting a small decimal value to hexadecimal
For values up to 255, divide by 16. The whole-number result gives the high hex digit and the remainder gives the low hex digit.
42 decimal
42 ÷ 16 = 2 remainder 10
2 -> hex digit 2
10 -> hex digit A
42 decimal = $2AAt first it is perfectly reasonable to use a conversion table or calculator. The aim is to understand the relationship; speed comes naturally with practice.
Why hexadecimal is so useful in assembly language
You are correct to expect a lot of hexadecimal in assembly programming. It is not used simply to make the code look technical. It is used because hexadecimal fits binary extremely neatly.
One hexadecimal digit represents exactly four binary bits. Two hexadecimal digits therefore represent exactly eight bits, which is one byte. Four hexadecimal digits represent sixteen bits, which is the size of a normal Z80 address.
1 hex digit = 4 bits
2 hex digits = 8 bits = 1 byte
4 hex digits = 16 bits = 2 bytesFor example:
binary: 0100 0010
4 2
hex: $42
decimal: 66This makes hexadecimal an excellent compact way to write values that are fundamentally binary.
A complete nibble conversion table
Because one hexadecimal digit corresponds to four bits, it is worth becoming comfortable with the following table. You do not need to memorise it immediately. Repeated use will make it familiar.
