310 likes | 468 Views
Chapter 4.1. Introduction. Topics 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. Chapter 4.2. Signed and Unsigned Numbers.
E N D
Chapter 4.1 Introduction
Topics 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
Chapter 4.2 Signed and Unsigned Numbers
Representing 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.
Representing 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
Representing Signed Numbers • We need a representation that distinguishes positive numbers from negative numbers, since computer programs use both. • Some possible representations: • Sign and magnitude • One’s complement • Two’s complement
Sign 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.
Examples 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
Sign 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
Sign 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)
Complement 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: • One’s complement • Two’s complement
One’s 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)
One’s 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 ...)
One’s 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
One’s 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
Two’s Complement Representation • Positive numbers • The representation of a positive number is the same as it is for “one’s complement”. • The most significant bit is set to zero. • The remaining bits determine the size of the number. • Example: 0001 0101two = 21ten
Two’s Complement Representation • Negative numbers • The representation of a negative number is formed by taking the one’s, 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.
Two’s 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
Two’s Complement Representation • The two’s complement representation of +127 is: 0111 1111two The two’s 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?)
Two’s Complement Representation • The two’s complement representation of 128 is: 1000 0000two The two’s 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.
Two’s 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 doesn’t have an additive inverse.
Problems • Give the designated 8-bit binary representation of each of the following decimal numbers: • 121 (unsigned) • 15 (sign and magnitude) • 89 (one’s complement) • 8 (two’s complement)
Problems • Find the decimal equivalent of each of the following binary numbers: • 1000 0110two (unsigned representation) • 1111 1000two (one’s complement representation) • 0010 1101two (sign and magnitude representation) • 1110 1001two (two’s complement representation)
Converting 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.
Converting an Integer Representation from One Size to Another • For example, the immediate field in the I-Format instructions contains a two’s 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.
Converting 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.
Converting an Integer Representation from One Size to Another • This conversion technique is commonly called sign extension. • Let’s look at the two examples on page 217 of your text.
43 29 9 8 Converting 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
43 29 9 8 Converting 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
8 29 29 4 Converting 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
8 29 29 4 Converting 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