Topic 3a Twos Complement Representation - PowerPoint PPT Presentation

About This Presentation
Title:

Topic 3a Twos Complement Representation

Description:

Number and its representation are not the same thing! Representation is a convention defined for ... Excercises. What is 2's complement representation of 3 ? ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 21
Provided by: PGao
Category:

less

Transcript and Presenter's Notes

Title: Topic 3a Twos Complement Representation


1
Topic 3aTwos Complement Representation
  • Introduction to Computer Systems Engineering
  • (CPEG 323)

2
2s Complement
  • Number and its representation are not the same
    thing!
  • Representation is a convention defined for
    expressing the number.
  • A number may have different representations, e.g.
    BCD, hexadecimal, 2s complement, etc.
  • 2s complement a kind of representation defined
    as follows

3
Excercises
  • What is 2s complement representation of 3 ?

Answer 3 0011 gt 3 0x(-23) 0011 011
How about 2s complement representation of -3 ?
Answer -3 -(0011) gt - 3 gt 1x(-23) 0011
-5 gt 1101
4
2s Complement
bn-1 bn-2 b1b0
5
Signed Integers(2s Complement)
(For 4 bits)
Reference Katz Contemporary Logic Design, p243
0
-1
1
-2
0000
1111
1110
0001
-3
2
1101
0010
-4
3
0011
1100
Why 1111 and 0000 are adjacent? What is the
meaning of bit 3? What is the sign of 0? What is
the smallest number? What is the biggest number?
-5
1011
4
0100
1010
0101
-6
5
1001
0110
6
-7
0111
1000
7
-8
6
2s Complement (Cont.)
  • Representing a negative number x
  • From the representation of x to that of x

Invert each bi, then add 1
  • Proof of correctness an exercise!
  • Shortcut Invert every bit and add 1 to the
    least significant bit
  • Example - (0100) 1011 1 1100
  • Example 10010 01100100 ? 10011100 -10010

7
Fit a shorter number to longer bits
  • Why is it necessary?
  • Compare an integer with a long integer
    typecasting
  • Load a byte to a word
  • To add an immediate field to a 32-bit number
  • MIPS ALU only works with values in 32-bit
    registers
  • How to deal with smaller sizes?
  • What about larger sizes?

8
Fit a shorter number to longer bits
  • Example (2-bit) 10 1 (-21)0 20-210
  • Representing -210 in 4-bit word Can you do
    that? What is the intuition?
  • copy the sign bit into the other bits (sign
    extension) 0010 -gt 0000 0010 1010 -gt 1111
    1010
  • Proof? Are 1010 1111 1010 ?

9
Addition Subtraction
  • Addition Just like in grade school 0110
     0001
  • Substraction a-ba(-b)
  • Two's complement operations easy
  • 0111-0110 0111 1010
  • 01

1
1
0
0
1
  • So 0111-0110 0001
  • Have you specially handled the sign bits?

10
Addition Subtraction (Cont.)
  • Wait! There is one extra bit lost!
  • 0111-0110 0111 1010
  • 10001
  • Why the 1 can be omitted?
  • Because

1
1
is
But treated in the addition as
023 1(-23) 123 0
023 123 123 10
  • 1-1110 from the viewpoint of 1-bit.
  • So as long as you do not consider the carry
    brought by 11 (the green 1), you are right!

11
Addition Subtraction (Cont.)
  • Questions
  • Do you have an adder and a subtractor?
  • What about unsigned numbers? (Another adder?)
  • Advantage of using 2s complement
  • Sub can share the same logic as add
  • Sign bit can be treated as a normal number bit in
    addition.
  • These are the clever points!

12
Overflow
  • Addition 0111 1111  0110
    1000

Overflow!
overflow!
  • What is the sign of the input?
  • What is the sign of the output?
  • Can overflow occur in adding a positive and a
    negative number?
  • Can overflow occur with 0?
  • Consider the operations A B, and A B

13
Overflow
  • Addition 0111 1111  0110
    1000
  • 1101 10111

What is the sign of the input? What is
the sign of the output?
14
Detecting Overflow
  • Overflow occurs when
  • add two positives yields a negative
  • add two negatives gives a positive
  • or, subtract a negative from a positive and get a
    negative
  • or, subtract a positive from a negative and get a
    positive
  • What about addition and subtraction of unsigned
    numbers?

15
What should CPU do about overflow?
  • Ignore it?
  • Don't always want to detect overflow
  • Addu, addiu, subu (MIPS do not generate a
    overflow)
  • Generate a trap so that the programmer can try to
    deal with it?
  • An exception (interrupt) occurs
  • Control jumps to predefined address for exception
  • Interrupted address is saved for possible
    resumption
  • Add, sub

16
Instructions
  • Comparison
  • Unsigned numbers
  • sltu set on less than unsigned
  • sltiu set on less than immediate unsigned
  • Signed numbers
  • slt set on less than
  • slti set on less than immediate
  • Example What are the values of t0 and t1?
  • s0 1111 1111 1111 1111 1111 1111 1111 1111
  • s1 0000 0000 0000 0000 0000 0000 0000 0001
  • (1) slt t0, s0, s1 (2) sltu t1, s0, s1

17
Example (contd)
  • What are the values of s0 and s1?
  • s0 1111 1111 1111 1111 1111 1111 1111 1111
  • s1 0000 0000 0000 0000 0000 0000 0000 0001
  • Answer
  • slt t0, s0, s1 -- the result of t0 is 1
    if both are signed
  • sltu t1, s0, s1 -- the result of t1
    is 0 if both are unsigned

18
Instructions (Cont.)
  • Load/Store
  • lb load byte
  • lbu load byte unsigned
  • Example lb s1, 100(s2)
  • What is the values of s1
  • When memorys2100 0000 0000?
  • When memorys2100 0000 0001?
  • When memorys2100 1111 1111?
  • Example lbu s1, 100(s2)
  • What is the values of s1
  • when memorys2100 1111 1111?

19
Remarks
  • MIPS 4000 has "Integer Arithmetic Overflow
    exception.
  • For most of the processors today, integer
    overflow will not trigger interrupt. Instead,
    some status bits will be set such that software
    may detect such situation.

20
Remarks (contd)
  • Usually, a typical architecture may have status
    bits like
  • N - not zero
  • Z - zero
  • S - sign
  • O overflow
  • The 4 bits make 16 combinations. They can be used
    to represent 16 situations (for compare and
    conditional branch), which includes
  • signed gt, , lt, !
  • unsigned gt, , lt, !
  • integer overflow, underflow, and etc.
Write a Comment
User Comments (0)
About PowerShow.com