chapter 5 arithmetic and logical operations. x 0011 +y+0001 sum 0100 or in tabular form… carry in...

24
Chapter 5 Arithmetic and Logical Operations

Post on 20-Dec-2015

233 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Chapter 5

Arithmetic and Logical Operations

Page 2: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

x 0011+y +0001 sum 0100

Or in tabular form…

Carry in a b sum carry out 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

Addition

Page 3: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Addition

a b

sumco ci

And as a full adder…

4-bit Ripple-Carry adder•Carry values propagate from bit to bit•Like pencil-and-paper addition•Time proportional to number of bits•“Lookahead-carry” can propagatecarry proportional to log(n) with morespace.

a b

sumco ci

a b

sumco ci

a b

sumco ci

a b

sumco ci

Page 4: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Addition: unsigned

Just like the simple addition given earlier:

Examples:

100001 (33) 00001010 (10) + 011101 (29) + 00001110 (14)

111110 (62) 00011000 (24)

(ignoring overflow)

Page 5: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Addition: 2’s complement

•Non-negative (0 and positives)•Just like unsigned addition

•Assume 6-bit and observe:

000011 (3) 101000 (-24) 111111 (-1)+111100 (-4) + 010000 (16) + 001000 (8) 111111 (-1) 111000 (-8) 1000111 (7)

•Ignore carry-outs (and overflow)•Sign bits is the 2n-1’s•A carry into the 2n-1’s place is a 2n-1 positive weight•What does this mean for adding different signs?

Page 6: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Addition: 2’s complement

•-20 + 15

•5 + 12

•-12 + -20

Page 7: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Addition: sign magnitude

•Add magnitudes only•Do not carry into the sign bit•Throw away any carry out of the MSB of the magnitude(overflow)•Add only integers of like sign (+ to + OR – to – )•Sign of the result is same as sign of the addends•Examples:

0 0101 (5) 1 1010 (-10)+ 0 0011 (3) + 1 0011 (-3)

0 1000 (8) 1 1101 (-13)

0 01011 (11)+ 1 01110 (-14)Don’t add!! Must subtract!!

Page 8: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Subtraction

•General rules:1 – 1 = 00 – 0 = 01 – 0 = 1

10 – 1 = 10 – 1 = borrow!

•Or replace (x – y) with x + (-y)•Can replace subtraction with additive inverse and addition

Page 9: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Subtraction: 2’s complement

•Use addition:

x – y x + (-y)

•Examples:

10110 (-10) - 00011 (3)

10110 (-10) + 11101 (-3)

1 11011 (-13)

Page 10: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Subtraction: 2’s complement

• Can also flip bits of y and add an LSB carry in: 1 10110 (-10)

+ 11100 (-3) 1 10011 (-13)(throw away carry out)

• Addition and subtraction are simple in 2’s complement, just need an adder and inverters.

Page 11: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Subtraction: unsigned

•For n-bits use the 2’s complement method and overflowif negative

11100 (+28) - 10110 (+22)

•Becomes

Page 12: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Subtraction: sign magnitude

•If the signs are the same, then do subtraction•If signs are different, then change the problem to addition•Compare magnitudes, then subtract smaller from larger•If the order is switched, then switch the sign also•When the integers are of the opposite sign,

•(+x) – (+y) x + (-y)• x + y x – (-y)

•Switch sign since the order of the subtraction wasreversed

000111 (7) 1 11000 (-24) - 011000 (24) - 1 00010 (-2)

do 1 10110 (-22) 011000 (24) - 000111 (7) 110001 (-17)

Page 13: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Overflow in Addition

Unsigned When there is a carry out of the MSB

Signed magnitude When there is a carry out of theMSB of the magnitude

2’s complement When the signs of the addends are thesame, and the sign of the result is different

1000 (8) + 1001 (9) 1 0001 (1)

1 1000 (-8) + 1 1001 (-9) 1 10001 (-1) (carry out of MSB of magnitude)

0011 (3) + 0110 (6) 1001 (-7)

•Adding 2 numbers of opposite signs never overflows

Page 14: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Overflow in Subtraction

Unsigned if negative

Signed magnitude never happens when doing subtraction

2’s complement we never do subtraction, so use the addition rule on the addition operation done.

Page 15: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Multiplication

•The multiplicand is multiplied by the multiplier toproduce the product, the sum of partial products

•Longhand, it looks just like decimal•Result can require twice as many bits as the largermultiplicand (why?)

multiplicand X multiplier = product

0011 (+3) x 0110 (+6)

0000 0011 0011 0000 0010010 (+18)

Page 16: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Multiplication: For 2’s Complement

•If negative multiplicand, just sign-extend it.•If negative multiplier, take 2SC of both multiplicand andmultiplier (-7 x -3 = 7 x 3, and 7 x –3 = -7 x 3)

0011 (3) x 1011 (-5)

1101 (-3) x 0101 (+5)

111111111010000000000111111101

+ 00000000

11111110001 (-15)

•Or use Booth’s algorithm to multiply signed numbers•Also allows multiplying groups of bits.•You will learn this in CE110.

Page 17: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Division

•Unsigned only!!!•14/3 = …

Page 18: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Logical Operations

•Operate on raw bits with 1 = true and 0 = false

In1 In2 & | ~(&)

~(|) ^ ~(^)

0 0 0 0 1 1 0 1

0 1 0 1 1 0 1 0

1 0 0 1 1 0 1 0

1 1 1 1 0 0 0 1•In computers, done bit-wise: in parallel for correspondingbits

X = 0011Y = 1010

X AND YX OR YX NOR YX XOR Y

Page 19: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

MAL Logical Instructions

Operate bit-wise on 32-bit .words

not x,y nand x,y,zand x,y,z xor x,y,zor x,y,z xnor x,y,znor x,y,z

Example: a: .word 0x00000003 # 0…0 0011b: .word 0x0000000a # 0…0 1010# assume $t0=a and $t1=b

and $t2,$t0,$t1 # $t2=$t0 & $t1or $t3,$t0,$t1 # $t3=$t0 | $t1

Page 20: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Logical Instructions

•Masking refers to using AND operations to isolate bitsin a word•Example:

abcd: .word 0x61626364mask: .word 0x000000ffmask2: .word 0x0000ff00tmp: .word

and tmp, abcd, maskbeq tmp, ‘d’,found_d # ‘d’ == 0x64 in ASCII

•How about adding:and tmp,abcd,mask2beq tmp,’c’,found_c # ‘c’ == 0x63 in ASCII

•Note: These are SAL instructions. MAL are just the same, but operate on registers rather than words.

Page 21: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Shifts and Rotates

•Logical right•Move bits to the right, same order•Throw away the bit that pops off the LSB•Introduce a 0 into the MSB

00110101 00011010 (shift by 1 right)

•Logical left•Move bits to the left, same order•Throw away the bit that pops off the MSB•Introduce a 0 into the LSB

00110101 01101010 (shift by 1 left)

Page 22: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Shifts and Rotates

•Arithmetic right•Move bits to the right, same order•Throw away the bit that pops off the LSB•Reproduce the original MSB into the new MSB•Alternatively, shift the bits, and then do sign extension

00110101 00011010 (right by 1)1100 1110 (right by 1)

•Arithmetic left•Move bits to the left, same order•Throw away the bit that pops off the MSB•Introduce a 0 into the LSB

00110101 01101010 (left by 1)

Page 23: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

Shifts and Rotates

•Rotate left•Move bits to the left, same order•Put the bit that pops off the MSB into the LSB•Not bits are thrown away or lost

00110101 0011010101100 1001

•Rotate right•Move bits to the right, same order•Put the bit that pops off the LSB into the MSB•No bits are thrown away or lost

00110101 100110101100 0110

Page 24: Chapter 5 Arithmetic and Logical Operations. x 0011 +y+0001 sum 0100 Or in tabular form… Carry in a b sum carry out 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1

MAL shift and Rotate Instructions

sll $t0, $t1, value # shift left logicalsrl $t0, $t1, value # shift right logicalsra $t0, $t1, value # shift right arithmeticrol $t0, $t1, value # rotate leftror $t0, $t1, value # rotate right

Example:abcd: .word 0x61626364mask: .word 0x0000ff00

lw $t1, abcdlw $t2, maskand $t3, $t1, $t2srl $t3, $t3, 8li $t4, ‘c’beq $t3, $t4, found_c