Lecture 17

Number systems and Functional circuits

Revision

A binary digit is known as a bit and stores either the value 0 or the value 1. It has two levels only. We normally need to handle numbers outside this range.

Take a binary number:

1 0 0 1      which is 9 (decimal)

This is made up of 4 bits. The left-hand bit is called the most-significant bit or MSB and the right-hand bit is called the least-significant bit or LSB.
The LSB, or bit 0, has the value (or weight) 1 or 20. Each bit has an increasing weight and is a power of 2. So bit 1, has a weight of 2 or 21, bit 2, has a weight of 4 or 22 and bit 3, has a weight of 8 or 23.

The number 1 0 0 1 equals 9 (decimal) as shown below.

Weight23222120
Weight in decimal8421
Number1001

Add the weight of each '1' bit8 +1=  9

In computing, 8 bits are used in a group and this is known as a byte. In the same way 4 bits as a group are called a nibble as this is half a byte!

You should also note that larger units than bytes are often used and are called words. A word is normally equal to the bus width of the processor (CPU) and could be 8, 16 or 32 bits wide.

Hexadecimal

Hexadecimal notation (hex) is based on radix 16. This is particularly convenient for expressing binary values as 4 bits are represented by one hexadecimal digit. Hex digits range from 0 to 15 (decimal) which is 0 to F (hex). Hex numbers above 9 use the letters A through F as follows:
	     decimal	   hexadecimal	       binary
		9		9		1001
		10		A		1010
		11		B		1011
		12		C		1100
		13		D		1101
		14		E		1110
		15		F		1111
Therefore a byte of data can be expressed as two hexadecimal digits.
	 Example:			10001010

	 Split into two 4-bit fields	1000 1010 = 8A (hexadecimal)

Computing using bytes

Addition

Making calculations using data arranged in bytes is just the same as calculating in decimal. The only difference is the significance of each number and the weight of the carrys (or borrows) generated.

So, 2316 + 4516 = 6816.
Also, 2316 + 4916 = 6C16. Notice that the hexadecimal digit "C" has to be used here to represent the decimal number "12" as a carry is only generated every 16 counts.

When a computer adds numbers together in bytes, then each carry has a value of 256 units.

 00100011
 +
 01000101

001101000
In the example above, the two numbers 2316 and 4516 have been added to make the answer 6816. No carry has been generated as shown by the 0 in the result.

Using different numbers,

 10100011
 +
 11000101

101101000
where A316 and C516 have been added to make the byte 6816. This looks wrong as it is the same answer as the previous calculation, but note the carry of 1 which is part of the result. So the true result is 1 68 (hex).

The carry will be added to the next most-significant column of bytes in the same way as we do a carry in decimal arithmetic.

The lesson here is that when two binary numbers are added together, the result potentially grows by one bit. So adding two 8-bit numbers together (bytes) gives a 9-bit result.

Subtraction

Subtraction follows similar rules to that above except the carry bit becomes a borrow.

Normally the number to be subtracted is converted into its 2's complement form (negated) and an addition is performed.

Multiplication

Multiplication may be performed by a "shift and add" algorithm and will take several steps.

For example (using 3-bits for simplicity):

  101
x 110

  000
 101 
101  

11110
In forming the partial product, an addition is performed only if the bit in the multiplier is '1'. The addition of the partial products is normally performed as the calculation proceeds, rather than at the end (as shown above).

In the multiplication process, multiplying two 8-bit numbers together will create a 16-bit result. More generally, multiplying an m-bit and an n-bit number together gives a result containing m+n bits.

Division

For division, the divisor is subtracted from the dividend, beginning at the most significant bit.

If the subtraction is successful (i.e. the result does not go negative) then a '1' is entered into the corresponding bit of the quotient. If the trial subtraction is not successful, the a '0' is entered into the quotient. This process continues until all the bits of the dividend have been used leaving a remainder.

Negative numbers

Negative numbers are most conveniently represented in 2's complement form. This allows binary circuits to perform arithmetic calculations in the correct way for either negative or positive numbers.

Since the most significant bit is set, if a 2's complement number is negative, this is an easy way of detecting the sign of a number.

The sign of a number is not an issue when adding or subtracting numbers. However in multiplication and division, the sign of the result will have to be determined from the signs of the two original numbers.

LOGICAL OPERATIONS

As well as the arithmetic operations on bytes above, logical operations may be performed. These are called bit-wise operations as each bit is treated as a separate logical variable, rather than taking the byte as a whole.

When two operands are used, these bit-wise functions operate in the same way as a 2-input logic gate.

ARITHMETIC AND LOGICAL FUNCTIONAL CIRCUITS

8-bit microprocessors perform arithmetic and logical operations on data one byte at a time. 16-bit, 32-bit and wider processors process data in larger units, but the principle remains the same.

A structure called an ALU (arithmetic and logical unit) is available which can perform most of the common arithmetic and logical functions.

An ALU comprises circuitry to perform arithmetic and logical operations, such a ADD, SUBTRACT, AND, OR, SHIFT. The construction and operation of an ALU may be understood by taking some examples of logical and arithmetic operations. A simple logic gate is shown below.

A and B are binary digits, and so is X - these are called bits. In a microprocessor, it is normal to deal with bytes (8 bits) rather than individual bits so the ALU equivalent to perform a byte-wide AND is

A0 to A7 comprise byte 1 and B0 to B7, byte 2. X0 to X7 is the result, again one byte. In this bitwise-AND operation, the circuitry for each bit is independent. These bits not always independent as we can see in the following example. Consider the circuit for an adder.

A and B are 1-bit inputs and SUM is the 1-bit output. CARRYin and CARRYout allow full adder stages to be cascaded as shown.

This is more commonly represented in the diagram below.

Suppose that an ALU supports the operations below. Some are unary operations on A only and others require two inputs, A and B:

FunctionCodeDescription
ADD0A + B
SUB1A - B
SHL2Shift left A by 1 bit (= A x 2)
SHR3Shift right A by 1 bit (= A / 2)
AND4A AND B (bitwise)
OR5A OR B (bitwise)
XOR6A XOR B (bitwise)
COM71's complement of A (bitwise)
Then, using a 3-bit code as an input to the ALU to represent the functions 0 through 7, we could programme the ALU to perform one of the 8 functions at any time.

The ability to perform different operations on data is a function of a microprocessor. If the function codes were written in a list, to represent a sequence of operations, and these were presented to the ALU one at a time, then we would have the basis of a programmable microcomputer system.


| Back | Next |