Title: Topics Covered in Chapter 4
1Chapter 4.1
2Topics Covered in Chapter 4
- How numbers (integers, decimal numbers) are
represented in the computer - How addition, subtraction, multiplication and
division are really implemented in the hardware - Logical operations
3Chapter 4.2
- Signed and Unsigned Numbers
4Representing Unsigned Numbers of Various Sizes
- A 32-bit MIPS word can represent 232 different
numbers ranging from 0 ? 232 ? 1, or 0 ?
4,294,967,295ten. - A 16-bit number can represent values ranging from
0 ? 216 ? 1, or 0 ? 65,535ten. - An 8-bit number can represent values ranging
from 0 ? 28 ? 1, or 0 ? 255ten.
5Representing Unsigned Numbers of Various Sizes
- Examples
- 0000 0000 0000 0000 1000 0010 0011 0110two
33,334ten - 1111 1111 1111 1111two 65,535ten
- 1001 1101two 157ten
6Representing Signed Numbers
- We need a representation that distinguishes
positive numbers from negative numbers, since
computer programs use both. - Some possible representations
- Sign and magnitude
- Ones complement
- Twos complement
7Sign and Magnitude Representation
- This representation is implemented by assigning a
specific bit to be the sign bit. - By convention, a zero in the sign bit means
that the integer represented is positive or zero
a one in the sign bit means that it is negative
or zero.
8Examples of Sign and Magnitude Representation
- Note assume an 8-bit number for these examples.
- Unsigned number representations
- 0000 1101two 13ten
- 1000 1101two 141ten
- Signed number representations
- 0000 1101two 13ten
- 1000 1101two ?13ten
Sign bit Size of number
9Sign and Magnitude Representation
- Range of 8-bit numbers that can be represented
- ?127 ? 127
- Or
- ? (27 ? 1) ? 27 ? 1
- Or
- 1111 1111 ? 0111 1111
Sign
Sign
10Sign and Magnitude Representation
- Disadvantage of this representation
- Two distinct representations for zero
(0000 0000, 1000 0000) - Advantage of this representation
- Additive inverse easily formed by inverting the
sign bit - Result of such a representation
- Increased complexity of addition and subtraction
(e.g., an extra step may be required to set the
sign)
11Complement Representation of Numbers
- Complement representation is motivated by the
need to minimize the complexity of addition and
subtraction. - There are two types of complement representation
- Ones complement
- Twos complement
12Ones Complement Representation
- Positive numbers
- The representation of a positive number is the
same as it is for sign and magnitude. - The most significant bit is set to zero.
- The remaining bits determine the size of the
number. - Example
- 0111 1111two 127ten (maximum 8-bit number)
13Ones Complement Representation
- Negative numbers
- The representation of a negative number is formed
by inverting each bit of the corresponding
positive number the resulting number is the
bitwise complement, or additive inverse, of the
positive value. - The most significant bit is equal to one.
- Example
- 1111 1100two ?3ten (explanation follows ...)
14Ones Complement Representation
- Example, continued
- 0000 0011two 3ten
- Inverting all the bits gives us
- 1111 1100two ?3ten
- Adding the two numbers together, we get
- 0000 0011two 3ten
- 1111 1100two ?3ten
- 1111 1111two 0ten
-
15Ones Complement Representation
- The range of 8-bit numbers that can be
represented is the same as it is for sign and
magnitude representation - ?127 ? 127
- There are still two distinct representations for
zero - 0000 0000two
- and
- 1111 1111two
-
16Twos Complement Representation
- Positive numbers
- The representation of a positive number is the
same as it is for ones complement. - The most significant bit is set to zero.
- The remaining bits determine the size of the
number. - Example
- 0001 0101two 21ten
17Twos Complement Representation
- Negative numbers
- The representation of a negative number is formed
by taking the ones, or bitwise, complement of
the corresponding positive number, then adding
one. - The most significant bit is equal to one.
- Example
- The bitwise complement of 0001 0101two (21ten)
- is 1110 1010two. Adding 1 to that value gives
us - 1110 1011two, the additive inverse of 0001
0101two. -
18Twos Complement Representation
- The range of 8-bit numbers that can be
represented is - ?128 ? 127 Note that there is one
- Or more negative value
- ? (27) ? 27 ? 1 than there are positive
- values.
- There is now only one representation for zero,
however - 0000 0000two
-
19Twos Complement Representation
- The twos complement representation of 127 is
- 0111 1111two
- The twos complement representation of its
opposite, ?127, is - 1000 0001two
- Adding 127 and ?127 together, we get
- 0111 1111two 127ten
- 1000 0001two ?127ten
- 1 0000 0000two 0ten
- Ignored (Why?)
-
20Twos Complement Representation
- The twos complement representation of ?128 is
- 1000 0000two
- The twos complement representation of its
opposite, 128, is - 0111 1111two
- 1two
- 1000 0000two ?????
- Conclusion
- The most negative 8-bit number has no additive
inverse within a fixed precision. This is an
example of overflow. -
21Twos Complement Representation
- The range of 32-bit numbers that can be
represented is - ?2,147,483,648 ? 2,147,483,647
- Or
- ? (231) ? 231 ? 1
- Once again, there is one more negative value than
there are positive values. - No, it doesnt have an additive inverse.
22Problems
- Give the designated 8-bit binary representation
of each of the following decimal numbers - 121 (unsigned)
- ?15 (sign and magnitude)
- 89 (ones complement)
- ?8 (twos complement)
23Problems
- Find the decimal equivalent of each of the
following binary numbers - 1000 0110two (unsigned representation)
- 1111 1000two (ones complement representation)
- 0010 1101two (sign and magnitude representation)
- 1110 1001two (twos complement representation)
24Converting an Integer Representation from One
Size to Another
- It is sometimes necessary to convert a binary
number represented in n bits to a number
represented with more than n bits.
25Converting an Integer Representation from One
Size to Another
- For example, the immediate field in the I-Format
instructions contains a twos complement 16-bit
number representing ?32,768 (?215) to 32,767
(215 ? 1). - To add the contents of the field to a 32-bit
register, the computer must convert that 16-bit
number to its 32-bit equivalent.
26Converting an Integer Representation from One
Size to Another
- How is this conversion implemented?
- Two steps are required
- The sign bit of the smaller quantity is
replicated to fill the new (unoccupied) bits of
the larger quantity. - The bits making up the original (smaller) value
are simply copied into the right half of the new
word.
27Converting an Integer Representation from One
Size to Another
- This conversion technique is commonly called sign
extension. - Lets look at the two examples on page 217 of
your text.
28Converting an Integer Representation from One
Size to Another ? More Examples ?
- Convert the value in the immediate field of the
following instruction to its 32-bit binary
representation. - sw t1, 8(sp) t1 ? 9
- sp ? 29
-
29Converting an Integer Representation from One
Size to Another ? More Examples ?
- Conversion of 8, the value in the immediate
field, to its 32-bit binary representation. -
- 8ten ? 0000 0000 0000 1000two (16-bit)
- 8ten ? 0000 0000 0000 0000 0000 0000 0000 1000two
-
(32-bit)
new bits
original bits
30Converting an Integer Representation from One
Size to Another ? More Examples ?
- Convert the value in the immediate field of the
following instruction to its 32-bit binary
representation. - addi sp, sp, ?4 sp ? 29
-
-
31Converting an Integer Representation from One
Size to Another ? More Examples ?
- Conversion of ?4, the value in the immediate
field, to its 32-bit binary representation. -
- ? 4ten ? 1111 1111 1111 1100two (16-bit)
- ? 4ten ? 1111 1111 1111 1111 1111 1111 1111
1100two -
(32-bit)
new bits
original bits