Assembly Lesson 1

Number Systems

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

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
 


Looking at the above table, you can see that counting in binary is actually quite similar to counting in base ten, only the highest digit that can be in any column is a one, instead of a nine. Each digit in a binary number is called a bit, and a bit is the smallest increment of memory a computer can have. It is very simple, since it can only have one of two values, although there are several ways of expressing it: on or off, one or zero, true or false. In binary arithmetic, a bit is the same as any other digit in base ten arithmetic. When it is on (1), and another positive bit (another 1) is added to it, the sum has exceeded the highest digit a binary number can have in any one column and the one must carry to the next left-most column.

Look at the binary table again. 1. 10. 100. 1000. These nice "round numbers" are all powers of 2.
0001 = 1 = 2^0.
0010 = 2 = 2^1.
0100 = 4 = 2^2.
1000 = 8 = 2^3.
Now look at the numbers that are not powers of two, like 13:

 

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


Verbal explanation: the right-most bit in the binary number is the zeroeth place, and the value of a one in that column is 2^0, or 1. Moving left, each column increments the power to which you raise two. One left of the zeroeth column raises two to the first power, meaning two. Left again is two to the second power, 4. Adding up the power-of-two values associated with each bit in the binary number gives you the base ten equivalent.

 

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
here to check your work.
 

A) 00101011
B) 10101001
C) 11111110
D) 01011011
E) 11110001

Converting from base ten to binary

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
binary:
 

A) 238
B) 56
C) 27
D) 190
E) 163


 

Hexadecimal

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


It doesn't matter whether the hexadecimal letters are uppercase or lowercase, but I tend to use the uppercase convention just to emphasize that they are numbers, not characters.

Now look back at the very first table of binary/decimal numbers. Using four bits (half a byte), you can create numbers from 0 to 15: just like Hexadecimal. Unlike in base ten where you have to add up the values for all the binary digits in a number, hexadecimal digits have a one to four correspondence with their binary counterparts. To convert from binary to hexadecimal, break your binary number up into segments of four, starting at the right. You begin on the right because when there are trailing zeroes on the left (insignificant digits) they may be left off for convenience.


 

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


Therefore this number in base 16 would be E76DC3A3, a perfectly valid hexadecimal number. Converting from hexadecimal to binary is just as easy, and after a bit of practice with this, you will be able to remember what the binary number will be per hexadecimal digit without having to calculate it.

If you are unsure about how to calculate a binary number, given a base 16 one, this next paragraph does a recap that will talk about that. If it already makes sense to you, skip on down to the exercises at the bottom and you are done with number conversions.

Let's use the number E7C. Find the decimal equivalent for each digit. For E it is 14, 7 is of course just 7, and C is 12. You can use the Hex/decimal chart above if you haven't yet memorized this part. Now find the binary for each of the decimal numbers. 14-8. Mark a 1 down. 6-4. Mark another 1 down. 2-2. Mark a 1, and then a zero at the end. 1110 is the binary equivalent for 14, and it is also the leftmost four digits of your new binary number. Now convert 7 to binary to find your middle four bits. I'm not going to go through all the steps again, but the binary for 7 is 0111 and the binary for 12 is 1100. There is no adding needed for calculating the final binary digit, merely concatenate the binary digits in the order of the hex digits. Your binary number for E7C is 111001111100.

Exercises

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.

 


 

 

Hosted by www.Geocities.ws

1