Number Systems

"Take the sales, substract the costs, you get this big positive number." - Bill Gates

by Ciaran McCreesh
Created: 13th September 1999
Last Modified: 27th November 1999 1999

This page will explain the three number systems used in Assembly programs. If you already know the difference between decimal, binary and hexadecimal then I reccomend just reading the summary at the end.

Decimal

Our number system is called Decimal. It has ten different digits, 0 to 9, and can also be called 'Base 10'. If you think back to when you first started learning maths you might have been told about thousands, hundreds, tens and units. If not, the left-hand digit (ignore decimal places, I'm talking about integers here) was called the units because the number was 'multiplied by 1'. The next digit, working left to right, was the tens, because the number was 'multiplied by ten'. The third column was hundreds, and so on.

To look at it in a more complicated fashion, the right-hand column, the units, is effectively multiplied by 10^0 = 1. The second column was multiplied by 10^1 = 10, the third by 10^2 = 100 and so on.

Binary

Binary is another counting system, and it is very close to the number system used by the computer. The computer works by pulses of electricity, binary works by 0s (off) and 1s (on). They are the only two digits available. However, we can still write numbers such as 7 and 53 in binary.

Rather than having units, tens and hundreds we have to have units, twos, fours and so on. The column to the right is units as normal. The second column is the twos, the third the fours, the fourth the eights. In the more mathematical notation, the right-hand column is multiplied by 2^0 = 1, the second by 2^1 = 2 and so on.

Decimal Working Binary
10 10 = (1 * (2^3) = 8) + (0 * (2^2) = 0) + ( 1 * (2^1) = 2) + (0 * (2^0) = 0) 1010
27 27 = (1 * (2^4) = 16) + (1 * (2^3) = 8) + ( 0 * (2^2) = 0) + (1 * (2^1) = 2) + (1 * (2^0) = 1) 11011
34 34 = (1 * (2^5) = 32) + (0 * (2^4) = 0) + (0 * (2^3) = 0) + (0 * (2^2) = 0) + (1 * (2^1) = 2) + (0 * (2^0) = 0) 10010
237 237 = (1 * (2^7)) + (1 * (2^6)) + (1 * (2^5)) + (0 * (2^4)) + (1 * (2^3)) + (1 * (2^2)) + (0 * (2^1)) + (1 * (2^0)) 11101101

Hexadecimal

Hexadecimal (not hexidecimal as it is commonly mis-spelt) is base 16. There are 16 different digits, but there are only ten available in our counting system. Rather than doing anything clever and inventing new symbols for each of the six new digits we just use the first six letters of the alphabet, A to F. It doesn't matter is uppercase or lowercase is used.

Decimal Working Hexadecimal
24 24 = (1 * (16^1) = 16) + (8 * (16^0) = 8) 18
61 61 = (3 * (16^1) = 48) + (D (14) * (16^0) = D (14)) 3D
19775 19775 = (4 * (16^3) = 16384) + (D * (16^2) = 3328) + (3 * (16^1) = 48) + (F * (16^0) = 15) 4D3F

Telling Them Apart

Assembly Studio recognises three different number systems (actually it also recognises a fourth, Octal or Base 8, but this is rarely used and serves no real purpose), but how does it tell them apart? If we were to write ld a,10 the assembler doesn't know if we mean decimal 10, hexadecimal 10 which is decimal 16 or binary 10, which is decimal 2. We have to tell the assembler which number system we are using. There are two ways to do this, with prefixes and suffixes. There is no prefix for decimal, and the suffix is either d or D (if no prefix or suffix is given then the value is treated as decimal). For binary the prefix is % and the suffix either b or B. For hexadecimal the prefix is $ and the suffix either h or H. In case you ever need to use octal the prefix is @ and the suffixes o or O.

Summary

Name Base Prefix Suffix Examples
Binary 2 % b or B %1101, 01101101b, 110B
Decimal 10 none d or D (or none) 27, 48d, 126D
Hexadecimal 16 $ h or H $fc00, 207Ah, 17H