Upload
dokiet
View
216
Download
2
Embed Size (px)
Citation preview
1성균관대소프트웨어대학신동군
Computer Architecture
Chapter 3-1
Arithmetic for computers
2성균관대소프트웨어대학신동군
• 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
• Overflow (result too large for finite computer word):
– e.g., adding two n-bit numbers does not yield an n-bit number
0111
+ 0001 note that overflow term is somewhat misleading,
1000 it does not mean a carry “overflowed”
Addition & Subtraction
3성균관대소프트웨어대학신동군
• 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
• Consider the operations A + B, and A – B
– Can overflow occur if B is 0 ?
– Can overflow occur if A is 0 ?
Detecting Overflow
4성균관대소프트웨어대학신동군
• An exception (interrupt) occurs
– Save PC in exception program counter (EPC) register
• mfc0 (move from coprocessor reg) instruction can retrieve EPC
value, to return after corrective action
– Control jumps to predefined address for exception
• Details based on software system / language
– example: flight control vs. homework assignment
• Don't always want to detect overflow
– Overflow MIPS instructions: add, addi, sub
– No overflow MIPS instructions: addu, addiu, subu
note: addiu still sign-extends! (negative constant)
note: sltu, sltiu for unsigned comparisons
Effects of Overflow
5성균관대소프트웨어대학신동군
Arithmetic for Multimedia
• Graphics and media processing operates on vectors of 8-bit and 16-bit data
– Use 64-bit adder, with partitioned carry chain
• Operate on 8×8-bit, 4×16-bit, or 2×32-bit vectors
– SIMD (single-instruction, multiple-data)
• Saturating operations
– On overflow, result is largest representable value
• c.f. 2s-complement modulo arithmetic
– E.g., clipping in audio, saturation in video
6성균관대소프트웨어대학신동군
Half Adder (1-bit)
A B S(um) C(arry)
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
HalfAdder
A B
S
C
7성균관대소프트웨어대학신동군
Half Adder (1-bit)
A B S(um) C(arry)
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
AB C
BABABAS
A
BSum
Carry
8성균관대소프트웨어대학신동군
Full Adder
Cin A B S(um) Cout
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
FullAdder
A B
S
Cout
Carry In(Cin)
9성균관대소프트웨어대학신동군
Full Adder
Cin A B S(um) Cout
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
00 01 11 10
00 1 0 1
11 0 1 0
CinAB
BACin
B)(ACin)BACin(
)BABA(CinAB)BACin(
BACinCinABBACinBACinS
00 01 11 10
00 0 1 0
10 1 1 1
CinAB
ABCinACinBCout
00 01 11 10
00 0 1 0
10 1 1 1
CinAB
B)Cin(AAB)BABACin(ABCout
Or
10성균관대소프트웨어대학신동군
Full Adder
BACinS
A
B
Cin
Cout
S
H.A. H.A.
B)Cin(AABCout
11성균관대소프트웨어대학신동군
Full Adder
Cout
S
HalfAdder
S
C
A
B
HalfAdder
S
C
A
BB
A
Cin
BACinS
B)Cin(AABCout
12성균관대소프트웨어대학신동군
4-bit Ripple Adder using Full Adder
FullAdder
A B
CinCout
S
S0
A0 B0
FullAdder
A B
CinCout
S
S1
A1 B1
FullAdder
A B
CinCout
S
S2
A2 B2
FullAdder
A B
CinCout
S
S3
A3 B3
Carry
A
BS
C
Half Adder
A
B
Cin
Cout
S
H.A. H.A.
Full Adder
13성균관대소프트웨어대학신동군
Subtractor Design
• A – B = A + (-B)
– Take 2’s complement of B
– Perform addition of A and 2’s complement of B
FullAdder
A B
CinCout
S
S0
A0
FullAdder
A B
CinCout
S
S1
A1
FullAdder
A B
CinCout
S
S2
A2
FullAdder
A B
CinCout
S
S3
A3
B0B1B2B3
C
Subtract