180 likes | 322 Vues
Computer Organization. Integers and Arithmetic Operations Terrence Mak. Counting to 9, oh no, 1 please. Binary numbers (0, 1) are commonly used in computers as they are easily represented as on/off electrical signals Other related radix systems are Octal & Hexadecimal
E N D
Computer Organization Integers and Arithmetic Operations Terrence Mak
Counting to 9, oh no, 1 please • Binary numbers (0, 1) are commonly used in computers as they are easily represented as on/off electrical signals • Other related radix systems are Octal & Hexadecimal • Different number (representation) systems are used in computers • Let's assume n bits (binary digits) are used in the following discussions
Only 0 and 1 in a Sequence • Numbers are represented as binary vectors B = bn-1bn-2 … b1 b0 • MSB = Most Significant Bit = bn-1i.e. leftmost digit in a binary vector • LSB = Least Significant Bit = b0 i.e. rightmost digit in a binary vector
Representing Non-negative Numbers • Unsigned (non-negative) numbers are in range 0 to 2n– 1 • Represented by Value(B) = bn-12n-1 + … + b121 + b020 if B is a binary vector representing an unsigned integer
Representing Signed Numbers • In written decimal system, a signed number is "usually" represented by a "dash" or "plus" sign and followed by the magnitude e.g. –73, –215, +349 • In binary system, we have several choices: • Sign-and-magnitude • 1’s complement • 2’s complement
Representing Signed Numbers • Sign-and-magnitude • MSB determines sign, remaining unsigned bits represent magnitude • MSB: 0 means "+", 1 means "–" • 1’s complement • MSB determines sign • To change sign from unsigned to negative, invert all the bits • 2’s complement (commonly used) • MSB determines sign • To change sign from unsigned to negative, invert all the bits and then add 1 • This is equivalent to subtracting the positive number from 2n
B (n = 4) V alues represented Sign and b b b b 1' s complement 2' s complement magnitude 3 2 1 0 + 7 + 7 + 7 0 1 1 1 + 6 + 6 + 6 0 1 1 0 + 5 + 5 + 5 0 1 0 1 + 4 + 4 + 4 0 1 0 0 + 3 + 3 + 3 0 0 1 1 + 2 + 2 + 2 0 0 1 0 + 1 + 1 + 1 0 0 0 1 + 0 + 0 + 0 0 0 0 0 - 0 - 7 - 8 1 0 0 0 - 1 - 6 - 7 1 0 0 1 - 2 - 5 - 6 1 0 1 0 - 3 - 4 - 5 1 0 1 1 - 4 - 3 - 4 1 1 0 0 - 5 - 2 - 3 1 1 0 1 - 6 - 1 - 2 1 1 1 0 - 7 - 0 - 1 1 1 1 1 Signed Number Systems
0 1 N - 1 N - 2 2 (a) Circle representation of integers modN 0000 1111 0001 1110 0010 0 - 1 + 1 - 2 + 2 1101 0011 - 3 + 3 - 4 + 4 1100 0100 - 5 + 5 1011 0101 - 6 + 6 - 7 + 7 - 8 1010 0110 1001 0111 1000 (b) Mod 16 system for 2's-complement numbers 2’s Complement • 2's complement numbers actually make sense since they follow normal modulo arithmetic except when they overflow • Range is –2(n–1) to +2(n–1) – 1 e.g. –8 to +7 for n = 4 Overflow cut-off for n = 4, N = 2n = 24 = 16
0 1 0 1 + 0 + 0 + 1 + 1 0 1 1 1 0 Carry-out 1-bit Addition
n-bit Addition/ Subtraction • X + Y • Use 1-bit addition propagating carry to the most significant bit • X – Y X + (– Y) • Add X to the 2’s complement of Y
( + 4 ) (a) 0 0 1 0 ( + 2 ) (b) 0 1 0 0 ( ) + 0 0 1 1 + 3 + 1 0 1 0 ( - 6 ) ( - 2 ) 0 1 0 1 ( + 5 ) 1 1 1 0 ( ) + 7 (c) 1 0 1 1 ( - 5 ) (d) 0 1 1 1 + 1 1 1 0 ( - 2 ) + 1 1 0 1 ( - 3 ) 1 0 0 1 ( - 7 ) 0 1 0 0 ( + 4 ) (e) 1 1 0 1 1 1 0 1 ( - 3 ) 1 0 0 1 + 0 1 1 1 - ( - 7 ) 0 1 0 0 ( + 4 ) (f) 0 0 1 0 ( + 2 ) 0 0 1 0 ( ) - 0 1 0 0 + 4 + 1 1 0 0 1 1 1 0 ( - 2 ) 4-bit Addition/ Subtractionof numbers represented in 2’s Complement
(g) 0 1 1 0 0 1 1 0 ( + 6 ) ( + 3 ) - 0 0 1 1 + 1 1 0 1 0 0 1 1 ( + 3 ) 1 0 0 1 (h) 1 0 0 1 ( - 7 ) 0 1 0 1 1 0 1 1 ( - 5 ) - + 1 1 1 0 ( - 2 ) (i) 1 0 0 1 ( - 7 ) 1 0 0 1 - 0 0 0 1 ( + 1 ) + 1 1 1 1 1 0 0 0 ( - 8 ) (j) 0 0 1 0 0 0 1 0 ( + 2 ) ) - 1 1 0 1 + 0 0 1 1 ( - 3 ( ) 0 1 0 1 + 5 4-bit Addition/ Subtractionof numbers represented in 2’s Complement
Sign Extension • Given a 4-bit 2’s complement number • Make it into an 8-bit 2's complement number • Positive number • add 0’s to LHS • e.g. 0111 00000111 • Negative number • add 1’s to LHS • e.g. 1010 11111010 • c.f. circle representation, i.e. extend the MSB
Overflow • In 2’s complement arithmetic • Addition of opposite sign numbers never overflow • If the numbers are the same sign and the result is the opposite sign, overflow has occurred • e.g. 0111 + 0100 = 1011 = –5! • positive + positive = negative?! • In unsigned arithmetic • Carry out signals an overflow
Exercise • Using 4-bit 2’s complement number system, • What is the binary for -2? • Calculate 2+3 • Calculate -2-3 • Calculate 5+5 • Using 5-bit number system • What is the largest 2’s complement number? • What is the smallest 2’s complement number? • What is the largest unsigned number? • Convert 56 to unsigned binary • What is the decimal value of 10110101 in 2’s complement? • What is the decimal value of unsigned number 10110101?