Assembly Basics
Binary and Hexadecimal
The binary system is fundamental to programming in assembly.
Computers use the binary system, but
before I explain this we'll look at the denary system, the number system
we use in everyday life.
The Denary Number System
In the denary system, there are ten digits, which are labelled 0 to 9.
In a multi-digit number the rightmost position is the 'ones' position.
Each digit is multiplied by 1, or 100. The next position over
to the left from this one is the 'tens' position, and the numbers are
multiplied by 101 to obtain their value. Similarly the next
column is multiplied by 102. The 'n'th column would be multiplied
by 10n-1 to give their value.
The Binary Number System
The difference with the binary system is that we limit ourselves to just
two digits rather than 10. The digits are 0 and 1.
Now, instead of the rightmost position being
multiplied by 100, we multipy by 20. The next position
over to the left is multiplied by 21, and so on.To get used
to this we'll do an example. We'll find the denary value of the binary
number 1001101. Starting at the right we'll take the first position. It contains
a one, which we multiply by 20. This is 1 * 1, which is 1. The
second column contains a 0, which when multiplied by 21, gives
0. The third column gives 1 * 22, or four. After multiplying
the remaining digits by their corresponding value, we will have:
(20 * 1) + (21 * 0) + (22 * 1) +
(23 * 1) + (24 * 0) + (25 * 0) +
(26 * 1)
=1 + 4 + 8 + 64
=77
Each of the positions for a digit in a binary number is a 'bit'. Therefore
a number with 8 possible positions is called an 8 bit number. An 8 bit
number has a maximum value of 255 (eight 'ones'). Other sizes which are
used on computers are 16 and 32 bit, with maximum sizes of 65535 and
4,294,967,294 respectively.
In your assembler, to specify a binary
number, use the notation ????????xB, where the ?'s are the binary digits.
The Hexadecimal system
The hexadecimal system uses 16 digits instead of 10 or 2. The reason it
is used is as a shorthand for binary. The 16 digits which are used are
'0', '1', ... '9', 'A', 'B', ... 'F'. The letters can be thought of as
extra digits. The 'A' has a decimal value of 10, and the 'F' has a decimal
value of 15. To convert a hexadecimal number to decimal, follow the same
system as for the binary system but use instead 160, 161,
etc. The main advantage of using hexadecimal is that it is easily
converted to binary and vice versa. To do the conversion we use the
binary values of the hexadecimal digits, which are given below.
Hexadecimal Digit | Binary equivalent |
Hexadecimal Digit | Binary equivalent |
0 | 0 | 8 | 1000 |
1 | 1 | 9 | 1001 |
2 | 10 | A | 1010 |
3 | 11 | B | 1011 |
4 | 100 | C | 1100 |
5 | 101 | D | 1101 |
6 | 110 | E | 1110 |
7 | 111 | F | 1111 |
To convert from hexadecimal to binary, simply replace the hex(short for
hexadecimal) digits with their binary equivalents, making sure to include
leading zeroes to make the binary number 4-bit. For example: The hex
number A5 is converted to binary by replacing the 'A' with 1010 and
replacing the 5 with 0101, giving 10100101. An 8-bit number uses two hex
digits, a 16-bit four etc.
To convert from binary to hex, divide the
binary number into groups of four, starting at the right, and replace
each group by its corresponding hex digit.
In assembly language, the
notation ????xH is used to denote a hexadecimal number, where the ?'s are
the hex digits.
Back to main index