Ch. 2.1 Data Representation - PowerPoint PPT Presentation

Loading...

PPT – Ch. 2.1 Data Representation PowerPoint presentation | free to download - id: 6f5b7b-MGZkM



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Ch. 2.1 Data Representation

Description:

Ch. 2.1 Data Representation Unsigned and Signed Integers representation, addition, subtraction – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 45
Provided by: UWO77
Learn more at: http://www.uwosh.edu
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Ch. 2.1 Data Representation


1
Ch. 2.1 Data Representation
  • Unsigned and Signed Integers representation,
    addition, subtraction

2
Data representation
  • High-level language typesunsigned int, int,
    char, float, double
  • How are values represented in binary?
  • Each type employs a data representation scheme

3
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


4
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, msb0
  • Example J? code 1001010 ? byte 0x4a

hexadecimal
5
Ascii table
  • Reference
  • ASCII Table.htm

6
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

7
Unsigned integers
  • Use a fixed number of bits (32)
  • Write the integer in binary, pad with leading 0s
    if necessary
  • Example 32-bit representation of 25
  • 25?11001?
  • 0000 0000 0000 0000 0000 0000 0001 1001
  • 0x00000019

8
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 )

9
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?

10
Signed integers
  • Twos-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
11
Twos-complement representation
  • How do I figure out the bit pattern for a
    non-negative number?
  • Write the number in binary
  • Pad with leading 0s to the appropriate length

12
Twos-complement representation
  • How do I figure out the bit pattern for a
    negative number?
  • Write the positive number in binary, pad w/ 0s
  • Perform the twos-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
13
Example
  • 32-bit twos-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
14
Cool Fact
  • The twos-complement operation is self-reversing
  • Example
  • 11111111 11111111 11111111 11110011
  • 00000000 00000000 00000000 00001100
  • 00000000 00000000 00000000 00001101
  • ?13
  • The twos-complement operation negates the number

flip
Add 1
15
Twos-complement operation visualized
3
-3
16
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, , _____

17
Addition
  • Do binary addition, discarding any carry out
  • Works for unsigned representation.
  • Works for twos-complement representation.
  • The computation has two interpretations

18
Example (using 8 bits)
  • Bit patterns
  • 10010011
  • 00101110

Unsigned interpretation 147 46

Twos- Complement interpretation -109 46

19
Why does addition work for both interpretations
(within limits)?
  • Unsigned obvious
  • Twos-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

20
But Overflows may occur
  • Result of computation is out of range
  • Depends on interpretation
  • Example (4 bits) 11110001

21
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
  • (cout1) 0011 3(?)
  • (unsigned overflow occurs)

22
Overflow conditions for addition and subtraction
for signed numbers.
Operation Operand A Operand B Result indicating overflow
A B 0 0 lt 0
A B lt 0 lt 0 0
A - B 0 lt 0 lt 0
A - B lt 0 0 0
23
Detection of overflows in (unsigned and signed)
additions and subtractions
unsigned signed
Addition Cout of MSB 1
Subtraction
24
Overflow for signed integer addition? (a)
  • (a) Previous example
  • 1111 -1
  • 0100 4
  • (cout1) 0011 3(?)
  • (Cin 1) (NO signed overflow
  • occurs)

25
Overflow for signed integer addition (b)
  • (b) New example
  • 0110 6
  • 0110 6
  • (cout0) 1100 - 4(?)
  • (cin 1) (Signed overflow occurs)

26
Overflow for signed integer addition (c)
  • (c) New example
  • 1010 -6
  • 1001 -7
  • (cout1) 0011 3?
  • (cin 0) (signed overflow occurs)

27
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

28
Detection of overflows in (unsigned and signed)
additions and subtractions
unsigned signed
Addition Cout of MSB 1 Cout of MSB ? Cin of MSB
Subtraction
29
Subtraction in computer hardware
  • To compute x-y
  • Perform twos-complement operation on y
  • Add x and result of twos-complement operation.
  • Works for unsigned representation
  • Works for twos-complement representation
  • Instead of moving y positions counter-clockwise,
    move z positions clockwise

30
Example (4 bits)
  • 0101
  • -1001

0101 0111
Unsigned interpretation?
Twos-complement interpretation?
31
How does subtraction work for unsigned integers?
  • The same adder for unsigned addition is used for
    unsigned subtraction
  • A B A (-B) A (2n B)
  • 0101 5
  • - 1001 -9
  • 0101 5
  • 0111 (-9)
  • (cout0) 1100 12? (unsigned
    overflow)
  • Note no carryout and the result is wrong.

32
Overflow in unsigned integer subtraction
  • 1001 9
  • - 0101 -5
  • 1001 9
  • 1011 -5
  • (cout1) 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

33
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

34
Detection of overflows in (unsigned and signed)
additions and subtractions
unsigned signed
Addition Cout of MSB 1 Cout of MSB ? Cin of MSB
Subtraction Cout of MSB 0 Cout of MSB ? Cin of MSB
35
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
36
1-bit adder
cin

a
s
b
cout
37
Multi-bit adder
0
a0

s0
b0
a1
s1

b1
a2
s2

b2
a3
s3

b3
a4
s4

b4
38
Subtraction with adder hardware
  • Want to compute a b
  • Add a and twos-comp. of b

Inverter
1
In out
0 1
1 0
a0

s0
b0
a1
s1

b1
a2
s2

b2
a3
s3

b3
a4
s4

b4
39
1-bit ALU with addition/subtraction capabilities
Multiplexor
AddSubtract
Carry in
select
in0

a
out
0 1
a b
b
in1
0 1
Select out
0 in0
1 in1
Carry out
40
Appendix More about overflow
  • Some add/sub instructions cause overflows
  • Other add/sub instructions do not cause overflows?

41
Overflow conditions for addition and subtraction
for signed numbers.
Operation Operand A Operand B Result indicating overflow
A B 0 0 lt 0
A B lt 0 lt 0 0
A - B 0 lt 0 lt 0
A - B lt 0 0 0
42
Instructions causing exceptions on overflow
  • Add (add)
  • Add Immediate (addi)
  • Subtract (sub)

43
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)

44
Overflow conditions for addition and subtraction
for signed numbers.
Operation Operand A Operand B Result indicating overflow
A B 0 0 lt 0
A B lt 0 lt 0 0
A - B 0 lt 0 lt 0
A - B lt 0 0 0
About PowerShow.com