Download presentation
Presentation is loading. Please wait.
1
Chapter 5 Arithmetic and Logical Operations
2
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
3
ab sum coci 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 more space. ab sum coci ab sum coci ab sum coci ab sum coci
4
Addition: unsigned Just like the simple addition given earlier: Examples: 100001 (33)00001010 (10) +011101 (29) +00001110 (14) 111110 (62)00011000 (24) (ignoring overflow)
5
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 2 n-1 ’s A carry into the 2 n-1 ’s place is a 2 n-1 positive weight What does this mean for adding different signs?
6
Addition: 2’s complement -20 + 15 5 + 12 -12 + -20
7
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!!
8
Subtraction General rules: 1 – 1 = 0 0 – 0 = 0 1 – 0 = 1 10 – 1 = 1 0 – 1 = borrow! Or replace (x – y) with x + (-y) Can replace subtraction with additive inverse and addition
9
Subtraction: 2’s complement Use addition: x – y x + (-y) Examples: 10110 (-10) -00011 (3) 10110 (-10) +11101 (-3) 1 11011 (-13)
10
Subtraction: 2’s complement Can also flip bits of y and add an LSB carry in: 1 10110(-10) + 11100(-3) 110011(-13) (throw away carry out) Addition and subtraction are simple in 2’s complement, just need an adder and inverters.
11
Subtraction: unsigned For n-bits use the 2’s complement method and overflow if negative 11100 (+28) -10110 (+22) Becomes
12
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 was reversed 000111 (7) 111000 (-24) - 011000 (24) - 100010 (-2) do 110110 (-22) 011000 (24) - 000111 (7) 110001 (-17)
13
Overflow in Addition Unsigned When there is a carry out of the MSB Signed magnitude When there is a carry out of the MSB of the magnitude 2’s complement When the signs of the addends are the same, and the sign of the result is different 1000 (8) +1001 (9) 10001 (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
14
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.
15
Multiplication The multiplicand is multiplied by the multiplier to produce the product, the sum of partial products Longhand, it looks just like decimal Result can require twice as many bits as the larger multiplicand (why?) multiplicand X multiplier = product 0011 (+3) x0110 (+6) 0000 0011 0000 0010010 (+18)
16
Multiplication: For 2’s Complement 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) x1011 (-5) 1101 (-3) x0101 (+5) 11111111101 0000000000 111111101 +00000000 11111110001 (-15) Or use Booth’s algorithm to multiply signed numbers Also allows multiplying groups of bits. You will learn this in CE110.
17
Division Unsigned only!!! 14/3 = …
18
Logical Operations Operate on raw bits with 1 = true and 0 = false In1In2&|~(&)~(|)^~(^) 00001101 01011010 10011010 11110001 In computers, done bit-wise: in parallel for corresponding bits X = 0011 Y = 1010 X AND Y X OR Y X NOR Y X XOR Y
19
MAL Logical Instructions Operate bit-wise on 32-bit.words notx,ynandx,y,z andx,y,zxorx,y,z orx,y,zxnorx,y,z norx,y,z Example: a:.word0x00000003# 0…0 0011 b:.word0x0000000a# 0…0 1010 # assume $t0=a and $t1=b and$t2,$t0,$t1# $t2=$t0 & $t1 or$t3,$t0,$t1# $t3=$t0 | $t1
20
Logical Instructions Masking refers to using AND operations to isolate bits in a word Example: abcd:.word0x61626364 mask:.word0x000000ff mask2:.word0x0000ff00 tmp:.word andtmp, abcd, mask beqtmp, ‘d’,found_d# ‘d’ == 0x64 in ASCII How about adding: and tmp,abcd,mask2 beq 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.
21
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)
22
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)
23
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 001101010 1100 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 10011010 1100 0110
24
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 rol$t0, $t1, value# rotate left ror$t0, $t1, value# rotate right Example: abcd:.word0x61626364 mask:.word0x0000ff00 lw$t1, abcd lw$t2, mask and$t3, $t1, $t2 srl$t3, $t3, 8 li$t4, ‘c’ beq$t3, $t4, found_c
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.