450 likes | 872 Views
Ch. 2.1 Data Representation. Unsigned and Signed Integers – representation, addition, subtraction. Data representation. High-level language types: unsigned int, int, char, float, double How are values represented in binary? Each type employs a data representation scheme.
E N D
Ch. 2.1 Data Representation Unsigned and Signed Integers – representation, addition, subtraction CS 251 -- Data representation
Data representation • High-level language types:unsigned int, int, char, float, double • How are values represented in binary? • Each type employs a data representation scheme CS 251 -- Data representation
Byte-addressable memory • Memory is a collection of bits • Grouped into bytes (8-bit chunks) • Organized sequentially • Each byte has a unique numerical address 0 1 2 3 Note: lower addresses at the top … … CS 251 -- Data representation
Text • American Standard Code for Information Interchange (ASCII) • 7-bit code for each character (See ASCII Table) How many different characters? • One character per byte, msb=0 • Example: ‘J’ code 1001010 byte 0x4a hexadecimal CS 251 -- Data representation
Ascii table: • Reference • ASCII Table.htm CS 251 -- Data representation
Text • Unicode (http://unicode.org/) • 8-bit, 16-bit, and 32-bit character codes millions of possible characters • All written languages • Backward compatible with ASCII CS 251 -- Data representation
Unsigned integers • Use a fixed number of bits (32) • Write the integer in binary, pad with leading 0’s if necessary • Example: 32-bit representation of 25 2511001 0000 0000 0000 0000 0000 0000 0001 1001 0x00000019 CS 251 -- Data representation
Range of unsigned integers Fixed # of bits limited range of numbers • With 8 bits, 28 = 256 bit patterns Number range: 0, 1, 2, … , 255 • With 32 bits: 232 = 4,294,967,296 bit patterns Number range: 0, …, 4294967295 (232 -1 ) CS 251 -- Data representation
Number range: 0, …, 4294967295? • How was the range calculated? • How to calculate the largest unsigned integer in 32 bits? • How many unsigned integers can be represented in 32 bits? CS 251 -- Data representation
Signed integers • Two’s-complement representation • Use fixed number of bits (32) • 4-bit example: Msb = 0 non-negative Msb = 1 negative 0000 0001 1111 0010 1110 0 -1 +1 -2 +2 0011 1101 -3 +3 +4 0100 -4 1100 +5 -5 +6 0101 -6 1011 +7 -7 -8 0110 1010 0111 1001 1000 CS 251 -- Data representation
Two’s-complement representation • How do I figure out the bit pattern for a non-negative number? • Write the number in binary • Pad with leading 0’s to the appropriate length CS 251 -- Data representation
Two’s-complement representation • How do I figure out the bit pattern for a negative number? • Write the positive number in binary, pad w/ 0’s • Perform the “two’s-complement” operation • Flip all the bits • Add 1 (discarding any carry out) • Example: 4-bit representation of -3 +3 11 0011 1100 1101 Flip bits Add 1 CS 251 -- Data representation
Example 32-bit two’s-complement representation of -13: +13 1101 00000000 00000000 00000000 00001101 11111111 11111111 11111111 11110010 11111111 11111111 11111111 11110011 Check data0.a out with xspim pad flip Add 1 CS 251 -- Data representation
Cool Fact • The two’s-complement operation is self-reversing • Example: 11111111 11111111 11111111 11110011 00000000 00000000 00000000 00001100 00000000 00000000 00000000 00001101 +13 • The two’s-complement operation negates the number flip Add 1 CS 251 -- Data representation
Two’s-complement operation visualized 3 -3 CS 251 -- Data representation
Range of Signed integers • With 32 bits: 231 non-negatives: 0, 1 ,2 ,…, 231-1 231 negatives: -1, -2, -3, …, -231 • With n bits: ____ non-negatives: 0, 1 ,2 ,…, _____ ____ negatives: -1, -2, -3, …, _____ CS 251 -- Data representation
Addition • Do binary addition, discarding any carry out • Works for unsigned representation. • Works for two’s-complement representation. • The computation has two interpretations CS 251 -- Data representation
Example (using 8 bits) Bit patterns: 10010011 +00101110 Unsigned interpretation: 147 +46 Two’s- Complement interpretation: -109 + 46 CS 251 -- Data representation
Why does addition work for both interpretations (within limits)? • Unsigned: obvious • Two’s-complement: consider x + -y • Start at x, move y positions counter-clockwise OR • Start at x, move z positions clockwise, where z is the bit pattern representing –y CS 251 -- Data representation
But Overflows may occur • Result of computation is out of range • Depends on interpretation • Example: (4 bits) 1111+0001 CS 251 -- Data representation
Overflow for Unsigned integer Addition • Unsigned overflow – result is “out of range” • Occurs if carryout of MSB is 1 Binary Check in decimal 1111 15 + 0100+4 (cout=1) 0011 3(?) (unsigned overflow occurs) CS 251 -- Data representation
Overflow conditions for addition and subtraction for signed numbers. CS 251 -- Data representation
Detection of overflows in (unsigned and signed) additions and subtractions: CS 251 -- Data representation
Overflow for signed integer addition? (a) • (a) Previous example 1111 -1 + 0100+4 (cout=1) 0011 3(?) (Cin =1) (NO signed overflow occurs) CS 251 -- Data representation
Overflow for signed integer addition (b) • (b) New example 0110 +6 +0110+6 (cout=0) 1100 - 4(?) (cin = 1) (Signed overflow occurs) CS 251 -- Data representation
Overflow for signed integer addition (c) • (c) New example 1010 -6 +1001+-7 (cout=1) 0011 3? (cin = 0) (signed overflow occurs) CS 251 -- Data representation
How do we detect overflow in signed integer addition? • Whenever 2 positive values are added and result is negative • Whenever 2 negative values are added and the result is positive • What if one operand is positive and the other is negative in addition? • Cout ≠Cin CS 251 -- Data representation
Detection of overflows in (unsigned and signed) additions and subtractions: CS 251 -- Data representation
Subtraction in computer hardware To compute x-y: • Perform two’s-complement operation on y • Add x and result of two’s-complement operation. • Works for unsigned representation • Works for two’s-complement representation • Instead of moving y positions counter-clockwise, move z positions clockwise CS 251 -- Data representation
Example (4 bits) 0101 -1001 0101 +0111 Unsigned interpretation? Two’s-complement interpretation? CS 251 -- Data representation
How does subtraction work for unsigned integers? • The same adder for unsigned addition is used for unsigned subtraction • A – B == A + (-B) == A + (2^n – B) 0101 5 - 1001 -9 0101 5 +0111 +(-9) (cout=0) 1100 12? (unsigned overflow) Note no carryout and the result is wrong. CS 251 -- Data representation
Overflow in unsigned integer subtraction 1001 9 - 0101 -5 1001 9 +1011 +-5 (cout=1) 0100 4 (correct answer) There is carryout and the result is correct! Carryout of MSB == 1 means no overflow in unsigned subtraction Carryout of MSB == 0 means overflow in unsigned subtraction CS 251 -- Data representation
Overflow in signed integer subtraction • No need to implement a separate subtractor • A – B = A + (-B) • Overflow may occur • Overflow detection same as overflow addition of signed integers • Whenever 2 positive values are added and result is negative • Whenever 2 negative values are added and the result is positive • Or Carryout of MSB Carryin of MSB CS 251 -- Data representation
Detection of overflows in (unsigned and signed) additions and subtractions: CS 251 -- Data representation
Arithmetic Logic Unit (ALU) hardware AddSubtract (1 bit; 0 = Add, 1 = Subtract) 32-bit ALU 32 bits a 32 bits a ± b 32 bits b CS 251 -- Data representation
1-bit adder cin + a s b cout CS 251 -- Data representation
Multi-bit adder 0 a0 + s0 b0 a1 s1 + b1 a2 s2 + b2 a3 s3 + b3 a4 s4 + b4 CS 251 -- Data representation
Subtraction with adder hardware • Want to compute: a – b • Add a and two’s-comp. of b Inverter 1 a0 + s0 b0 a1 s1 + b1 a2 s2 + b2 a3 s3 + b3 a4 s4 + b4 CS 251 -- Data representation
1-bit ALU with addition/subtraction capabilities Multiplexor AddSubtract Carry in select in0 a + out 0 1 a ± b b in1 0 1 Carry out CS 251 -- Data representation
Appendix: More about overflow • Some add/sub instructions cause overflows • Other add/sub instructions do not cause overflows? CS 251 -- Data representation
Overflow conditions for addition and subtraction for signed numbers. CS 251 -- Data representation
Instructions causing exceptions on overflow • Add (add) • Add Immediate (addi) • Subtract (sub) CS 251 -- Data representation
Unsigned integer Addition/Subtraction • Unsigned integers are commonly used for memory addresses where overflows are ignored. • Instructions do not cause exceptions on overflow: • Add unsigned (addu) • Add immediate unsigned (addiu) • Subtract unsigned (subu) CS 251 -- Data representation
Overflow conditions for addition and subtraction for signed numbers. CS 251 -- Data representation