Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic and Logical Operations

Similar presentations


Presentation on theme: "Arithmetic and Logical Operations"— Presentation transcript:

1 Arithmetic and Logical Operations

2 Binary Addition Or in tabular form… x 0011 +y +0001 sum 0100 Carry In
Carry Out 1 2

3 4-bit Ripple-Carry adder: Carry values propagate from bit to bit
Binary Addition And as a full adder… a b sum co ci 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 more space. a b sum co ci a b sum co ci a b sum co ci a b sum co ci 3

4 Addition: unsigned Just like the simple addition given earlier:
Examples: (we are ignoring overflow for now) (33) (10) (29) (14) (62) (24) 4

5 Addition: 2’s complement
Just like unsigned addition Assume 6-bit and observe: (3) (-4) (-1) (-24) (16) (-8) (-1) (8) (7) Ignore carry-outs (overflow) Sign bit is in the 2n-1 bit position What does this mean for adding different signs? 5

6 More examples: Convert to 2SC and do the addition
Addition: 2’s complement More examples: Convert to 2SC and do the addition 5 + 12 6

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

8 Cannot add!!! This is a subtraction
Addition: sign magnitude Examples: (-10) (-3) (-13) (5) (3) (8) (11) (-14) Cannot add!!! This is a subtraction 8

9 Subtraction General Rules: Replace (x – y) with x + (-y) 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 9

10 Subtraction: 2’s Complement
Don’t! Use addition instead (x – y)  x + (-y) Example: 10110 (-10) (-10) (3) (-3) (-13) 10

11 “flip bits of bottom number”
Subtraction: 2’s complement Can also flip bits of bottom # and add an LSB carry in, so for we get: 1 10110 110011 (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. 11

12 Subtraction: Unsigned
For n bits, use the two’s complement method and underflow if result is negative. Example: (+28) (+26) Becomes: (+1) 11000 (+28) 01001 ( -25) (+2) only take 5 bits 12

13 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 13

14 Switched sign since the order of the subtraction was reversed
Subtraction: sign magnitude For example: (7) (24) (24) (7) (-17) becomes Switched sign since the order of the subtraction was reversed Evaluation of the sign bit is not part of the arithmetic, it is determined by comparing magnitudes (-24) (-2) (-22) 14

15 Overflow in Addition Unsigned: if there is a carry-out of the MSB
Ex: (8) 1001 (9) (1) Overflow 15

16 Overflow in Addition Signed Magnitude: if there is a carry-out of the MSB of the magnitude part Ex: (-8) (-9) (1) Overflow 16

17 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: (3) 0110 (6) 1001 (-7) Overflow Cannot overflow if addends are different signs. Why not? 17

18 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. 18

19 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 (+18) 19

20 2’s Complement Multiplication
If negative multiplicand, just sign-extend it. If negative multiplier, take 2SC of both multiplicand and multiplier (-7 x -3 = 7 x 3, and 7 x –3 = -7 x 3) 0011 (3) x 1011 (-5) 1101 (-3) x 0101 (+5) (-15) Only need 8 bits for result 20

21 Division Complex operation Only required to know for unsigned binary
21

22 Logical Operations Operate on raw bits with 1 = true and 0 = false In1
& | ~(&) ~(|) ^ ~(^) 1 (AND OR NAND NOR XOR XNOR ) 22

23 In MAL, done bit-wise in parallel for corresponding bits
Logical Operations In MAL, done bit-wise in parallel for corresponding bits Example: X AND Y = ? X OR Y = ? X NOR Y = ? X XOR Y = ? X = 0011 Y = 1010 23

24 MAL Logical Instructions
Operate bit-wise on 32-bit words # x is destination, y & z are sources not x, y and x, y, z nand x, y, z or x, y, z nor x, y, z xor x, y, z xnor x, y, z 24

25 Example: a: .word 0x00000003 # 0…0 0011 b: .word 0x0000000a # 0…0 1010
MAL Logical Instructions Example: a: .word 0x # 0…0 0011 b: .word 0x a # 0…0 1010 # assume $t0=a and $t1=b and $t2,$t0,$t1 # $t2=$t0 & $t1 or $t3,$t0,$t1 # $t3=$t0 | $t1 25

26 How about adding this, does it work?
MAL Logical Instructions Masking refers to using AND operations to isolate bits in a word (These are SAL instructions. MAL is just the same, but operate on registers rather than words) Example: abcd: .word 0x mask: .word 0x000000ff mask2: .word 0x0000ff00 tmp: .word and tmp, abcd, mask beq tmp, ‘d’, found_d # ‘d’ == 0x64 in ASCII How about adding this, does it work? and tmp, abcd, mask2 beq tmp, ’c’, found_c # ‘c’ == 0x63 in ASCII 26

27 Move bits to the right, same order
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  (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  (shift left by 2 ) 27

28 Move bits to the right, same order
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  (right by 1)  (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  (left by 1) 28

29 Move bits to the left, same order
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  (rotate by 1) 1100  (rotate by 1) Rotate right Move bits to the right, same order Put the bit that pops off the LSB into the MSB  (rotate by 1) 1101  (rotate by 2) 29

30 sll $t0, $t1, value # shift left logical
MAL shift and Rotate Instructions sll $t0, $t1, value # shift left logical srl $t0, $t1, value # shift right logical sra $t0, $t1, value # shift right arithmetic Example: abcd: .word 0x mask: .word 0x0000ff00 lw $t1, abcd lw $t2, mask and $t3, $t1, $t2 srl $t3, $t3, 8 li $t4, ‘c’ beq $t3, $t4, found_c 30


Download ppt "Arithmetic and Logical Operations"

Similar presentations


Ads by Google