|
DP234 |
TOPIC 1
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Unit
Introduction |
|
|
binary
|
0000
|
0001
|
0010
|
0011
|
0100
|
0101
|
0110
|
0111
|
1000
|
1001
|
1010
|
1011
|
1100
|
1101
|
1110
|
1111
|
|
hex
|
0
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
A
|
B
|
C
|
D
|
E
|
F
|
Hex allows us to exactly represent a byte with two hex digits, a 16-bit value with four hex digits and a 32 bit value with eight hex digits. This is much more compact than a binary representation.
So for example, the above two binary numbers can be represented in hex as:
03 and 0039447
If you examine these numbers by themselves you will have an interpretation problem. Is the number 23 a decimal or a hexadecimal number? In the above context it is obvious which is which. However, this will not always be the case. We will follow the C/C++ convention of writing hex numbers with a preceding '0x' or '0X' string. For example, the above two numbers will be written as:
0x03 and 0x0039447
The same problem arrises when we write binary numbers. Does 10 represent a binary number or the usual decimal ten? To distinguish binary numbers we will precede the number with the string '0b' or '0B'. For example, the binary number 10 will be written as:
0b10
We will use both these conventions for all binary and hex numbers that appear from now on in the unit materials. In the case where the context is clear we will omit these prefixes.
The following textbook reading describes hexadecimal and binary numbers and how to work out the decimal equivalents. Although it is easier to use a calculator you are required to learn the manual procedures. You will find with practice that it is quicker to convert small numbers manually.
Textbook:
Englander, 1996, Section 2.0-2.2, pages 30-36.
Englander, 1996, Section
2.6, pages 44-46.
Activity 1.2: For this activity you should work out the answers manually.
0b11111 0b100110
0b101010101010
0b101100000001
0xFF 0x101
0x123 0x100A 0xABCD
0b111 0b1000
0b11111111
0b101010101 0b1000000000000000
0xA 0xA0
0x1A 0xFFFF 0x1000
The last reading discussed how to find the decimal equivalents of binary and hex numbers. We also have to convert in the other direction. That is, we need to convert from decimal to binary and from decimal to hexadecimal numbers. The following reading shows you two ways to do this. You should concentrate on the second method and use it in the activities given below.
Textbook: Englander, 1996, Section 2.4, pages 40-43.
Activity 1.3: For this activity you should work out the answers without a calculator. If you have difficulty with the first one then check you answer in this topic's feedback section before attempting the remainder.
What is the binary and hexadecimal equivalent of the following decimal numbers?
3 255
1234 100 2049
We have concentrated in this section on the conversion of numbers between three bases (binary, decimal and hexadecimal). You may have noticed in the textbook readings that there is also a lot to learn about arithmetic in the various bases. However, for the purposes of this unit you do not need to know how to do this. When arithmetic arises we can always convert hex and binary numbers to decimal and do the arithmetic. If the answer is required in the other base then we convert the decimal answer to the base.
There is one further attribute of computer numbers that we have not discussed.
That is how to represent negative numbers. Representing negative numbers
is discussed in the next section.
![]()
![]()
So far in this topic we have dealt only with positive numbers. You will be aware that most computer languages have an integer data type that allows programs to store negative as well as positive numbers. These can generally be called signed numbers. So how are these stored in memory and registers?
There are two techniques commonly used to store negative numbers. Early
computer systems used a technique called 1's complement. Modern
computer systems use a technique called 2's complement. We will
examine both these techniques. The 2's complement technique will be the
one used through the remainder of this unit.
![]()
![]()
1's complement stores a negative number as the complement (logical NOT) of the positive number. For example, the numbers -2 to +2 are stored in 16 bit registers as:
|
number
|
binary | hex |
|
2
|
0000 0000 0000 0010 | 0x0002 |
|
1
|
0000 0000 0000 0001 | 0x0001 |
|
0
|
0000 0000 0000 0000 | 0x0000 |
|
-1
|
1111 1111 1111 1110 | 0xFFFE |
|
-2
|
1111 1111 1111 1101 | 0xFFFD |
Notice that this technique results in the most significant bit (MSB) being a 1 for negative numbers and a 0 for positive numbers. Another attribute of this system is that the value with all 1 bits (i.e. 0xFFFF in this case) does not have a positive equivalent in the normal number system. It is commonly called negative zero since its complement is zero.
The following reading describes 1's complement in a round about way. It shows why this technique was chosen by describing how addition and subtraction are done. The text shows how the system would work using a decimal number equivalent. While reading this you should remember that it's the binary 1's complement that we are studying.
Textbook: Englander, 1996, Section 4.4, pages 97-104.
Activity 1.4: For this activity you should work out the answers manually.
23 -3 -255 -1234 2.
0xF 0xFFFF 0xFFF7 0xFFF0
2's complement stores a negative number as the complement (logical NOT) of the positive number plus one. For example, the numbers -2 to +2 are stored in 16 bit registers as:
|
number
|
binary | hex |
|
2
|
0000 0000 0000 0010 | 0x0002 |
|
1
|
0000 0000 0000 0001 | 0x0001 |
|
0
|
0000 0000 0000 0000 | 0x0000 |
|
-1
|
1111 1111 1111 1111 | 0xFFFF |
|
-2
|
1111 1111 1111 1110 | 0xFFFE |
Notice that this technique again results in the most significant bit (MSB) being a 1 for negative numbers and a 0 for positive numbers. This system also eliminates the redundant negative zero value so it can represent one more signed value than 1's complement.
The following reading describes 2's complement in the same round about way as the text described 1's complement. It shows why this technique was chosen by describing how addition and subtraction are done. The text shows how the system would work using a decimal number equivalent. While reading this you should remember that its the binary 2's complement that we are studying.
Textbook: Englander, 1996, Section 4.4, pages 104-107.
Activity 1.5: For this activity you should work out the answers manually.
23 -3
-255 -1234
0xF 0xFFFF
0xFFF7 0xFFF0
In this section we have examined the representation of integers in computer systems. There are several things we have skipped over or avoided. We have not examined closely how arithmetic is done in binary and hex numbers. In general this is not important for this unit. If you wish to check the results of computer arithmetic in the practical part of this unit then you must either use a calculator or convert numbers to decimal equivalents and then do decimal arithmetic.
A large area we have not mentioned at all is how to represent fractions in the computer. We will briefly look at this problem in a later topic where will see how floating point numbers are represented. We will not look at these at all in the practical part of this unit.
Next, we will examine another way to look at the contents of memory cells
and registers. Instead of interpreting them as numbers we can interpret
them as codes.
![]()
![]()
Another way of interpreting binary numbers is by assigning a property to each bit pattern. Looking at this another way, we can assign a bit pattern or code to a number of properties.
In this section we will look at assigning codes to symbols of an alphabet. We can assign a certain binary code for each symbol so that whenever this code appears it can be interpreted as that symbol. We can also develop input and output devices which reproduce the symbol whenever they are presented with the bit pattern through an I/O port.
The most common of these codes at the present time is ASCII (American Standard Code for Information Interchange). It is a 7 bit code that represents 96 printable characters and 32 control characters. Figure 1.4 shows a table of ASCII codes.

To find the code for one of the ASCII symbols we read the first three bits from the top of the table and the next four bits from the left-hand side. For example, the letter capital C ( 'C') has binary code 1000011.
ASCII is not the only code available. EBCDIC (Extended Binary Coded Decimal Interchange Code) is used extensively by IBM mainframe equipment. A new 16-bit code called Unicode is also gaining acceptance by users of the Internet. Because of the large number of codes possible, symbols from many languages can be included in Unicode.
Textbook: Englander, 1996, Section 3.0-3.2, pages 59-69.
Activity 1.6:
You will also notice from the above reading that codes can be used to store useful things besides language symbols. We will not study these in this unit.
Finally, we now look at how we can store large binary objects (numbers,
codes, etc.) in memory that is normally organised as addressable 8-bit
units.
![]()
![]()
An interesting question arises when we wish to store multi-byte objects in byte addressable storage. For example, suppose we have the 32-bit number value 0x12345678. This must be stored in four bytes in memory, so let's suppose they are addresses 0x400, 0x401, 0x402 and 0x403. There are two possibilities:
| address |
option1
|
option2
|
| 0x400 |
12
|
78
|
| 0x401 |
34
|
56
|
| 0x402 |
56
|
34
|
| 0x403 |
78
|
12
|
At first glance the first of these methods appears the most obvious way to store numbers. It stores the big end of the number first and is called big-endian byte order. It means that the address of the 32-bit number is the address of the most significant byte. However, the second techniques which stores the little end first (little-endian byte order) is also used. For example, all Intel 80x86 processors are little-endian. This technique has the advantage that bytes are stored in the same order as bits so that the least significant bits have the lowest memory address.
There has been considerable debate about which ordering is more appropriate. In fact the names little-endian and big-endian reflect the non-agreement of computer system designers. These terms are taken from Jonathon Swift's novel Gulliver's Travels where a religious war broke out between two sides who broke their eggs at the big end (big-endians) and those who broke their eggs at the little end (little-endians). The issue is similarly unresolved.
In general, the byte order is not noticeable in a program. You will need to understand the byte order when examining dumps of memory, which display large binary numbers as separate bytes. However, the main difficulty with the alternate approaches is when data is transferred between machines with different architectures. Any data unit greater than one byte long will have its byte ordering reversed so a conversion will have to be made.
Activity 1.7: Suppose memory at addresses 0x400, 0x401, 0x402 and 0x403 respectively contained the hex value 45, 56, 67 and 01. What is the 32-bit number stored at address 0x400 in a big-endian computer system? What if it is a little-endian computer? [Feedback]
In this topic we have revised the basic operation of a computer system. We briefly examined each of the four main components, ie. the CPU, memory, I/O subsystem and the bus that connects the other three parts together.
We also looked at how numbers and codes are handled inside the computer. Hexadecimal and binary representations were discussed, as was how to convert binary and hex to decimal numbers and vice versa. The 1's complement and 2's complement techniques for representing signed numbers were also examined. Finally we saw that there were two alternate methods of storing large binary numbers in memory.
In the next topic we will look more closely at memory and how the CPU accesses it. We will start to examine the SPASM machine by looking at some of its instructions.