Professional Documents
Culture Documents
Notes
In chapter 1 we saw that the computer is organized as a system with several levels
of transformation, and ultimately works by electrons moving, which in turn are
controlled by voltage levels in circuits. Electrons react to actual voltage values,
and not the presence or absence of these voltages.
But it is easier to think of the presence/absence of voltages.
Each ISA has its own set of datatypes and operations (instructions that operate on
that datatype).
In this book we use two datatypes - integers in 2's complement (tbd) and characters
as ASCII codes.
In real world computers there are other datatypes, e.g floating point.
00000
00001
...
11111
The unsigned scheme = Each pattern represents the integer value obtained by direct
binary-to-integer conversion
so
00000 = 0
00001 = 1
00010 = 2
00011 = 3
00100 = 4
00101 = 5
00110 = 6
00111 = 7
01000 = 8
01001 = 9
01010 = 10
01011 = 11
01100 = 12
01101 = 13
01110 = 14
01111 = 15
10000 = 16
10001 = 17
10010 = 18
10011 = 19
10100 = 20
10101 = 21
10110 = 22
10111 = 23
11000 = 24
11001 = 25
11010 = 26
11011 = 27
11100 = 28
11101 = 29
11110 = 30
11111 = 31
In the "signed magnitude" representation, the concept is that the left most bit
indicates the sign 1 for + , 0 for -, and the remaining bits, converted to
decimal, indicate the decimal number.
00000 = + 0
00001 = + 1
00010 = + 2
00011 = + 3
00100 = + 4
00101 = + 5
00110 = + 6
00111 = + 7
01000 = + 8
01001 = + 9
01010 = + 10
01011 = + 11
01100 = + 12
01101 = + 13
01110 = + 14
01111 = + 15
10000 = - 0
10001 = - 1
10010 = - 2
10011 = - 3
10100 = - 4
10101 = - 5
10110 = - 6
10111 = - 7
11000 = - 8
11001 = - 9
11010 = - 10
11011 = - 11
11100 = - 12
11101 = - 13
11110 = - 14
11111 = - 15
So the range is from -15 to +15 with *two* patterns for 0, 00000 and 10000.
In the "one's complement" representation the idea is that inverting the pattern of
a positive number should give the representation of the same number negated.
so
00000 = 0 so -0 must = 11111
00001 = 1 so -1 must == 11110
so we get
00000 = + 0
00001 = + 1
00010 = + 2
00011 = + 3
00100 = + 4
00101 = + 5
00110 = + 6
00111 = + 7
01000 = + 8
01001 = + 9
01010 = + 10
01011 = + 11
01100 = + 12
01101 = + 13
01110 = + 14
01111 = + 15
10000 = - 15
10001 = - 14
10010 = - 13
10011 = - 12
10100 = - 11
10101 = - 10
10110 = - 9
10111 = - 8
11000 = - 7
11001 = - 6
11010 = - 5
11011 = - 4
11100 = - 3
11101 = - 2
11110 = - 1
11111 = - 0
Note: Again the range is from -15 to +15 with two numbers for 0, 00000 and 11111.
00110+ in decimal 6 +
00101 5
----- ---
01011 11
now we say that 2's complement should give the effect that when a positive integer
is added to its negative equivalent we should get 0
if 1 = 00001
00001 + in decimal, 1 +
----- = -1 =
00000 0
with this condition we get - 1 being 11111 (note that leftmost carry is thrown
away)
00001+
11111
-----
00000
00010+
11110
------
00000
A shortcut to generate 2's complement is, invert the number, then add 1
decimal 2 is 00010
flip to get 11101
then add 1 to get
11110 = -2 in 2s complement.
So now the range is -16 to -15. Note 1: Zero has only one representation, 00000.
note 2: the negative range is one more (0 to -16) than the positive range (0 to 15)
The LC3 works with 16 bit values, which (with 2's complement representation) can
represent integers from -32768 to 32767
check
11000111
00111001 <-- this is what we need to convert into decimal magnitude
--------
00000000
so we get
Step 2 divide both sides by 2. (on the right side, ignore the right most bit,
consider only the n-1 bits from the left.
Got to step 1, till all bits are filled. pad as necessary with zeros to fill
the word size
Step 3 if decimal number is -ve, the leftmost digit should be 0, else add a
leftmost 0 *and take 2's complement*.
if decimal number is +ve, the left most digit should be 0. Done
-57 = _ _ _ _ _ _ _ _
==
div by 2 on both sides to get
[28 = b_7 * 2^6 + b_6 * 2^5 + b_5* 2^4 + b_4 * 2^3 + b_3 * 2^2 + b_2 * 2^1 + b_1 *
2^0] 1
[28 = b_7 * 2^6 + b_6 * 2^5 + b_5* 2^4 + b_4 * 2^3 + b_3 * 2^2 + b_2 * 2^1 + 0 *
2^0] 1
==
[28 = b_7 * 2^6 + b_6 * 2^5 + b_5* 2^4 + b_4 * 2^3 + b_3 * 2^2 + b_2 * 2^1 + 0] 1
[14 = b_7 * 2^5 + b_6 * 2^4 + b_5* 2^3 + b_4 * 2^2 + b_3 * 2^1 + b_2 * 2^0] 0 1
[14 = b_7 * 2^5 + b_6 * 2^4 + b_5* 2^3 + b_4 * 2^2 + b_3 * 2^1 + 0 * 2^0] 0 1
==
[14 = b_7 * 2^5 + b_6 * 2^4 + b_5 * 2^3 + b_4 * 2^2 + b_3 * 2^1 + 0 ] 0 1
[7 = b_7 * 2^4 + b_6 * 2^3 + b_5* 2^2 + b_4 * 2^1 + b_3 * 2^0] 0 0 1
divide by 2
[0 0 1]1 1 0 1 1
==
0 0 1 1 1 0 1 1
We take the 2's complement of this to get the representation for -57, getting,
-57 = 11000111
Binary to Decimal
Example
Decimal To Binary
Here we *multiply by two* and if the number on the left side of the number is < 1
we assign 0 to the co efficient of 2^0 and if it is 1 0, we assign one, and reduce
the number on the left by 1, and repeat (note: we *first* multiply by 2, as it
stands there is no coefficient of 2^0, since the binary digits in the beginning are
multiplied with 2^-1, 2^-2 etc
multiply by 2 give
0.842 = 0. [b_1 * 2^0 + b_2 * 2^(-1) + b_3 * 2^(-2) + b_4 * 2^(-3) ...]
multiply by 2 gives
multiply by 2 to get
number on left > 1, so b_3 is one, subtract one on each side to get
multiply by 2
0.736 = 0. 0 1 1 0 [...]
Note that converting 0.0110 = 1/4 + 1/8 = 0.25 + 0.125 = 0.375 which only
*approximates* to 0.421
With more digits in the binary number, the approximation will come closer.
adding -9 to 14 we get
14 01110 -
9 10111 is
5 00101
5 00101 +
5 00101
01010 = 10 decimal
iow the co-ordinate of 2^n in the old number is now the co-ordinate of 2^(n+1) in
the doubled number, with an extra zero padding the right
2.5.2 Sign-Extension
the basic idea is that
for positive 2s complement numbers, adding 0s to the left does not change the
number.
for negative 2s complement numbers, adding 1s to the left does not change the
number.
This fact can be used to pad the leftsides of 2's complement numbers, thus bringing
two operands of an operation to the same bit size.
2.5.3 Overflow
happens when the bit width is not sufficient to hold the result of an operation.
x y x AND y
0 0 0
0 1 0
1 0 0
1 1 1
we can apply the logical operation AND to two n bit operators. the AND operation is
applied independently to each pair of bits in the two operands
so
0011101001101001 AND
0101100100100001 is
0001100000100001
Suppose there is a bit pattern of which a subset of bits are important for some
reason.
We can AND the source bit pattern with a pattern which has all zeros except for
the bits of importance, where it is 1
and the AND operation will extract the required bits into a new pattern with all
other bits being zero
OR Truth Table
OR
x y x OR Y
0 0 0
0 1 1
1 0 1
1 1 1
x NOT X
0 1
1 0
2.6.5 The Exclusive-Or Function
An n bit pattern where each bit a logical value and operations are performed
bitwise is called a bit vector.
2.7
2.7.1 Floating Point Data Type (Greater Range, Less Precision)
Most arithmetic uses integer values. LC3 uses 16 bit 2 complement integers. The
data type uses 1 bit to identify positive or negative and the remaining 15 bits
hold the value. With 16 bits the value ranges from -32768 to +32767. We say the
precision is 15 bits and the range is 2^16
The range needed to express 10^23 is far larger than the largest positive value
2^15-1 that is possible with 16 bits (and 2s complement).
However the *precision* associated with 2's complement integer is *more* than what
we require to represent 10^23. We only need to represent 6022 and 23 as exponent
(assuming 10 as the base).
So the problem is we don't have enough range, but we do have more than enough
precision.
Its structure is
S: 1 bit for sign (+/-)
E: 8 bits for exponent
F: 23 bits for the fraction
The floating point data type *mostly* represents large numbers in the 'normalized
form'
where S, fraction and exponent are the components of the structure above.
The exponent bits (8 bits) use what is called an excess code. the idea = treat the
8 bits as an unsigned integer, then subtracting an excess value == bias value) .
For 32 bit integers this is 127.
Thus an exponent field containing 10000110 has a raw value of decimal unsigned
integer 134, from which we reduce 127 , getting 7.
Exponent field containing 00000111 has a raw value of 7, from which we reduce 127
to get -120.
Range of normalized form's *exponent*
largest number it can hold is (given the constraint on it) 254 = 11111110 which
gives us (after subtracting 127) 2^127
smallest number it holds (subject to range constraint) is 00000001, =
decimal 1, which after subtracting 127 gives us 2^-126
== -1.10101 * 2^2
now normalizing
so -1.1000000110101000000000000000000
If the exponent is 11111111 we use the floating point datatype to represent many
things, including infinity.
Infinity is represented by exponent containing all 1s and fraction field containing
all zeros.
sign bit represents positive or negative infinity.
N = 1.00000000000000000000000 × 2^−126
= 0.00000000000000000000001 × 2−126.
the largest is
N = 0.11111111111111111111111 × 2^−126
the basic idea is each 4 bit binary number is represented by it hex equivalent
so
3 D 6 E hex
Exercises