Arithmetic and Logical Operations. CMPE12cGabriel Hugh Elkaim 2 x 0011 +y+0001 sum 0100 Or in...

30
Arithmetic and Logical Operations
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    0

Transcript of Arithmetic and Logical Operations. CMPE12cGabriel Hugh Elkaim 2 x 0011 +y+0001 sum 0100 Or in...

Arithmetic and Logical Operations

2CMPE12c Gabriel Hugh Elkaim

x 0011+y +0001 sum 0100

Or in tabular form…

Binary Addition

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

3CMPE12c Gabriel Hugh Elkaim

Binary 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 propagate

carry proportional to log(n) with morespace.

a b

sumco ci

a b

sumco ci

a b

sumco ci

a b

sumco ci

4CMPE12c Gabriel Hugh Elkaim

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 Gabriel Hugh Elkaim

Addition: 2’s complement

111111 (-1)+001000 (8)

1000111 (7)

101000 (-24)+010000 (16)

111000 (-8)

000011 (3)+111100 (-4)

111111 (-1)

•Just like unsigned addition•Assume 6-bit and observe:

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

6CMPE12c Gabriel Hugh Elkaim

Addition: 2’s complement

-20 + 15

5 + 12

-12 + -25

More examples: Convert to 2SC and do the addition

7CMPE12c Gabriel Hugh Elkaim

Addition: sign magnitude

• Add magnitudes only, just like unsigned addition

• Do not carry into the sign bit• If there is a carry out of the MSB of the

magnitude, then overflowed• Add only integers of like sign (+ to + and

– to –)• Sign of the result is the same as the

addends

8CMPE12c Gabriel Hugh Elkaim

Examples:

0 0101 (5)+ 0 0011 (3)

0 1000 (8)

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

1 1101 (-13)

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

Cannot add!!! This is a subtraction

Addition: sign magnitude

9CMPE12c Gabriel Hugh Elkaim

Subtraction

• General Rules: 1 – 1 = 0 0 – 0 = 0 1 – 0 = 1 10 – 1 = 1 0 – 1 = need to borrow

• Replace (x – y) with x + (-y)– can replace subtraction with addition to the

additive inverse

10CMPE12c Gabriel Hugh Elkaim

Subtraction: 2’s Complement

• Don’t! Use addition instead• (x – y) x + (-y)

• Example:

10110 (-10) 10110 (-10)-00011 (3) +11101 (-3)

110011 (-13)

11CMPE12c Gabriel Hugh Elkaim

Subtraction: 2’s complement

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

1 10110 + 11100 110011

(throw away carry out)

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

“add 1”

“flip bits of bottom number”

12CMPE12c Gabriel Hugh Elkaim

Subtraction: Unsigned

• For n bits, use the two’s complement method and underflow if result is negative.

• Example: 11000 (+28) -10110 (+26)

• Becomes: 1 (+1) 11000 (+28) 01001 ( -25) 1 00010 (+2) only take 5

bits

13CMPE12c Gabriel Hugh Elkaim

Subtraction: Sign Magnitude

• If signs are different, change problem to addition

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

result

14CMPE12c Gabriel Hugh Elkaim

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

1 10110 (-22)

Subtraction: sign magnitude

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

becomes

0 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

15CMPE12c Gabriel Hugh Elkaim

Overflow in Addition

• Unsigned: if there is a carry-out of the MSB

• Ex: 1000 (8) 1001 (9) 1 0001 (1)Overflow

16CMPE12c Gabriel Hugh Elkaim

Overflow in Addition

• Signed Magnitude: if there is a carry-out of the MSB of the magnitude part

• Ex: 1 1000 (-8) 1 1001 (-9) 1 10001 (1)Overflow

17CMPE12c Gabriel Hugh Elkaim

Overflow in Addition

• 2’s Complement: if the sign of the addends are the same sign and the sign of the result is different:

• Ex: 0011 (3) 0110 (6) 1001 (-7) Overflow

• Cannot overflow if addends are different signs.

• Why not?

18CMPE12c Gabriel Hugh Elkaim

Underflow in Subtraction

• Unsigned: if the result is negative.

• Signed Magnitude: never happens when actually doing subtraction.

• 2’s Complement: never do subtraction, do addition and use the rules for overflow.

19CMPE12c Gabriel Hugh Elkaim

Unsigned Binary Multiplication• The multiplicand is multiplied by the multiplier to produce the product, the sum of the partial products

multiplicand x multiplier = product

• Example:

• Longhand, it looks just like decimal• Result can require twice as many bits as

the operands. Why?

0011 (+3) x 0110 (+6) 0000 0011 0011 0000 0010010 (+18)

20CMPE12c Gabriel Hugh Elkaim

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) 11111111101 0000000000 111111101 + 00000000 11110001 (-15)

Only need 8 bits for result

21CMPE12c Gabriel Hugh Elkaim

Division

• Complex operation• Only required to know for unsigned

binary

• Ex: 14/3 = ?

22CMPE12c Gabriel Hugh Elkaim

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

(AND OR NAND NOR XOR XNOR )

23CMPE12c Gabriel Hugh Elkaim

In MAL, done bit-wise in parallel for corresponding bits

X = 0011Y = 1010

X AND Y = ?X OR Y = ?X NOR Y = ?X XOR Y = ?

Logical Operations

Example:

24CMPE12c Gabriel Hugh Elkaim

MAL Logical Instructions

Operate bit-wise on 32-bit words

# x is destination, y & z are sourcesnot x, yand x, y, znand x, y, zor x, y, znor x, y, zxor x, y, zxnor x, y, z

25CMPE12c Gabriel Hugh Elkaim

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

Example:

MAL Logical Instructions

26CMPE12c Gabriel Hugh Elkaim

MAL Logical Instructions

Masking refers to using AND operations to isolate bitsin a word (These are SAL instructions. MAL is just the same, but operate on registers rather than words)

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

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

How about adding this, does it work?and tmp, abcd, mask2beq tmp, ’c’, found_c # ‘c’ == 0x63 in ASCII

27CMPE12c Gabriel Hugh Elkaim

Logical Operations: 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 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 )

28CMPE12c Gabriel Hugh Elkaim

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)

29CMPE12c Gabriel Hugh Elkaim

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)

30CMPE12c Gabriel Hugh Elkaim

MAL shift and Rotate Instructions

sll $t0, $t1, value # shift left logicalsrl $t0, $t1, value # shift right logicalsra $t0, $t1, value # shift right arithmetic

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

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