Machine Instructions and Programs
Machine Instructions and Programs
Machine Instructions and Programs
Computers are built using logic circuits that operate on information represented by two- valued electrical signals.
The label the two values as 0 and 1; and we define the amount of information represented by such a signal as a bit of information,
where bit stands for binary digit.
The most natural way to represent a number in a computer system is by a string of bits, called a binary number.
A text character can also be represented by a string of bits called a character code.
NUMBER REPRESENTATION
In all these systems, the leftmost bit is 0 for positive numbers and
for negative numbers.
Figure 1 illustrates all three representations using 4-bit numbers.
Positive values have identical representations in all systems, but negative values have different representations.
In the sign-and-magnitude system, negative values are represented by changing the most significant bit (by in Figure 1) from 0
to in the B vector of the corresponding positive value.
For example, +5 is represented by 0101, and -5 is represented by 1101.
In 1's-complement representation, negative values are obtained by complementing each bit of the corresponding positive
number.
Thus, the representation for-3 is obtained by complementing each hit in the vector 0011 to yield 1100.
Clearly, the same operation, bit complementing, is done in converting a negative number to the corresponding positive value.
Converting either way is referred to as forming the 1's-complement of a given number.
The operation of forming the 1's-complement of a given number is equivalent to subtracting that number from 2ⁿ-1, that is, from
1111 In the case of the 4-bit numbers in Figure 1.
Finally, in the 2's-complement system, farming the 2's-complement of a number is done by subtracting that number from 2ⁿ.
Here, the 2's-complement of the number is obtained by adding to the 1's complement of that number.
Note that there are distinct representations for +0 and -0 in both the sign-and- magnitude and 1's-complement systems, but the
2's-complement system has only one representation for 0.
For 4-bit numbers, the value -8 is representable in the 2's complement system but not in the other system.
The sign and magnitude system seem the most natural, because we deal with sign-and-magnitude decimal value in manual
computations.
The 1's-complement system is easily related to this system, but the 2’s- complement system seems unnatural.
ADDITION OF POSITIVE NUMBERS
Three systems for representing positive and negative numbers, or, simply, signed numbers.
These systems differ only in the way they represent negative values.
Their relative merits from the standpoint of performing arithmetic operations can be summarised as follows:
1)The sign-and-magnitude system is the simplest representation, but it is also the most awkward for addition and
subtraction operations.
2)The 1's-complement method is somewhat better.
3)The 2's-complement system is the most efficient method for performing addition and subtraction operations.
Let us apply the mod 16 addition technique to the simple example of adding +7 to -3.
The 2's-complement representation for these numbers is 0111 and 1101, respectively.
To add these numbers, locate 0111 on the circle in Figure(4).
Then move 1101 (13) steps in the clockwise direction to arrive at 0100, which yields the correct
answer of +4.
If we perform this addition by adding bit pairs from right to left, we obtain:
Note that if we ignore the carry-out from the fourth bit position in this addition, we obtain the correct answer.
In fact, this is always the case.
Ignoring this carry-out is a natural result of using mod N arithmetic.
As we move around the circle in Figure (4), the value next to 1111 would normally be 10000. Instead, we go back to the value 0000.
We now state the rules governing the addition and subtraction of n-bit signed numbers using the 2's-complement
representation system.
1) To add two numbers, add their n-bit representations, ignoring the carry-out signal from the most significant bit (MSB)
position. The sum will be the algebraically correct value in the 2's-complement representation as long as the answer is in the range
through -2ⁿ-¹ through +2ⁿ-¹-1.
2) To subtract two numbers X and Y, that is, to perform XY, form the 2's- complement of Y and then add it to X, as in rule
1.Again, the result will be the algebraically correct value in the 2's-complement representation system if the answer is in the
range through -2ⁿ-¹ through +2ⁿ-¹-1.
Figure 5 shows some examples of addition and subtraction.
The simplicity of either adding or subtracting signed numbers in 2's-complement representation is the reason why this number
representation is used in modern computers.
It might seem that the 1's-complement representation would be just as good as the 2's-complement system.
However, although complementation is easy, the result obtained after an addition operation is not always correct. The carry-out,
cⁿ, cannot be ignored. If cⁿ=0, the result obtained is correct.
If cⁿ= 1, then a 1 must be added to the result to make it correct.
The need for this correction cycle, which is conditional on the carry-out from the add operation, means that addition and
subtraction cannot be implemented as conveniently in the 1's-complement system as in the 2's-complement system.
OVERFLOW IN INTEGER ARITHMETIC
In the 2's-complement number representation system, n hits can represent values in the range-2 to +2-1-1.
For example, using four bits, the range of numbers that can be represented is-8 through +7, as shown in Figure (1).
When the result of an arithmetic operation is outside the representable range, an arithmetic overflow has occurred.
When adding unsigned numbers, the carry-out, cⁿ, from the most significant bit position serves as the overflow indicator.
However, this does not work for adding signed numbers.
For example, when using 4-bit signed numbers, if we try to add the number +7 and +4, the output sum vector, 5, is 1011, which is
the code for -5, an incorrect result.
The carry-out signal from the MSB position is 0.
Similarly, if we try to add -4 and-6, we get S=0110=+6, another incorrect result, and in this case, the carry-out signal is 1.
Thus, overflow may occur if both summands have the same sign.
Clearly, the addition of numbers with different signs cannot cause overflow.
This leads to the following conclusions:
1) Overflow can occur only when adding two numbers that have the same sign.
2) The carry-out signal from the sign-bit position is not a sufficient indicator of over- flow when adding signed numbers.
A simple way to detect overflow is to examine the signs of the two summands X and Y and the sign of the result.
When both operands X and Y have the same sign, an overflow occurs when the sign of S is not the same as the signs of X and Y.
CHARACTERS
In addition to numbers, computers must be able to handle non numeric text information consisting of characters.
Characters can be letters of the alphabet, decimal digits, punctuation marks, and so on.
They are represented by codes that are usually eight bits long.
One of the most widely used such codes is the American Standards Committee on Information Interchange (ASCII).