Arithmetic and Logical Operations
2 CMPE2c x +y + sum Or in tabular form Binary Addition Carry Out Sum B A Carry In
Binary Addition And as a full adder a b co ci sum 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 co ci sum a b co ci sum a b co ci sum a b co ci sum CMPE2c 3
Addition: unsigned Just like the simple addition given earlier: Examples: (33) () + (29) + (4) (62) (24) (we are ignoring overflow for now) CMPE2c 4
Addition: 2 s complement Just like unsigned addition Assume 6-bit and observe: (3) + (-4) (-) (-24) + (6) (-8) (-) + (8) (7) Ignore carry-outs (overflow) Sign bit is in the 2 n- bit position What does this mean for adding different signs? CMPE2c 5
Addition: 2 s complement More examples: Convert to 2SC and do the addition -2 + 5 5 + 2-2 + -25 CMPE2c 6
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 CMPE2c 7
Addition: sign magnitude Examples: (5) + (3) (8) (-) + (-3) (-3) () + (-4) Cannot add!!! This is a subtraction CMPE2c 8
Subtraction General Rules: = = = = = need to borrow Replace (x y) with x + (-y) can replace subtraction with addition to the additive inverse CMPE2c 9
Subtraction: 2 s Complement Don t! Use addition instead (x y) x + (-y) Example: (-) (-) - (3) + (-3) (-3) CMPE2c
Subtraction: 2 s complement Can also flip bits of bottom # and add an LSB carry in, so for - - 3 we get: + add flip bits of bottom number (throw away carry out) Addition and subtraction are simple in 2 s complement, just need an adder and inverters. CMPE2c
Subtraction: Unsigned For n bits, use the two s complement method and underflow if result is negative. Example: (+28) - (+26) Becomes: (+) (+28) ( -25) (+2) only take 5 bits CMPE2c 2
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 CMPE2c 3
Subtraction: sign magnitude For example: (7) - (24) becomes (24) - (7) (-7) Switched sign since the order of the subtraction was reversed (-24) - (-2) (-22) Evaluation of the sign bit is not part of the arithmetic, it is determined by comparing magnitudes CMPE2c 4
Overflow in Addition Unsigned: if there is a carry-out of the MSB Ex: (8) (9) () Overflow CMPE2c 5
Overflow in Addition Signed Magnitude: if there is a carryout of the MSB of the magnitude part Ex: (-8) (-9) () Overflow CMPE2c 6
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) (6) (-7) Overflow Cannot overflow if addends are different signs. Why not? CMPE2c 7
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. CMPE2c 8
Unsigned Binary Multiplication The multiplicand is multiplied by the multiplier to produce the product, the sum of the partial products Example: multiplicand x multiplier = product (+3) x (+6) (+8) Longhand, it looks just like decimal Result can require twice as many bits as the operands. Why? CMPE2c 9
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) (3) x (-5) Only need 8 bits for result (-3) x (+5) + (-5) CMPE2c 2
Division Complex operation Only required to know for unsigned binary Ex: 4/3 =? CMPE2c 2
Logical Operations Operate on raw bits with = true and = false In In2 & ~(&) ~( ) ^ ~(^) (AND OR NAND NOR XOR XNOR ) CMPE2c 22
Logical Operations In MAL, done bit-wise in parallel for corresponding bits Example: X = Y = X AND Y =? X OR Y =? X NOR Y =? X XOR Y =? CMPE2c 23
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 CMPE2c 24
MAL Logical Instructions Example: a:.word x3 # b:.word xa # # assume $t=a and $t=b and $t2,$t,$t # $t2=$t & $t or $t3,$t,$t # $t3=$t $t CMPE2c 25
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 x6626364 mask:.word xff mask2:.word xff tmp:.word and tmp, abcd, mask beq tmp, d, found d # d == x64 in ASCII How about adding this, does it work? and tmp, abcd, mask2 beq tmp, c, found c # c == x63 in ASCII CMPE2c 26
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 into the MSB (shift right by ) Logical left Move bits to the left, same order Throw away the bit that pops off the MSB Introduce a into the LSB (shift left by 2 ) CMPE2c 27
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 ) (right by 2) Arithmetic left Move bits to the left, same order Throw away the bit that pops off the MSB Introduce a into the LSB (left by ) CMPE2c 28
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 ) (rotate by ) 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 (rotate by ) (rotate by 2) CMPE2c 29
MAL shift and Rotate Instructions sll $t, $t, value # shift left logical srl $t, $t, value # shift right logical sra $t, $t, value # shift right arithmetic Example: abcd:.word x6626364 mask:.word xff lw $t, abcd lw $t2, mask and $t3, $t, $t2 srl $t3, $t3, 8 li $t4, c beq $t3, $t4, found c CMPE2c 3