chapter 5 arithmetic and logical operations · pdf file11001 11111. cmpe12c 3 cyrus bazeghi...

28
Arithmetic and Logical Operations (Ch 2 & 3)

Upload: votram

Post on 15-Feb-2018

225 views

Category:

Documents


3 download

TRANSCRIPT

Arithmetic and Logical Operations

(Ch 2 & 3)

2CMPE12c Cyrus Bazeghi

Binary Addition

Or in tabular form…x 0011

+y +0001sum 0100

Carry In A B Sum Carry Out0 0 0 0 0

0 0 1 1 00 1 0 1 00 1 1 0 1

1 0 0 1 01 0 1 0 11 1 0 0 1

1 1 1 1 1

3CMPE12c Cyrus Bazeghi

Binary Addition

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 propagate

carry proportional to log(n) with morespace.

a b

sumco ci

a b

sumco ci

a b

sumco ci

a b

sumco ci

a b

sumco ci

4CMPE12c Cyrus Bazeghi

Addition: unsigned

Just like the simple addition given earlier:

Examples:

(we are ignoring overflow for now)

100001 (33) 00001010 (10)+011101 (29) + 00001110 (14)111110 (62) 00011000 (24)

5CMPE12c Cyrus Bazeghi

Addition: 2’s complement•Just like unsigned addition•Assume 6-bit and observe:

000011 (3)+111100 (-4)

111111 (-1)

101000 (-24)+010000 (16)

111000 (-8)

111111 (-1)+001000 (8)

1000111 (7)

•Ignore carry-outs (overflow)•Sign bit is in the 2n-1 bit position•What does this mean for adding different signs?

6CMPE12c Cyrus Bazeghi

Addition: 2’s complement

More examples: Convert to 2SC and do the addition

-20 + 15

5 + 12

-12 + -25

7CMPE12c Cyrus Bazeghi

Addition: sign magnitude

• Add magnitudes only, just like unsigned addition• Do not carry into the sign bit• If a carry out of the MSB of magnitude then

overflowed• Add only integers of like sign (“+ to +” OR “– to –” )• Sign of the result is same as sign of the addends

8CMPE12c Cyrus Bazeghi

Addition: sign magnitude

Examples:

1 1010 (-10)+ 1 0011 (-3)

1 1101 (-13)

0 0101 (5)+ 0 0011 (3)

0 1000 (8)

0 01011 (11)+ 1 01110 (-14)

Cannot add!!! This is a subtraction

9CMPE12c Cyrus Bazeghi

SubtractionGeneral rules:

1 – 1 = 00 – 0 = 01 – 0 = 1

10 – 1 = 10 – 1 = need to borrow!

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

10CMPE12c Cyrus Bazeghi

Subtraction: 2’s complement

Don’t. Just use addition:

x – y x + (-y)

Example:

10110 (-10)- 00011 (3)

10110 (-10)+11101 (-3)

1 10011 (-13)

11CMPE12c Cyrus Bazeghi

Subtraction: 2’s complement

Can also flip bits of bottom # and add an LSB carry in, so for -10 - 3 we get:

110110

+ 11100110011

(throw away carry out)

“add 1”

“flip bits of bottom number”

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

12CMPE12c Cyrus Bazeghi

Subtraction: unsigned

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

11100 (+28)- 10110 (+22)

Becomes 111100

+01001

100110 Only take 5 bits of result

13CMPE12c Cyrus Bazeghi

Subtraction: sign magnitude• If signs are different, then change the problem to addition

• If the signs are the same then do subtraction• compare magnitudes• subtract smaller from larger• if the order was switched, then switch the sign of the result

14CMPE12c Cyrus Bazeghi

Subtraction: sign magnitude

0 11000 (24)- 0 00111 (7)1 10001 (-17)

becomes0 00111 (7)- 0 11000 (24)

Switched sign since the order of the subtraction was reversed

For example:

Evaluation of the sign bit is not part of the arithmetic, it is determined by comparing magnitudes

1 11000 (-24)- 1 00010 (-2)

1 10110 (-22)

15CMPE12c Cyrus Bazeghi

Overflow in Addition

Unsigned: When there is a carry out of the MSB

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

16CMPE12c Cyrus Bazeghi

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

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

Overflow in Addition

carry out from MSB of magnitude

110001 (-1)

17CMPE12c Cyrus Bazeghi

2’s complement: When the signs of the addends are the same, but the sign of the result is different

0011 (3)+ 0110 (6)

Overflow in Addition

1001 (-7)

Adding 2 numbers of opposite signs never overflows

Why?

18CMPE12c Cyrus Bazeghi

Overflow in Subtraction

Unsigned: if result is negative

Signed magnitude: never happens when actually doing subtraction

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

19CMPE12c Cyrus Bazeghi

Unsigned Binary MultiplicationThe multiplicand is multiplied by the multiplier toproduce the product, the sum of partial products

multiplicand X multiplier = product0011 (+3)

x 0110 (+6)00000011001100000010010 (+18)

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

20CMPE12c Cyrus Bazeghi

2’s Complement Multiplication•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

+ 0000000011110001 (-15)

Only need 8 bits for result

21CMPE12c Cyrus Bazeghi

Division

Only required to know for unsigned binary

Just like you do with decimal long hand

14/3 = …

22CMPE12c Cyrus Bazeghi

Logical Operations

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

In1 In2 & | ~(&) ~(|) ^ ~(^)0 0 0 0 1 1 0 10 1 0 1 1 0 1 01 0 0 1 1 0 1 01 1 1 1 0 0 0 1

(AND OR NAND NOR XOR XNOR )

23CMPE12c Cyrus Bazeghi

Logical Operations

In LC-3, done bit-wise in parallel for corresponding bits

Example:X = 0011Y = 1010

X AND Y = ?

So how do an OR? How about an XOR?

24CMPE12c Cyrus Bazeghi

MaskingMasking refers to using AND operations to isolate bits in a word Example:

ab .FILL 0x6162mask .FILL 0x00ffb .FILL 0x0062

LD R2, bLD R5, abLD R3, maskAND R1, R5, R3JSR Sub ; R0 = R2 – R1BRz found_b ; ‘b’ is 0x62 in ASCII

Does this code find “b”?

25CMPE12c Cyrus Bazeghi

Masking

How about adding this, does it work?

ab .FILL 0x6162mask2 .FILL 0xff00a .FILL 0x0061

LD R2, aLD R5, abLD R3, mask2AND R1, R5, R3JSR Sub ; R0 = R2 – R1BRz found_a ; ‘a’ is 0x61 in ASCII

Does this code find “a”?

26CMPE12c Cyrus Bazeghi

Shifts and RotatesLogical Operations

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 right by 1 )

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 11010100 (shift left by 2 )•Can do by adding number to it self

27CMPE12c Cyrus Bazeghi

Logical Operations: 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 1111 (right by 2)

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)

28CMPE12c Cyrus Bazeghi

Logical Operations: Shifts and Rotates

Rotate left•Move bits to the left, same order•Put the bit(s) that pop off the MSB into the LSB•No bits are thrown away or lost

00110101 01101010 (rotate by 1)1100 1001 (rotate by 1)

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 10011010 (rotate by 1)1101 0111 (rotate by 2)