Assembly Lesson 1
Many people get scared of assembly (and HTML too for that matter) because
they see strange numbers that contain letters. These are hexadecimal (base 16)
numbers, a number system that contains the digits 0 1 2 3 4 5 6 7 8 9 A B C D E
F, six more than our usual "base ten" system that runs 0-9. Converting from
hexadecimal to binary is easier than going from base 10 to binary so the
computer prefers it, but there is seldom any real need to convert between number
systems by hand. However, it is a useful skill to know. We'll begin with binary,
because you'll soon see that converting from hex to binary to base 10 is often
easier than going directly from hex to base 10.
Binary, base two, is the only language the computer really understands. Everything is translated into binary before it can be read by the computer. Fortunately for us, that is all done automatically. That's very good, because the binary for something as simple as "hello, world" looks like this:
00100010 11010001 1001010 11011000 11011000 11011110 01011000
01000000 11101110 11011110 1110010 01101100 01100100 00100010
Binary counting table:
|
0000 - 0 0001 - 1 0010 - 2 0011 - 3 0100 - 4 0101 - 5 0110 - 6 0111 - 7 |
1000 - 8 1001 - 9 1010 - 10 1011 - 11 1100 - 12 1101 - 13 1110 - 14 1111 - 15 |
| Which power of two? | 2^3 | 2^2 | 2^1 | 2^0 | |
| Value of column: | 8 | 4 | 2 | 1 | |
| Binary number 13: | 1 | 1 | 0 | 1 | |
| Sum of columns: | 8+ | 4+ | 0+ | 1 | =13 |
What is the value of this binary number?
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 0 | 1 | 1 | 1 | 0 | 0 | 1 | 0 |
This number is 114: 64+32+16+2. Try converting the following binary numbers to
base ten, and click
|
A) 00101011 B) 10101001 C) 11111110 D) 01011011 E) 11110001 |
We'll use the base ten number 139 for this example. Look at the above table
with the power-of-two values for each bit. What is the largest power-of-two
number that can be subtracted from 139? Looking at the table, we can see that it
is 128. With a scratch sheet of paper, mark done a 1. We are making a binary
number from left to right, and that 1 indicates that the 128 bit is active. Ok,
subtract 128 from 139. We are left with 11. Can we subtract 64 (the next highest
bit) from 11? No, mark a 0 to the right of your 1. How about 32? No, mark
another 0 for the 32 column. 16? No, mark that zero as well. 8? Yes, we can
subtract 8. Mark a 1 for the bit containing 8 (the fourth bit from the right)
and subtract 8 from 11. We have 3. Four cannot be subtracted from 3, so mark a
zero in the fours column and move on. 3-2 is acceptable, so mark a 1 in the twos
column. Finish the number by marking that last leftover one down in the right-most
column. You should now have written down a binary number that looks like this:
10001011. If you want to, you can check your work by converting it from your
binary back to base ten.
Convert the following base ten numbers to
|
A) 238 B) 56 C) 27 D) 190 E) 163 |
Hexadecimal is a numbering system that has 16 digits instead of ten, so it is
also frequently referred to as "Base 16." Although the idea of using 16 digits
is confusing to a human at first, the computer can convert with amazing ease
between binary and hex, so to speed up operations we are often asked to input
numbers in hexadecimal format. (Note: sometimes our own base ten system is
referred to simply as "decimal" numbers, so be prepared to hear me call it that)
| Hex | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
| Decimal | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| Original number: | 11100111011011011100001110100011 | |||||||
| Broken up number: | 1110 | 0111 | 0110 | 1101 | 1100 | 0011 | 1010 | 0011 |
| Decimal equivalent (per section): | 14 | 7 | 6 | 13 | 12 | 3 | 10 | 3 |
| Hexadecimal equivalent: | E | 7 | 6 | D | C | 3 | A | 3 |
Occasionally you will also be expected to convert directly from hex to base
10. The process for this is similar to the one used for converting binary to
decimal, but much more complicated, since things are being multiplied on a scale
of 16 instead of 2. I recommend using binary as a middle step and going
hex-binary-decimal or decimal-binary-hex instead of attempting to convert
directly.
Binary to Base 10: (1) 10011111 (2) 00101110 (3) 01000101 (4) 11101110
Binary to Hex: (1) 10011010 (2) 01011101 (3) 10100010 (4) 01110110
Decimal to Binary: (1) 164 (2) 235 (3) 85 (4) 98 (5) 149
Hex to Binary: (1) E76 (2) 9C (3) 52 (4) F7F (5) A4
Download The Answers From
Here
Final points about numbers:
|
|
When you are using numeric constants you must specify which number system the number is. The convention is to but a little h at the end of hexadecimal numbers (example: 76h), a b for binary (10010111b), and a d for decimal (76d), but if you leave off the suffix it is assumed to be base 10. |
|
|
If the left-most digit in a hexadecimal number is A-F, you must add a 0 in front of it so that the assembler knows that it is a numeric constant and not a variable. example: A7h must be written 0A7h |
|
|
Mathematical operators (+, -, *, /, etc) are practically non-existant in assembly. Therefore something like "mov var1, var2+3" is illegal. You would have to say "add var2, 3" and "mov var1, var2" separately. |