week 7.1spring 2005 14:332:331 computer architecture and assembly language spring 2005 week 7...

56
Week 7.1 Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary Jane Irwin’s PSU CSE331 slides]

Post on 22-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.1 Spring 2005

14:332:331Computer Architecture and Assembly Language

Spring 2005

Week 7

[Adapted from Dave Patterson’s UCB CS152 slides and

Mary Jane Irwin’s PSU CSE331 slides]

Page 2: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.2 Spring 2005

MIPS arithmetic instructions

Instruction Example Meaning Commentsadd add $1,$2,$3 $1 = $2 + $3 3 operands; exception possiblesubtract sub $1,$2,$3 $1 = $2 – $3 3 operands; exception possibleadd immediate addi $1,$2,100 $1 = $2 + 100 + constant; exception possibleadd unsigned addu $1,$2,$3 $1 = $2 + $3 3 operands; no exceptionssubtract unsigned subu $1,$2,$3 $1 = $2 – $3 3 operands; no exceptionsadd imm. unsign. addiu $1,$2,100 $1 = $2 + 100 + constant; no exceptionsmultiply mult $2,$3 Hi, Lo = $2 x $3 64-bit signed productmultiply unsigned multu$2,$3 Hi, Lo = $2 x $3 64-bit unsigned productdivide div $2,$3 Lo = $2 ÷ $3, Lo = quotient, Hi = remainder

Hi = $2 mod $3 divide unsigned divu $2,$3 Lo = $2 ÷ $3, Unsigned quotient &

remainder Hi = $2 mod $3

Move from Hi mfhi $1 $1 = Hi Used to get copy of HiMove from Lo mflo $1 $1 = Lo Used to get copy of Lo

Page 3: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.3 Spring 2005

ALU VHDL Representation

entity ALU is port(A, B: in std_logic_vector (31 downto 0);

m: in std_logic_vector (3 downto 0); result: out std_logic_vector (31 downto 0); zero: out std_logic; ovf: out std_logic)

end ALU;

architecture process_behavior of ALU is. . .begin

ALU: processbegin

. . . result := A + B; . . .end process ALU;

end process_behavior;

Page 4: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.4 Spring 2005

Number Representation Bits are just bits (have no inherent meaning)

conventions define the relationships between bits and numbers

Binary numbers (base 2) - integers0000 0001 0010 0011 0100 0101 0110 0111

1000 1001 . . . in decimal from 0 to 2n-1 for n bits

Of course, it gets more complicated storage locations (e.g., register file words) are finite, so

have to worry about overflow (i.e., when the number is too big to fit into 32 bits)

have to be able to represent negative numbers, e.g., how do we specify -8 in

addi $sp, $sp, -8 #$sp = $sp - 8 in real systems have to provide for more that just integers,

e.g., fractions and real numbers (and floating point)

Page 5: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.5 Spring 2005

32-bit signed numbers (2’s complement):

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

What if the bit string represented addresses? need operations that also deal with only positive (unsigned)

integers

maxint

minint

MIPS Representations

Page 6: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.6 Spring 2005

Review: Signed Binary Representation2’s comp decimal

1000 -8

1001 -7

1010 -6

1011 -5

1100 -4

1101 -3

1110 -2

1111 -1

0000 0

0001 1

0010 2

0011 3

0100 4

0101 5

0110 6

0111 723 - 1 =

1011

then add a 1

1010

complement all the bits

-(23 - 1) =

-23 =

Page 7: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.7 Spring 2005

Negating a two's complement number: complement all

the bits and add a 1

remember: “negate” and “invert” are quite different!

Converting n-bit numbers into numbers with more than n

bits:

MIPS 16-bit immediate gets converted to 32 bits for

arithmetic

copy the most significant bit (the sign bit) into the other bits

0010 -> 0000 0010

1010 -> 1111 1010

sign extension versus zero extend (lb vs. lbu)

Two's Complement Operations

Page 8: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.8 Spring 2005

Goal: Design an ALU for the MIPS ISA

Must support the Arithmetic/Logic operations of the ISA

Tradeoffs of cost and speed based on frequency of occurrence, hardware budget

Page 9: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.9 Spring 2005

MIPS Arithmetic and Logic Instructions

Signed arithmetic generates overflow, but no carry out

R-type:

I-Type:

31 25 20 15 5 0

op Rs Rt Rd funct

op Rs Rt Immed 16

Type op funct

ADDI 001000 xx

ADDIU 001001 xx

SLTI 001010 xx

SLTIU 001011 xx

ANDI 001100 xx

ORI 001101 xx

XORI 001110 xx

LUI 001111 xx

Type op funct

ADD 000000 100000

ADDU 000000 100001

SUB 000000 100010

SUBU 000000 100011

AND 000000 100100

OR 000000 100101

XOR 000000 100110

NOR 000000 100111

Type op funct

000000 101000

000000 101001

SLT 000000 101010

SLTU 000000 101011

000000 101100

Page 10: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.11 Spring 2005

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 0111 - 0110 + 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

Addition & Subtraction

1101 0001 0001

0001 1 0001

1000

Page 11: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.12 Spring 2005

Building a 1-bit Binary Adder

1 bit Full Adder

A

BS

carry_in

carry_out

S = A xor B xor carry_in

carry_out = AB v Acarry_in v Bcarry_in (majority function)

How can we use it to build a 32-bit adder?

How can we modify it easily to build an adder/subtractor?

A B carry_in carry_out S

0 0 0 0 0

0 0 1 0 1

0 1 0 0 1

0 1 1 1 0

1 0 0 0 1

1 0 1 1 0

1 1 0 1 0

1 1 1 1 1

Page 12: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.13 Spring 2005

Building 32-bit Adder

1-bit FA

A0

B0

S0

c0=carry_in

c1

1-bit FA

A1

B1

S1

c2

1-bit FA

A2

B2

S2

c3

c32=carry_out

1-bit FA

A31

B31

S31

c31

. .

.

Just connect the carry-out of the least significant bit FA to the carry-in of the next least significant bit and connect . . .

Page 13: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.14 Spring 2005

Building 32-bit Adder/Subtractor

Remember 2’s complement is just

complement all the bits

add a 1 in the least significant bit

A 0111 0111 B - 0110 + 1010

1-bit FA S0

c0=carry_in

c1

1-bit FA S1

c2

1-bit FA S2

c3

c32=carry_out

1-bit FA S31

c31

. .

.

A0

A1

A2

A31

B0

B1

B2

B31

add/subt

B0

control(0=add,1=subt) B0 if control =

0, !B0 if control = 1

Page 14: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.15 Spring 2005

Overflow Detection and Effects

Overflow: the result is too large to represent in the number of bits allocated

When adding operands with different signs, overflow cannot occur! Overflow occurs when

adding two positives yields a negative or, adding two negatives gives a positive or, subtract a negative from a positive gives a negative or, subtract a positive from a negative gives a positive

On overflow, an exception (interrupt) occurs Control jumps to predefined address for exception Interrupted address (address of instruction causing the

overflow) is saved for possible resumption

Don't always want to detect (interrupt on) overflow

Page 15: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.16 Spring 2005

Overflow Detection Overflow: the result is too large to represent in the

number of bits allocated

Overflow occurs when adding two positives yields a negative or, adding two negatives gives a positive or, subtract a negative from a positive gives a negative or, subtract a positive from a negative gives a positive

On your own: Prove you can detect overflow by: Carry into MSB xor Carry out of MSB

1

1

1 10

1

0

1

1

0

0 1 1 1

0 0 1 1+

7

3

0

1

– 6

1 1 0 0

1 0 1 1+

–4

– 5

71

0

Page 16: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.17 Spring 2005

A Simple ALU Cell

1-bit FA

carry_in

carry_out

A

B

add/subt

add/subt

result

op

AND

ORXOR

NORB or!B

Mux

A and BA and !BA or BA or !B

A xor BA xor !B

A nor BA nor !B

A + BA - B

Page 17: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.18 Spring 2005

A Simple 32-bit ALU

+

A1

B1

result1

+

A0

B0

result0

+

A31

B31

result31

. . .

add/sub op

Add/

subt

Op operation

0 000

1 000

0 001

1 001

0 010

1 010

0 011

1 011

0 100

1 100

A and B

A and !B

A or B

A or !B

A xor BA xor !B

A nor B

A nor !B

A + BA - B

Page 18: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.19 Spring 2005

Need to support the set-on-less-than instruction (slt)

remember: slt is an arithmetic instruction

produces a 1 if rs < rt and 0 otherwise

use subtraction: (a - b) < 0 implies a < b

Need to support test for equality (beq)

use subtraction: (a - b) = 0 implies a = b

Need to add the overflow detection hardware

Tailoring the ALU to the MIPS ISA

Page 19: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.20 Spring 2005

Modifying the ALU Cell for slt

1-bit FA

A

B

result

carry_in

carry_out

add/subt op

add/subt

less

Page 20: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.22 Spring 2005

Modifying the ALU for slt

0

0set

First perform a subtraction

Make the result 1 if the subtraction yields a negative result

Make the result 0 if the subtraction yields a positive result

A1

B1

A0

B0

A31

B31

+

result1

less

+

result0

less

+

result31

less

. . .tie the most significant sum bit (sign bit) to the low order less input

Page 21: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.24 Spring 2005

Modifying the ALU for Zero

+

A1

B1

result1

less

+

A0

B0

result0

less

+

A31

B31

result31

less

. . .

0

0

set

First perform subtraction

Insert additional logic to detect when all result bits are zero

zero

. . .

add/subtop

Note zero is a 1 when result is all zeros

Page 22: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.25 Spring 2005

Review: Overflow Detection Overflow: the result is too large to represent in the

number of bits allocated

Overflow occurs when adding two positives yields a negative or, adding two negatives gives a positive or, subtract a negative from a positive gives a negative or, subtract a positive from a negative gives a positive

On your own: Prove you can detect overflow by: Carry into MSB xor Carry out of MSB

1

1

1 10

1

0

1

1

0

0 1 1 1

0 0 1 1+

7

3

0

1

– 6

1 1 0 0

1 0 1 1+

–4

– 5

71

0

Page 23: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.26 Spring 2005

Modifying the ALU for Overflow

+

A1

B1

result1

less

+

A0

B0

result0

less

+

A31

B31

result31

less

. . .

0

0

set

Modify the most significant cell to determine overflow output setting

Disable overflow bit setting for unsigned arithmetic

zero

. . .

add/subtop

overflow

Page 24: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.27 Spring 2005

MULTIPLY (unsigned)

Paper and pencil example (unsigned):

Multiplicand 1000 Multiplier 1001

1000 0000 0000

1000 Product 01001000

m bits x n bits = m+n bit product

Binary makes it easy:0 => place 0 ( 0 x multiplicand)1 => place a copy ( 1 x multiplicand)

4 versions of multiply hardware & algorithm: successive refinement

Page 25: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.28 Spring 2005

Unsigned Combinational Multiplier

Stage i accumulates A * 2 i if Bi == 1

Q: How much hardware for 32 bit multiplier? Critical path?

B0

A0A1A2A3

A0A1A2A3

A0A1A2A3

A0A1A2A3

B1

B2

B3

P0P1P2P3P4P5P6P7

0 0 0 0

Page 26: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.29 Spring 2005

How does it work?

at each stage shift A left ( x 2)

use next bit of B to determine whether to add in shifted multiplicand

accumulate 2n bit partial product at each stage

B0

A0A1A2A3

A0A1A2A3

A0A1A2A3

A0A1A2A3

B1

B2

B3

P0P1P2P3P4P5P6P7

0 0 0 00 0 0

Page 27: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.30 Spring 2005

Unisigned shift-add multiplier (version 1)

64-bit Multiplicand reg, 64-bit ALU, 64-bit Product reg, 32-bit multiplier reg

Product

Multiplier

Multiplicand

64-bit ALU

Shift Left

Shift Right

WriteControl

32 bits

64 bits

64 bits

Multiplier = datapath + control

Page 28: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.31 Spring 2005

Multiply Algorithm Version 1

Product Multiplier Multiplicand 0000 0000 0011 0000 0010

0000 0010 0001 0000 0100

0000 0110 0000 0000 1000

0000 0110

3. Shift the Multiplier register right 1 bit.

DoneYes: 32 repetitions

2. Shift the Multiplicand register left 1 bit.

No: < 32 repetitions

1. TestMultiplier0

Multiplier0 = 0Multiplier0 = 1

1a. Add multiplicand to product & place the result in Product register

32nd repetition?

Start

Page 29: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.32 Spring 2005

Observations on Multiply Version 1

1 clock per cycle => 100 clocks per multiply Ratio of multiply to add 5:1 to 100:1

1/2 bits in multiplicand always 0=> 64-bit adder is wasted

0’s inserted in left of multiplicand as shifted=> least significant bits of product never changed once formed

Instead of shifting multiplicand to left, shift product to right?

Page 30: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.33 Spring 2005

MULTIPLY HARDWARE Version 2

32-bit Multiplicand reg, 32 -bit ALU, 64-bit Product reg, 32-bit Multiplier reg

Product

Multiplier

Multiplicand

32-bit ALU

Shift Right

WriteControl

32 bits

32 bits

64 bits

Shift Right

Page 31: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.34 Spring 2005

Multiply Algorithm Version 2Multiplier Multiplicand Product

0011 0010 0000 0000

3. Shift the Multiplier register right 1 bit.

DoneYes: 32 repetitions

2. Shift the Product register right 1 bit.

No: < 32 repetitions

1. TestMultiplier0

Multiplier0 = 0Multiplier0 = 1

1a. Add multiplicand to the left half of product & place the result in the left half of Product register

32nd repetition?

Start

° Product Multiplier Multiplicand 0000 0000 0011 0010

Page 32: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.35 Spring 2005

What’s going on?

Multiplicand stay’s still and product moves right

B0

B1

B2

B3

P0P1P2P3P4P5P6P7

0 0 0 0

A0A1A2A3

A0A1A2A3

A0A1A2A3

A0A1A2A3

Page 33: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.37 Spring 2005

Observations on Multiply Version 2

Product register wastes space that exactly matches size of multiplier=> combine Multiplier register and Product register

Page 34: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.38 Spring 2005

MULTIPLY HARDWARE Version 3

32-bit Multiplicand reg, 32 -bit ALU, 64-bit Product reg, (0-bit Multiplier reg)

Product (Multiplier)

Multiplicand

32-bit ALU

WriteControl

32 bits

64 bits

Shift Right

Page 35: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.39 Spring 2005

Multiply Algorithm Version 3Multiplicand Product

0010 0000 0011

DoneYes: 32 repetitions

2. Shift the Product register right 1 bit.

No: < 32 repetitions

1. TestProduct0

Product0 = 0Product0 = 1

1a. Add multiplicand to the left half of product & place the result in the left half of Product register

32nd repetition?

Start

Page 36: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.40 Spring 2005

Observations on Multiply Version 3

2 steps per bit because Multiplier & Product combined

MIPS registers Hi and Lo are left and right half of Product

Gives us MIPS instruction MultU

How can you make it faster?

What about signed multiplication? easiest solution is to make both positive & remember whether to

complement product when done (leave out the sign bit, run for 31 steps)

apply definition of 2’s complement - need to sign-extend partial products and subtract at the end

Booth’s Algorithm is elegant way to multiply signed numbers using same hardware as before and save cycles

- can handle multiple bits at a time

Page 37: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.41 Spring 2005

Motivation for Booth’s Algorithm Example 2 x 6 = 0010 x 0110:

0010 x 0110 + 0000 shift (0 in multiplier) + 0010 add (1 in multiplier) + 0100 add (1 in multiplier) + 0000 shift (0 in multiplier) 00001100

ALU with add or subtract gets same result in more than one way:6 = – 2 + 8 0110 = – 00010 + 01000 = 11110 + 01000

For example

0010 x 0110 0000 shift (0 in

multiplier) – 0010 sub (first 1 in multpl.) . 0000 shift (mid string of 1s) . + 0010 add (prior step had last 1) 00001100

Page 38: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.42 Spring 2005

Booth’s Algorithm

Current Bit Bit to the Right Explanation Example Op

1 0 Begins run of 1s 0001111000 sub

1 1 Middle of run of 1s 0001111000none

0 1 End of run of 1s 0001111000 add

0 0 Middle of run of 0s 0001111000none

Originally for Speed (when shift was faster than add)

Replace a string of 1s in multiplier with an initial subtract when we first see a one and then later add for the bit after the last one

0 1 1 1 1 0beginning of runend of run

middle of run

–1+ 10000

01111

Page 39: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.43 Spring 2005

Booths Example (2 x 7)

1a. P = P - m 1110 + 11101110 0111 0 shift P (sign

ext)

1b. 0010 1111 0011 1 11 -> nop, shift

2. 0010 1111 1001 1 11 -> nop, shift

3. 0010 1111 1100 1 01 -> add

4a. 0010 + 0010 0001 1100 1 shift

4b. 0010 0000 1110 0 done

Operation Multiplicand Product next?

0. initial value 0010 0000 0111 0 10 -> sub

Page 40: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.44 Spring 2005

Booths Example (2 x -3)

1a. P = P - m 1110 + 11101110 1101 0 shift P (sign

ext)

1b. 0010 1111 0110 1 01 -> add + 0010

2a. 0001 0110 1 shift P

2b. 0010 0000 1011 0 10 -> sub + 1110

3a. 0010 1110 1011 0 shift

3b. 0010 1111 0101 1 11 -> nop4a 1111 0101 1 shift

4b. 0010 1111 1010 1 done

Operation Multiplicand Product next?

0. initial value 0010 0000 1101 0 10 -> sub

Page 41: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.45 Spring 2005

MIPS logical instructionsInstruction Example Meaning Comment

and and $1,$2,$3 $1 = $2 & $3 3 reg. operands; Logical ANDor or $1,$2,$3 $1 = $2 | $3 3 reg. operands; Logical ORxor xor $1,$2,$3 $1 = $2 $3 3 reg. operands; Logical XORnor nor $1,$2,$3 $1 = ~($2 |$3) 3 reg. operands; Logical NORand immediate andi $1,$2,10 $1 = $2 & 10 Logical AND reg, constantor immediate ori $1,$2,10 $1 = $2 | 10 Logical OR reg, constantxor immediate xori $1, $2,10 $1 = ~$2 &~10 Logical XOR reg, constantshift left logical sll $1,$2,10 $1 = $2 << 10 Shift left by constantshift right logical srl $1,$2,10 $1 = $2 >> 10 Shift right by constantshift right arithm. sra $1,$2,10 $1 = $2 >> 10 Shift right (sign extend) shift left logical sllv $1,$2,$3 $1 = $2 << $3 Shift left by variableshift right logical srlv $1,$2, $3 $1 = $2 >> $3 Shift right by variableshift right arithm. srav $1,$2, $3 $1 = $2 >> $3 Shift right arith. by variable

Page 42: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.46 Spring 2005

Shifters

Two kinds: logical-- value shifted in is always "0"

arithmetic-- on right shifts, sign extend

msb lsb"0" "0"

msb lsb "0"

Note: these are single bit shifts. A given instruction might request 0 to 32 bits to be shifted!

Page 43: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.47 Spring 2005

Combinational Shifter from MUXes

What comes in the MSBs?

How many levels for 32-bit shifter?

What if we use 4-1 Muxes ?

1 0sel

A B

D

Basic Building Block

8-bit right shifter

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

S2 S1 S0A0A1A2A3A4A5A6A7

R0R1R2R3R4R5R6R7

Page 44: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.48 Spring 2005

General Shift Right Scheme using 16 bit example

S 0 (0,1)

S 1(0, 2)

If added Right-to-left connections couldsupport Rotate (not in MIPS but found in ISAs)

S 3(0, 8)

S 2(0, 4)

Page 45: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.49 Spring 2005

Funnel Shifter

XY

R Shift A by i bits (sa= shift right amount)

Logical: Y = 0, X=A, sa=i

Arithmetic? Y = _, X=_, sa=_

Rotate? Y = _, X=_, sa=_

Left shifts? Y = _, X=_, sa=_

Instead Extract 32 bits of 64.

Shift Right

Shift Right

32 32

32

Y X

R

Page 46: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.50 Spring 2005

Barrel ShifterTechnology-dependent solutions: transistor per switch

D3

D2

D1

D0

A6

A5

A4

A3 A2 A1 A0

SR0SR1SR2SR3

Page 47: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.51 Spring 2005

Divide: Paper & Pencil

1001 Quotient

Divisor 1000 1001010 Dividend–1000 10 101 1010 –1000 10 Remainder (or Modulo result)

See how big a number can be subtracted, creating quotient bit on each step

Binary => 1 * divisor or 0 * divisor

Dividend = Quotient x Divisor + Remainder=> | Dividend | = | Quotient | + | Divisor |

3 versions of divide, successive refinement

Page 48: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.52 Spring 2005

DIVIDE HARDWARE Version 1

64-bit Divisor reg, 64-bit ALU, 64-bit Remainder reg, 32-bit Quotient reg

Remainder

Quotient

Divisor

64-bit ALU

Shift Right

Shift Left

WriteControl

32 bits

64 bits

64 bits

Page 49: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.53 Spring 2005

2b. Restore the original value by adding the Divisor register to the Remainder register, &place the sum in the Remainder register. Alsoshift the Quotient register to the left, setting the new least significant bit to 0.

Divide Algorithm Version 1Takes n+1 steps for n-bit Quotient & Rem.

Remainder Quotient Divisor0000 0111 0000 0010 0000

Test Remainder

Remainder < 0Remainder 0

1. Subtract the Divisor register from the Remainder register, and place the result in the Remainder register.

2a. Shift the Quotient register to the left setting the new rightmost bit to 1.

3. Shift the Divisor register right1 bit.

Done

Yes: n+1 repetitions (n = 4 here)

Start: Place Dividend in Remainder

n+1repetition?

No: < n+1 repetitions

Page 50: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.54 Spring 2005

Observations on Divide Version 1

1/2 bits in divisor always 0=> 1/2 of 64-bit adder is wasted => 1/2 of divisor is wasted

Instead of shifting divisor to right, shift remainder to left?

1st step cannot produce a 1 in quotient bit (otherwise too big) => switch order to shift first and then subtract, can save 1 iteration

Page 51: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.55 Spring 2005

DIVIDE HARDWARE Version 232-bit Divisor reg, 32-bit ALU, 64-bit Remainder reg, 32-bit Quotient reg

Remainder

Quotient

Divisor

32-bit ALU

Shift Left

WriteControl

32 bits

32 bits

64 bits

Shift Left

Page 52: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.56 Spring 2005

Divide Algorithm Version 2

Remainder Quotient Divisor0000 0111 0000 0010

3b. Restore the original value by adding the Divisor register to the left half of the Remainderregister, &place the sum in the left half of the Remainder register. Also shift the Quotient register to the left, setting the new least significant bit to 0.

Test Remainder

Remainder < 0Remainder 0

2. Subtract the Divisor register from the left half of the Remainder register, & place the result in the left half of the Remainder register.

3a. Shift the Quotient register to the left setting the new rightmost bit to 1.

1. Shift the Remainder register left 1 bit.

Done

Yes: n repetitions (n = 4 here)

nthrepetition?

No: < n repetitions

Start: Place Dividend in Remainder

Page 53: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.57 Spring 2005

Observations on Divide Version 2

Eliminate Quotient register by combining with Remainder as shifted left

Start by shifting the Remainder left as before. Thereafter loop contains only two steps because the shifting

of the Remainder register shifts both the remainder in the left half and the quotient in the right half

The consequence of combining the two registers together and the new order of the operations in the loop is that the remainder will shifted left one time too many.

Thus the final correction step must shift back only the remainder in the left half of the register

Page 54: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.58 Spring 2005

DIVIDE HARDWARE Version 3

32-bit Divisor reg, 32 -bit ALU, 64-bit Remainder reg, (0-bit Quotient reg)

Remainder (Quotient)

Divisor

32-bit ALU

WriteControl

32 bits

64 bits

Shift Left“HI” “LO”

Page 55: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.59 Spring 2005

Divide Algorithm Version 3Remainder Divisor

0000 0111 0010

3b. Restore the original value by adding the Divisor register to the left half of the Remainderregister, &place the sum in the left half of the Remainder register. Also shift the Remainder register to the left, setting the new least significant bit to 0.

Test Remainder

Remainder < 0Remainder 0

2. Subtract the Divisor register from the left half of the Remainder register, & place the result in the left half of the Remainder register.

3a. Shift the Remainder register to the left setting the new rightmost bit to 1.

1. Shift the Remainder register left 1 bit.

Done. Shift left half of Remainder right 1 bit.

Yes: n repetitions (n = 4 here)

nthrepetition?

No: < n repetitions

Start: Place Dividend in Remainder

Page 56: Week 7.1Spring 2005 14:332:331 Computer Architecture and Assembly Language Spring 2005 Week 7 [Adapted from Dave Patterson’s UCB CS152 slides and Mary

Week 7.60 Spring 2005

Observations on Divide Version 3

Same Hardware as Multiply: just need ALU to add or subtract, and 63-bit register to shift left or shift right

Hi and Lo registers in MIPS combine to act as 64-bit register for multiply and divide

Signed Divides: Simplest is to remember signs, make positive, and complement quotient and remainder if necessary

Note: Dividend and Remainder must have same sign

Note: Quotient negated if Divisor sign & Dividend sign disagreee.g., –7 ÷ 2 = –3, remainder = –1

Possible for quotient to be too large: if divide 64-bit interger by 1, quotient is 64 bits (“called saturation”)