190 likes | 434 Views
Representing Strings. ASCII (or UNICODE) characters linear sequences of characters, terminated by special character code (0). Representing Numbers. Integers Positive values Negative values Rationals Reals. Representing Integers Unsigned integers.
E N D
Representing Strings • ASCII (or UNICODE) characters • linear sequences of characters, terminated by special character code (0) Ch.4 - Computer Arithmetic
Representing Numbers • Integers • Positive values • Negative values • Rationals • Reals Ch.4 - Computer Arithmetic
Representing IntegersUnsigned integers 0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0001two = 1ten 0000 0000 0000 0000 0000 0000 0000 0010two = 2ten … 1111 1111 1111 1111 1111 1111 1111 1111two = 4,294,967,295ten Ch.4 - Computer Arithmetic
Representing IntegersAddition of positive values 0001two = 1ten 1111two = 15ten + 0110two = 6ten + 0011two = 3ten 0111two = 7ten 0010two = 2ten (overflow!) But what about subtraction? Ch.4 - Computer Arithmetic
Representing IntegersSubtraction of positive values 0111two = 7ten 0111two = 7ten - 0001two = 1ten + ????two = -1ten 0110two = 6ten 0110two = 6ten Ch.4 - Computer Arithmetic
Representing IntegersSign bit and magnitude 0 000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0 000 0000 0000 0000 0000 0000 0000 0001two = 1ten 0 000 0000 0000 0000 0000 0000 0000 0010two = 2ten … 0 111 1111 1111 1111 1111 1111 1111 1111two = 2,147,483,647ten 1 111 1111 1111 1111 1111 1111 1111 1111two = -2,147,483,647ten … 1 000 0000 0000 0000 0000 0000 0000 0001two = -1ten 1 000 0000 0000 0000 0000 0000 0000 0000two = -0ten Ch.4 - Computer Arithmetic
Representing IntegersSubtraction -Sign bit and magnitude 0 111two = 7ten 0 111two = 7ten - 0 001two = 1ten + 1 001two = -1ten 0 110two = 6ten 0 110two = 6ten Complicated logic required Ch.4 - Computer Arithmetic
Representing IntegersSigned -One’s Complement • If x is positive, convert x to binary • If x is negative • Write the positive value of x in binary • Reverse each bit Ch.4 - Computer Arithmetic
Representing IntegersSigned -One’s Complement 0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0001two = 1ten 0000 0000 0000 0000 0000 0000 0000 0010two = 2ten … 0111 1111 1111 1111 1111 1111 1111 1111two = 2,147,483,647ten 1000 0000 0000 0000 0000 0000 0000 0000two = -2,147,483,647ten … 1111 1111 1111 1111 1111 1111 1111 1110two = -1ten 1111 1111 1111 1111 1111 1111 1111 1111two = -0ten Ch.4 - Computer Arithmetic
Representing IntegersSubtraction -One’s Complement 0111two = 7ten 0111two = 7ten - 0001two = 1ten +1110two = -1ten 0110two = 6ten 0110two = 6ten Alas 0111two + 1110two = 0111two ! Ch.4 - Computer Arithmetic
Representing IntegersSigned -Two’s Complement 0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0001two = 1ten 0000 0000 0000 0000 0000 0000 0000 0010two = 2ten … 0111 1111 1111 1111 1111 1111 1111 1111two = 2,147,483,647ten 1000 0000 0000 0000 0000 0000 0000 0000two = -2,147,483,648ten … 1111 1111 1111 1111 1111 1111 1111 1110two = -2ten 1111 1111 1111 1111 1111 1111 1111 1111two = -1ten Ch.4 - Computer Arithmetic
Representing IntegersSigned -Two’s Complement • If x is positive, convert x to binary • If x is negative • Write the positive value of x in binary • Reverse each bit • Add 1 to the result Ch.4 - Computer Arithmetic
Representing IntegersSubtraction -Two’s Complement 0111two = 7ten 0111two = 7ten - 0001two = 1ten +1111two = -1ten 0110two = 6ten 0110two = 6ten Ch.4 - Computer Arithmetic
Representing IntegersSigned and Unsigned • Need both signed (2’s complement) and unsigned (always positive) integer values • Integers are also memory addresses • and addresses are never negative Ch.4 - Computer Arithmetic
MIPS Instructions (Arithmetic)(so far…) Ch.4 - Computer Arithmetic
MIPS Instructions (Cond. Branch)(so far…) Ch.4 - Computer Arithmetic
Representing IntegersSigned -Two’s Complement Negating values • Reverse each bit • Add 1 to the result Ch.4 - Computer Arithmetic
Representing IntegersSigned -Two’s Complement Sign Extension Converting an n-bit binary number to a longer m-bit number • Take most significant bit (nth bit from right) and duplicate it in the m-n new bits Ch.4 - Computer Arithmetic
Representing IntegersSigned -Two’s Complement Detecting Overflow When result cannot fit in representation Ch.4 - Computer Arithmetic