Arithmetic - PowerPoint PPT Presentation

About This Presentation
Title:

Arithmetic

Description:

negative numbers are obtained by inverting all bits and adding 1. Advantages and Disadvantages ... No overflow when adding a positive and a negative number ... – PowerPoint PPT presentation

Number of Views:137
Avg rating:3.0/5.0
Slides: 20
Provided by: scie226
Category:

less

Transcript and Presenter's Notes

Title: Arithmetic


1
Arithmetic
  • CPSC 321 Computer Architecture
  • Andreas Klappenecker

2
Overview
  • Number representations
  • Overflows
  • Floating point numbers
  • Arithmetic logic units

3
Unsigned Numbers
  • 32 bits are available
  • Range 0..232 -1
  • 11012 232220 1310
  • Upper bound 232 1 4 294 967 295

4
Number representations
  • What signed integer number
  • representations do you know?

5
Signed Numbers
  • Sign-magnitude representation
  • MSB represents sign, 31bits for magnitude
  • Ones complement
  • Use 0..231-1 for non-negative range
  • Invert all bits for negative numbers
  • Twos complement
  • Same as ones complement except
  • negative numbers are obtained by inverting all
    bits and adding 1

6
Advantages and Disadvantages
  • sign-magnitude representation
  • ones complement representation
  • twos complement representation

7
Signed Numbers (3bits)
8
Twos complement
  • The unsigned sum of an n-bit number its negative
    yields?
  • Example with 3 bits
  • 0112
  • 1012
  • 10002 2n gt negate(x) 2n-x
  • Explain ones complement

9
MIPS 32bit signed numbers
  • 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 1110two
    2,147,483,646ten
  • 0111 1111 1111 1111 1111 1111 1111 1111two
    2,147,483,647ten
  • 1000 0000 0000 0000 0000 0000 0000 0000two
    2,147,483,648ten
  • 1000 0000 0000 0000 0000 0000 0000 0001two
    2,147,483,647ten
  • 1000 0000 0000 0000 0000 0000 0000 0010two
    2,147,483,646ten
  • ...
  • 1111 1111 1111 1111 1111 1111 1111 1101two
    3ten
  • 1111 1111 1111 1111 1111 1111 1111 1110two
    2ten
  • 1111 1111 1111 1111 1111 1111 1111 1111two
    1ten

10
Conversions
  • How do you convert an n-bit number
  • into a 2n-bit number?
  • (Assume twos complement representation)

11
Conversions
  • Suppose that you have 3bit twos complement
    number
  • 1012 -3
  • Convert into a 6bit twos complement number
  • 1111012 -3
  • Replicate most significant bit!

12
Comparisons
  • What can go wrong if you accidentally
  • compare unsigned with signed numbers?

13
Comparisons for unsigned
  • Register s0
  • 1111 1111 1111 1111 1111 1111 1111 1111
  • Register s1
  • 0000 0000 0000 0000 0000 0000 0000 0001
  • Compare registers (set less than)
  • slt t0, s0, s1 yes, since 1 lt 1
  • sltu t1, s0, s1 no, since 232-1gt1

14
Addition Subtraction
  • Just like in grade school (carry/borrow 1s)
    0111 0111 0110  0110 - 0110 - 0101
  • Two's complement operations easy
  • subtraction using addition of negative numbers
    0111  1010

15
Overflow
  • Overflow means that the result is too large for
  • a finite computer word
  • for example, adding two n-bit numbers does not
    yield an n-bit number 0111  0001
  • 1000
  • the term overflow is somewhat misleading

16
Detecting Overflow
  • No overflow when adding a positive and a negative
    number
  • No overflow when signs are the same for
    subtraction
  • Overflow occurs when the value affects the sign
  • overflow when adding two positives yields a
    negative
  • or, adding 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

17
Detecting Overflow
18
Effects of Overflow
  • An exception (interrupt) occurs
  • Control jumps to predefined address for exception
  • Interrupted address is saved for possible
    resumption
  • Don't always want to detect overflow
  • MIPS instructions addu, addiu, subu note
    addiu still sign-extends!

19
What next?
  • More MIPS assembly operations
  • How does an ALU work?
  • Simple digital logic design
  • How can we speed-up addition?
  • What about multiplication?
Write a Comment
User Comments (0)
About PowerShow.com