Download presentation
Presentation is loading. Please wait.
1
COMS 361 Computer Organization
Title: Number Representations Date: 10/28/2004 Lecture Number: 16
2
Announcements Homework 7 Due Tuesday, 11/02/2004
3
Review Addressing modes Representing numbers in binary
Unsigned (magnitude) Sign-magnitude Addition Subtraction
4
Outline Representing numbers in binary Problems One’s complement
Unsigned (magnitude) Sign-magnitude One’s complement Two’s complement
5
Problems Unsigned (magnitude) Sign-magnitude
No representation for negative numbers Sign-magnitude Two representations for zero, +0 and -0 Arithmetic operations do not always produce the correct answer in the sign-magnitude representation
6
One’s compliment Positive integers have the most significant bit (leftmost) equal to 0 The magnitude of positive numbers is the same as the signed magnitude representation Negative integers have the most significant bit equal to 1 Negative numbers in n-bit one’s complement has the same binary representation as the unsigned binary number given by:
7
One’s compliment Unsigned decimal One’s complement Signed decimal 000
000 1 001 2 010 3 011 4 100 -3 5 101 -2 6 110 -1 7 111 -0 -3 = 23 – 1 – 3 = 8 – 4 = 4 -2 = 23 – 1 – 2 = 8 – 3 = 5 -1 = 23 – 1 – 1 = 8 – 2 = 6 -0 = 23 – 1 – 0 = 8 – 1 = 7
8
One’s compliment Example: let n = 8, and N = 5
There is something special here and in the previous table Negatives are made from the positive by inverting each bit! Simpler hardware for arithmetic operations Still two representations of zero
9
One’s complement
10
One’s complement Mathematical operations sometimes give an incorrect result using this representation Two representations of zero 4 – 3 = = 1 4 – 2 = = 2 1 1 +(-3) +(-2) 1 1 2 1 1 1
11
Two’s Complement Positive integers have the most significant bit (leftmost) equal to 0 The magnitude of positive numbers is the same as signed magnitude and one’s complement representations Negative integers have the most significant bit (leftmost) equal to 1 Negative numbers in n-bit one’s complement has the same binary representation as the unsigned binary number given by:
12
Two’s Complement Example: let n = 8, and N = 5
There is a relationship between one’s and two’s complement numbers ones’ complement two’s complement Two’s complement representation can be computed from a numbers one’s complement representation simple by adding one
13
Two’s Complement It is simple to determine the representation of a negative number in one’s complement given the unsigned (magnitude) representation Use the formula Invert the bits It is easy to convert a one’s complement representation to a two’s complement representation by simply adding 1 to the one’s complement representation
14
Two’s Complement
15
Two's Complement Subtraction
Two's complement operations easy Subtraction using addition of negative numbers X - Y = X + (-Y) Build an adder for addition, use it for subtraction! X = 6, Y = 5 X-Y = 6+(-5) 0110 +1011 10001
16
Arithmetic Two's complement mathematical operations
Compute the correct answer In the two’s complement representation Two’s complement is used in nearly all computers today X = 4, Y = 3 X - Y = = 4 + (-3) 0100 +1101 0001 X = 5, Y = 6 X - Y = = 5 + (-6) 0101 +1010 1111
17
Two’s complement Sign Bit Sign Extension
All negative numbers have a 1 in the most significant bit Hardware need test only this bit to determine if the number is positive or negative Sign Extension Duplicate the sign in all bits to the left in a larger representation of a number > >
18
Two’s complement Load byte (lb) assumes an signed byte and sign extends the 24 left most bits Load byte unsigned (lbu) do not sign extend
19
Two’s complement Addresses Signed integers represent values
Start at 0 and continue to the largest address Represented with an unsigned integer Signed integers represent values Important when comparing two numbers A 1 in the sign bit may mean the number is negative and is less than all numbers with a 0 in the sign bit A 1 in the sign bit may mean the number is positive and larger than all numbers with a 0 in this position
20
Two’s complement Comparing numbers Signed comparison
Set on less than (slt) Set on less than immediate (slti) Unsigned comparison Set on less than unsigned (sltu) Set on less than unsigned immediate (sltui)
21
Two’s complement Overflow A value to large for its representation
Represent the number 300 using 8 bits The sign bit is 0 when the number is negative or a 1 when the number is positive
22
Two's ComplementOverflow
Overflow cannot occur Adding two operands of opposite sign Both operands must be represented Sum can be no larger than one of the operands Subtracting two operands of the same sign Subtraction done by negating and add Adding numbers of opposite sign
23
Two's Complement Overflow
Overflow can occur Adding two positive numbers with a negative result Adding two negative numbers with a positive result Needed one extra bit to contain the sign Real sign bit is set to a value not to a sign Only the sign bit is wrong Carry out into the sign bit 1 4-bit two’s complement numbers
24
Two's Complement Overflow
Overflow can occur Subtracting a negative number from a positive number with a negative result Subtracting a positive number from a negative number with a positive result
25
Unsigned Overflow Ignore overflow in unsigned numbers
MIPS has different arithmetic instructions for two’s complement and unsigned numbers Signed (Overflow) add, addi, sub (Unsigned) No overflow addu, addiu, subu C/C++ ignores overflow Compilers always generate unsigned instructions
26
Overflow detection MIPS using an exception (interrupt)
Unscheduled function call Next instruction executed is the first in the exception routine Usually a predefined address Upon completion of the exception, normal program execution continues
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.