Rekenen en rekenschakelingen Ben Bruidegom AMSTEL Instituut FNWI UvA.

Post on 31-Mar-2015

220 views 3 download

Transcript of Rekenen en rekenschakelingen Ben Bruidegom AMSTEL Instituut FNWI UvA.

Rekenen en Rekenen en rekenschakelingenrekenschakelingen

Ben BruidegomBen Bruidegom

AMSTEL Instituut FNWI AMSTEL Instituut FNWI UvAUvA

Rekenen en Rekenen en rekenschakelingenrekenschakelingen

Binaire codeBinaire code Hexadecimale codeHexadecimale code OptellenOptellen Two’s complement codeTwo’s complement code AftrekkenAftrekken Arithmetic Logic UnitArithmetic Logic Unit Sign extensionSign extension Look Ahead Carry GenerationLook Ahead Carry Generation VermenigvuldigenVermenigvuldigen

Decimale codeDecimale code

235 = 2* 10235 = 2* 1022 + 3* 10 + 3* 101 1 + 5 * 10+ 5 * 1000

Grondtal is 10Grondtal is 10

Binaire codeBinaire code 101 = 1* 2101 = 1* 222 + 0* 2 + 0* 21 1 + 1 * 2+ 1 * 200

Grondtal is 2Grondtal is 2

Binary codeBinary code

Binary code Decimal 23 22 21 20

0 0 0 0 0 1 0 0 0 1 2 0 0 1 0 3 0 0 1 1 4 0 1 0 0 9 1 0 0 1

15 1 1 1 1

Binary code (Unsigned Binary code (Unsigned Integer)Integer)

1 of 0met

2222

...222

00

11

22

33

22

11

1

0

i

nn

nn

n

ii

iDecimal

a

aaaa

aaaA

8 bit

0100 0000 = ….

1000 0111 = ….

Range: 0 ..

n = 8 Range 0 …

n = 16 Range 0 …

2 = 32 Range 0 ..

Unsigned IntegerUnsigned Integer

12 n

8 bit

0100 0000 = 64

1000 0111 =

Range: 0 ..

n = 8 Range 0 ..

n = 16 Range 0 ..

n = 32 Range 0 ..

Unsigned IntegerUnsigned Integer

12 n

8 bit

0100 0000 = 64

1000 0111 = 135

Range: 0 ..

n = 8 Range 0 ..

n = 16 Range 0 ..

n = 32 Range 0 ..

Unsigned IntegerUnsigned Integer

12 n

8 bit

0100 0000 = 64

1000 0111 = 135

Range: 0 ..

n = 8 Range 0 .. 255 (256 –1)

n = 16 Range 0 ..

n = 32 Range 0 ..

Unsigned IntegerUnsigned Integer

12 n

1248163264128255

8 bit

0100 0000 = 64

1000 0111 = 135

Range: 0 ..

n = 8 Range 0 .. 255

n = 16 Range 0 .. 65.536

n = 32 Range 0 ..

Unsigned IntegerUnsigned Integer

12 n

8 bit

0100 0000 = 64

1000 0111 = 135

Range: 0 ..

n = 8 Range 0 .. 255

n = 16 Range 0 .. 65.535

n = 32 Range 0 .. 4.294.967.295

Unsigned IntegerUnsigned Integer

12 n

Hexadecimale codeHexadecimale code

Verkorte schrijfwijze binaire codeVerkorte schrijfwijze binaire code Grondtal 16Grondtal 16

Hex. Binary Decimal 7 0111 7 9 1001 9 A 1010 10 B 1011 11 C 1100 12 D 1101 13 E 1110 14 F 1111 15 10 0001 0000 16 11 .… …. .. .. .… …. 30 30 .… …. .. FE .… …. ..

Hex. Binary Decimal 7 0111 7 9 1001 9 A 1010 10 B 1011 11 C 1100 12 D 1101 13 E 1110 14 F 1111 15 10 0001 0000 16 11 0001 0001 17 .. .… …. 30 30 .… …. .. FE .… …. ..

Hex. Binary Decimal 7 0111 7 9 1001 9 A 1010 10 B 1011 11 C 1100 12 D 1101 13 E 1110 14 F 1111 15 10 0001 0000 16 11 0001 0001 17 1E 0001 1110 30 30 …. …. .. FE …. …. ..

Hex. Binary Decimal 7 0111 7 9 1001 9 A 1010 10 B 1011 11 C 1100 12 D 1101 13 E 1110 14 F 1111 15 10 0001 0000 16 11 0001 0001 17 1E 0001 1110 30 30 0011 0000 48 FE …. …. ..

Hex. Binary Decimal 7 0111 7 9 1001 9 A 1010 10 B 1011 11 C 1100 12 D 1101 13 E 1110 14 F 1111 15 10 0001 0000 16 11 0001 0001 17 1E 0001 1110 30 30 0011 0000 48 FE 1111 1110 254

OptellenOptellen

GetalGetal DecimaDecimaalal

BinairBinair

AA 2525 1 1 0 0 11 1 0 0 1

BB 2929 1 1 1 0 11 1 1 0 1

A + BA + B 5454 1 1 0 1 1 01 1 0 1 1 0

OptellenOptellen

GetalGetal DecimaalDecimaal BinairBinair

transportransportt

1 1 11001 11001

AA 2525 1100111001

BB 2929 1110111101

A + BA + B 5454 1101101101101 onthouden

OptelschakelingOptelschakeling

GetalGetal DecimaalDecimaal BinairBinair

transportransportt

1 1 111100001 1

AA 2525 11110000

11BB 2929

1111110011

A + BA + B 5454 110110111100

Half adder

Full adder

Sum bit

Carry bit

PauzePauze

PIDAC-practicum opgaven 2.3.1 PIDAC-practicum opgaven 2.3.1 t/m 2.3.6t/m 2.3.6

Negatieve getallenNegatieve getallenOne’s complement codeOne’s complement code

11

00

00

00

....

....

....

....

11

11

00

00

11

11

00

11

11

11

11

00

11

11

11

11

00

00

00

00

00

00

00

11

00

00

11

00

00

00

11

11

....

....

....

....

00

11

11

11

--77

.... --33

--22

--11

--00

00 11 22 33 .... 77

Negatieve getallenNegatieve getallenTwo’s complement codeTwo’s complement code

11

00

00

00

....

....

....

....

11

11

00

00

11

11

00

11

11

11

11

00

11

11

11

11

00

00

00

00

00

00

00

11

00

00

11

00

00

00

11

11

....

....

....

....

00

11

11

11

--88

.... --44

--33

--22

--11

00 11 22 33 .... 77

Negatieve getallenNegatieve getallenHoe genereer ik een negatief Hoe genereer ik een negatief getal?getal?

Negatieve getallenNegatieve getallenHoe genereer ik een negatief Hoe genereer ik een negatief getal?getal?

getalgetal 0001 10010001 1001 2525

complementcomplement 1110 01101110 0110

11

Two’s Two’s complementcomplement

1110 01111110 0111 -25-25

Controle: -128 + 64 + 32 + 4 + 2 + 1 = -25

Negatieve getallenNegatieve getallen

2

0

11

00

11

22

22

11

22

222...22n

ii

inn

nn

nn

aaA

aaaaaA

8 bit

0100 0000 = 64

1000 0111 = -121

Range: 0 ..

n = 8 Range –128 .. 127

n = 16 Range -32768 .. 32767

n = 32 Range –2147483648 .. 2147483647

Signed IntegerSigned Integer

12 n

Overflow in 4 bit systeemOverflow in 4 bit systeem

10111011

10101010-5-5

-6-6

1101001011

++55

antwoord 1001 + overflow

antwoord –7 + 16 = +9

00100111

01101100

33

66

10010011

-7-7

+ +

Negeren

4 bit systeem

antwoord 0101 + overflow

antwoord 5 - 16 = -11

Two’s complement codeTwo’s complement code

AftrekkenAftrekken

1)( BABABA

Aftrekken : -3 – (+6) = -3 Aftrekken : -3 – (+6) = -3 +(-6)+(-6)

11011011

01101100

-3-3

66 - +

Negeren

4 bit systeem

antwoord 0111 + overflow

antwoord 7 - 16 = -9

11011101

10101010-3-3

-6-6

1101101111

77

0110

bits inverteren 1001

1 bij optellen 1

1010

Arithmetic-Logic UnitArithmetic-Logic Unit

Carry ini

Carry outi

4 bits ALU4 bits ALU

Overflow condities:Overflow condities:

Boek bladz. Boek bladz. 172172

8 bit 8 bit systeemsysteem

Overflow = Overflow =

........ 7777 outout cbacba

Bitwise operatorenBitwise operatoren

1010110010101100

0011101000111010

0010100000101000

Bitwise AND-operator (&)

Bitwise operatoren: bittestBitwise operatoren: bittest

1010110010101100

0000100000001000

0000100000001000

Bitwise AND-operator (&)

1010010010100100

0000100000001000

0000000000000000

Bitwise operatoren: bit-Bitwise operatoren: bit-resetreset

1010110010101100

1111011111110111

1010010010100100

Bitwise AND-operator (&)

OpgavenOpgaven

Opgaven: 2.4.1 tot en met 2.7.2Opgaven: 2.4.1 tot en met 2.7.2

Vervolg college: 13.30 uurVervolg college: 13.30 uur

De hierna behandelde sheets De hierna behandelde sheets behoren niet tot de stof die voor behoren niet tot de stof die voor AI-studenten is bestemd.AI-studenten is bestemd.

Arithmetic Logic UnitArithmetic Logic Unit

32

32

32

operation

result

a

b

ALU

Bits are just bits (no inherent meaning)Bits are just bits (no inherent meaning)— conventions define relationship between bits — conventions define relationship between bits

and numbersand numbers Binary numbers (base 2)Binary numbers (base 2)

0000 0001 0010 0011 0100 0101 0110 0111 1000 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001...1001...

decimal: 0...2decimal: 0...2nn-1-1 Of course it gets more complicated:Of course it gets more complicated:

numbers are finite (overflow)numbers are finite (overflow)fractions and real numbersfractions and real numbersnegative numbersnegative numbersHow do we represent negative numbers?How do we represent negative numbers?i.e., which bit patterns will represent which i.e., which bit patterns will represent which

numbers?numbers?

NumbersNumbers

32 bit signed numbers:32 bit signed numbers:

0000 0000 0000 0000 0000 0000 0000 00000000 0000 0000 0000 0000 0000 0000 0000twotwo = 0 = 0tenten

0000 0000 0000 0000 0000 0000 0000 00010000 0000 0000 0000 0000 0000 0000 0001twotwo = + 1 = + 1tenten

0000 0000 0000 0000 0000 0000 0000 00100000 0000 0000 0000 0000 0000 0000 0010twotwo = + 2 = + 2tenten

......0111 1111 1111 1111 1111 1111 1111 11100111 1111 1111 1111 1111 1111 1111 1110twotwo = + 2,147,483,646 = + 2,147,483,646tenten

0111 1111 1111 1111 1111 1111 1111 11110111 1111 1111 1111 1111 1111 1111 1111twotwo = + 2,147,483,647 = + 2,147,483,647tenten

1000 0000 0000 0000 0000 0000 0000 00001000 0000 0000 0000 0000 0000 0000 0000twotwo = – 2,147,483,648 = – 2,147,483,648tenten

1000 0000 0000 0000 0000 0000 0000 00011000 0000 0000 0000 0000 0000 0000 0001twotwo = – 2,147,483,647 = – 2,147,483,647tenten

1000 0000 0000 0000 0000 0000 0000 00101000 0000 0000 0000 0000 0000 0000 0010twotwo = – 2,147,483,646 = – 2,147,483,646tenten

......1111 1111 1111 1111 1111 1111 1111 11011111 1111 1111 1111 1111 1111 1111 1101twotwo = – 3 = – 3tenten

1111 1111 1111 1111 1111 1111 1111 11101111 1111 1111 1111 1111 1111 1111 1110twotwo = – 2 = – 2tenten

1111 1111 1111 1111 1111 1111 1111 11111111 1111 1111 1111 1111 1111 1111 1111twotwo = – 1 = – 1tenten

maxint

minint

MIPSMIPS

Converting n bit numbers into numbers Converting n bit numbers into numbers

with more than n bits:with more than n bits:

• MIPS 16 bit immediate gets converted to 32 MIPS 16 bit immediate gets converted to 32

bits for arithmeticbits for arithmetic

• copy the most significant bit (the sign bit) into copy the most significant bit (the sign bit) into

the other bitsthe other bits

0011 0010 -> 0000 0000 0011 00100011 0010 -> 0000 0000 0011 0010

1001 1010 -> 1111 1111 1001 10101001 1010 -> 1111 1111 1001 1010

"sign extension""sign extension"

ConclusionConclusion

We can build an ALU to support the MIPS instruction setWe can build an ALU to support the MIPS instruction set• key idea: use multiplexor to select the output we want key idea: use multiplexor to select the output we want (fig. 2.7.1)(fig. 2.7.1)

• we can efficiently perform subtraction using two’s complementwe can efficiently perform subtraction using two’s complement

• we can replicate a 1-bit ALU to produce a 32-bit ALUwe can replicate a 1-bit ALU to produce a 32-bit ALU

Important points about hardwareImportant points about hardware• all of the gates are always workingall of the gates are always working

• the speed of a gate is affected by the number of inputs to the gatethe speed of a gate is affected by the number of inputs to the gate??????

• the speed of a circuit is affected by the number of gates in seriesthe speed of a circuit is affected by the number of gates in series(on the “critical path” or the “deepest level of logic”)(on the “critical path” or the “deepest level of logic”)

Our primary focus: comprehension, however,Our primary focus: comprehension, however,• Clever changes to organization can improve performanceClever changes to organization can improve performance

(similar to using better algorithms in software)(similar to using better algorithms in software)• we’ll look at two examples for addition and multiplicationwe’ll look at two examples for addition and multiplication

Is there more than one way to do addition?Is there more than one way to do addition? Is a 32-bit ALU as fast as a 1-bit ALU?Is a 32-bit ALU as fast as a 1-bit ALU?

• two extremes: ripple carry and sum-of-productstwo extremes: ripple carry and sum-of-products

cc11 = b = b00cc00 + a + a00cc0 0 ++ aa00bb00

cc22 = b = b11cc11 + a + a11cc1 1 ++ aa11bb11 = =

= b= b11(b(b00cc00 + a + a00cc0 0 ++ aa00bb00)+ a)+ a11(b(b00cc00 + a + a00cc0 0 ++ aa00bb00)+ )+ aa11bb11

cc33 = b = b22cc22 + a + a22cc2 2 ++ aa22bb2 2 cc33 = =

cc44 = b = b33cc33 + a + a33cc3 3 ++ aa33bb3 3 cc44 = =

Not feasible! Why?Not feasible! Why?

Problem: ripple carry adder Problem: ripple carry adder is slowis slow

Can you see the ripple? Can you see the ripple? How could you get rid of it?How could you get rid of it?

Result31a31

b31

Result0

CarryIn

a0

b0

Result1a1

b1

Result2a2

b2

Operation

ALU0

CarryIn

CarryOut

ALU1

CarryIn

CarryOut

ALU2

CarryIn

CarryOut

ALU31

CarryIn

An approach in-between our two extremesAn approach in-between our two extremes Motivation: Motivation:

• If we didn't know the value of carry-in, what could we do?If we didn't know the value of carry-in, what could we do?• When would we always generate a carry? When would we always generate a carry? ggii = a = ai .i .bbi i

• When would we propagate the carry? When would we propagate the carry? ppii = a = ai i + b+ bii

Did we get rid of the ripple?Did we get rid of the ripple?

cc11 = a = a00.b.b00 + b + b00cc00 + a + a00cc0 0 = a= a00.b.b00 + c + c00(b(b00+a+a00))

cc11 = g = g00 + p + p00cc0 0

cc22 = g = g11 + p + p11cc1 1 cc22 = g = g11 + p + p11(g(g00 + p + p00cc00) )

cc33 = g = g22 + p + p22cc2 2 cc33 = =

cc44 = g = g33 + p + p33cc3 3 cc44 = =

Feasible! Why?Feasible! Why?

Carry-lookahead adderCarry-lookahead adder

Can’t build a 16 bit adder this way... (too Can’t build a 16 bit adder this way... (too big)big)

Could use ripple carry of 4-bit CLA addersCould use ripple carry of 4-bit CLA adders Better: use the CLA principle again! Better: use the CLA principle again!

Use principle to Use principle to build bigger build bigger addersadders

CarryIn

Result0--3

ALU0

CarryIn

Result4--7

ALU1

CarryIn

Result8--11

ALU2

CarryIn

CarryOut

Result12--15

ALU3

CarryIn

C1

C2

C3

C4

P0G0

P1G1

P2G2

P3G3

pigi

pi + 1gi + 1

ci + 1

ci + 2

ci + 3

ci + 4

pi + 2gi + 2

pi + 3gi + 3

a0b0a1b1a2b2a3b3

a4b4a5b5a6b6a7b7

a8b8a9b9

a10b10a11b11

a12b12a13b13a14b14a15b15

Carry-lookahead unit

More complicated than additionMore complicated than addition• accomplished via shifting and additionaccomplished via shifting and addition

More time and more areaMore time and more area Let's look at 3 versions based on gradeschool Let's look at 3 versions based on gradeschool

algorithmalgorithm

0010 0010 (multiplicand) (multiplicand)

____xx__10111011 (multiplier) (multiplier)

Negative numbers: convert and multiplyNegative numbers: convert and multiply• there are better techniques, we won’t look at themthere are better techniques, we won’t look at them

MultiplicationMultiplication

MultiplicationMultiplication

ControltestWrite

32 bits

64 bits

Shift rightProduct

Multiplicand

32-bit ALU

Done

1. TestProduct0

1a. Add multiplicand to the left half ofthe product and place the result inthe left half of the Product register

2. Shift the Product register right 1 bit

32nd repetition?

Start

Product0 = 0Product0 = 1

No: < 32 repetitions

Yes: 32 repetitions

Multiplier