The Lush Engine





Introduction
General Assembler
Introduction
Assembler Intro
Number Systems
Mono Gameboy
Gameboy Colour
Reference




Number Systems

Title :Number Systems
Overview :A run through the different number systems used in assembly language programming and how to convert numbers between them.
Audience :Beginner assembly language programmers.
Requirements :None.
Objectives :Explain binary and hexadecimal number systems, the byte, and how to convert between the three main number systems (binary, decimal, and hexadecimal).

Section 1 - Binary

A computer, being purely electronic, stores information (or data) by using lots of tiny switches. These switches can either be open or closed, off or on. This gives two possible positions, each of which is given a value by the computer. The values are 0 (open or off) and 1 (closed or on).

This value is known as a Binary Digit, or BIT. It is the smallest piece of information (or data) that the computer can work with (it cannot distinguish between an open switch and a halfway open switch. The switch is either open or closed).

Being human, we like to work with larger values than 0 and 1 which is why our native number system uses decimal digits (0-9). Even so, a single digit on its own isn't much use to us.

Going back to school for a moment, when we were taught the number system (decimal or base ten), we learned that the position of a digit (0-9) told us the actual value of it. We have a units column, a tens column, a hundreds column, and so on. Each column uses the same digits, but suddenly we could look at a digit and recognise it as a much bigger number than that represented by a single digit.

For example, we know that the number

3

Has a value of 3, because the 3 is in the units column, and that the number

90

Has a value of 90, because the 9 is in the tens column, so it is multiplied by ten to get its value.

Binary Digits are used in the same way, but of course, the columns have different values. To find the values of the columns in any number system you simply follow these rules:

1. The first column (the right hand one) is always the units column, the digit in that column is multiplied by one to get its value.

2. Each other column is equal to its right hand neighbour's value multiplied by the number system's base.

The 'base' of a number system is equal to the number of different digits it has. For example, decimal has the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. There are ten different digits, so the base is 10. In the same way, binary has the digits 0 and 1, there are two different digits, so the base is 2.

To prove the above rules work, we'll look at the decimal system. We find that the right hand column is equal to 1, or the units (rule 1). The next column to the left is equal to its right hand neighbours value (1) multiplied by the number systems base (there are 10 different digits, so the base is 10) which gives us 1x10 or 10 (rule 2). The next column is 10x10 or 100, the next 100x10 or 1000, and so on.

To demonstrate this for the binary system, we find that the right hand column is equal to 1, or the units (rule 1). The next column to the left is equal to its right hand neighbours value (1) multiplied by the number systems base (there are 2 different digits, so the base is 2) which gives us 1x2 or 2 (rule 2). The next column is 2x2 or 4, the next 4x2 or 8, and so on.

Now knowing this, we can look at a binary number and work out what its value is. Let us look at the following binary number:

1101

Now, we have four columns. For the purposes of this section of the tutorial I will number the columns 1 to 4 running right to left (we'll see later what the columns are really called). Using the binary number system, we find that each columns value or digit multiplier is as follows:

Column 1 : units, or 1
Column 2 : 1 x 2 or 2 (right hand column multiplied by the base)
Column 3 : 2 x 2 or 4 (right hand column multiplied by the base)
Column 4 : 4 x 2 or 8 (right hand column multiplied by the base)

Now we can take each column in turn and multiply the digit by the column value as shown below:

Column 1 : 1 x 1 = 1
Column 2 : 0 x 2 = 0
Column 3 : 1 x 4 = 4
Column 4 : 1 x 8 = 8

All we need to do now is add up the results, which gives us 1+0+4+8 or 13. It may seem a little complicated at first, but it will become much easier as time passes.

As a little exercise, try proving, on a piece of paper, the following binary numbers and their values:

1001 = 9
0001 = 1
1110 = 14
0111 = 7

So long as you can follow through the logic, you will have no problems with binary numbers.

Section 2 - The BYTE

Although in the real world, we use as many digits as we need to write a decimal number, from 1 to 14567 and beyond, computers need some order and standards. Due to this, the BYTE appeared.

A BYTE is simply the name for an 8 digit binary number, like the following examples:

10100010
00001101
10100011

Notice that the byte must always have eight digits, even if some of them don't appear to be needed. This isn't complicated, just work out what your binary number is and stick enough 0's on the left hand side until you have 8 digits.

The BYTE is a very useful size, as it can store a number ranging from 0 (all 0's) to 255 (all 1's). The column values are as follows (BIT 0 is the right hand BIT, and BIT 7 is the left hand BIT, these are the standard column references).

Bit 7 6 5 4 3 2 1 0
Value 128 64 32 16 8 4 2 1

As you can see, finding the largest number that a BYTE can hold is done by adding together the values of all the columns, 128+64+32+16+8+4+2+1 = 255. Don't forget that 00000000 is a valid value where computers are concerned, so although the highest number you can store in a BYTE is 255, the lowest number is 0, not 1. This gives you 256 combinations. This magic number will appear very often in the computer world.

There are a few of points worth mentioning here. The first is that sometimes programmers need to split a BYTE into two halves, the left four BITs being the first half and the right four BITs being the second half. A collection of four BITs is called a NIBBLE.

The second point is the HIGH and LOW terms in number systems. The HIGH BIT, NIBBLE, BYTE, or whatever is being referred to, is always on the left hand side, and the LOW BIT, NIBBLE, BYTE, or whatever is being referred to, is always on the right hand side.

A final point worth mentioning is a reference to the significance of a BIT within a group of BITs. The BIT on the left hand edge of the group is called the Most Significant Bit (or MSB) and the BIT on the right hand edge of the group is called the Least Significant Bit (or LSB). So, within a BYTE, BIT 7 is the MSB, and Bit 0 is the LSB. An easy way to remember which is which, is to work out which BIT is worth the most, or has the highest value. This is the MSB. The BIT which is worth the least, or has the lowest value (always the units BIT) is the LSB.

Section 3 - Hexadecimal

In addition to decimal and binary number systems, programmers like to use base sixteen, or hexadecimal. You might ask why base 16?, why not base 17 or base 36?. The answer is simple, base 16, or hexadecimal, relates to binary in a very special way as we will see later when we convert numbers from binary to hexadecimal and vice versa.

Anyway, back to Hexadecimal (or hex as it is commonly known). Our first hurdle is how to show the digits. The only numerical digits we have are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 which are used for decimal, we don't have any more numerical digits to represent hexadecimal. Due to this, we have to start using letters. We need an extra six digits, so we use the letters A, B, C, D, E, and F. The following table shows the hex digits and their values:

Hex digit 0 1 2 3 4 5 6 7 8 9 A B C D E F
Value 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Now that we know what the hex digits are, we can use the same rules as before to work out what the column values are.

The right hand column is the units column (rule 1). The next column to the left is equal to its right hand neighbours value (1) multiplied by the number systems base (there are 16 different digits, so the base is 2) which gives us 1x16 or 16 (rule 2). The next column is 16x16 or 256, the next column is 256x16 or 4096, and so on.

In your normal use of hex, you will only ever use 2 or 4 digits, and they are always used in pairs. The 2 digit hex number has a range of 0-255, just like a BYTE which is why hex is often used for sections of data in programs as it is a nice, compact, and fixed size.

Section 4 - Converting Binary to Hex

This is quite straightforward. Throughout the explanation I'll use an example to further explain how it works.

Our binary number is 01101011.

Step one

Take the left hand four BITs of the binary number (the high NIBBLE) and treat them as a normal binary number. The result (0-15) gives you the high digit (the left hand one) of the resulting hex number.

Taking our example, we split the number into two NIBBLEs, and get the following result.

0110 1011

We want the left hand NIBBLE which is 0110, and we convert it to a normal number as shown.

0110 = 6

The number 6 in hex notation is also six. This is our high digit (the left hand one).

Step two

Now take the right hand four BITs of the binary number (the low NIBBLE) and treat them as a normal binary number. The result (0-15) gives you the low digit (the right hand one) of the resulting hex number.

Taking our example again, we split the number into two NIBBLEs, and get the following result.

0110 1011

We want the right hand NIBBLE which is 1011, and we convert it to a normal number as shown.

1011 = 11

The number 11 in hex notation is B. This is our low digit (the right hand one).

Step three

Now we have the left and right digits, put them together and you have the hex equivalent of the binary number.

The left digit was 6, and the right digit was B, this gives us 6B, so the full conversion is:

01101011 = 6B

or

01101011 = 0110 1011 = 6 11 = 6 B = 6B

Section 5 - Converting Hex to Binary

This is also quite straightforward. Throughout the explanation I'll use an example to further explain how it works.

Our hex number is 6B.

Step one

Take the left hand digit of the hex number (the high digit) and treat it as a normal number. Convert the result (0-15) into binary to give you the high NIBBLE (the left four BITs) of the resulting binary number. Remember to ensure that the resulting binary number is exactly four digits long, padding to the left with 0's if needed.

Taking our example, we split the number into two digits, and get the following result.

6 B

We want the left hand digit which is 6. This digits value is 6, and we convert it to a binary number as shown. We pad an extra 0 on the left hand side to make it a four digit number.

6 - 110
110 - 0110

Step two

Now take the right hand digit of the hex number (the low digit) and treat it as a normal number. Convert the result (0-15) into binary to give you the low NIBBLE (the right hand four BITs) of the resulting binary number. Remember to ensure that the resulting binary number is exactly four digits long, padding to the left with 0's if needed.

Taking our example again, we split the number into two digits, and get the following result.

6 B

We want the right hand digit which is B. This digits value is 11, and we convert it to a binary number as shown. We don't need to pad the number, as it is already four digits long.

11 = 1011

Step three

Now we have the high and low NIBBLEs, put them together and you have the binary equivalent of the hex number.

The high NIBBLE was 0110, and the low NIBBLE was 1011, this gives us 01101011, so the full conversion is:

6B = 01101011

or

6B = 6 B = 6 11 = 0110 1011 = 01101011

End of Tutorial


Buttons created using the interactive button maker at Coolarchive