Chapter5

Chapter5 - Chapter 5 Arithmetic and Chapter Logical...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 5: Arithmetic and Chapter Logical Operations Logical EEC 70 Fall 2010 Professor Wilken 1 Integer Operations Integer operations built in hardware by essentially all processors include: essentially • Addition • Subtraction • Multiplication • Division • Logical (NOT, AND, OR, NAND, NOR, XOR, XNOR) • Shift/Rotate 2 Logical (Boolean) Operations Logical operations are defined using a truth table, which specifies the output for truth which each input each Unary (1-operand) boolean operations: Unary Operations Input Input zero one not not same 0 0 1 1 0 1 0 1 0 1 3 Binary (2-Operand) Logical Operators 16 possible logical operations, one for each possible truth table. Not all are named (?), or commonly used, some are really unary: commonly Input Operations AB zero zero nor nor 00 0 1 01 0 10 11 ? notA notA ? notB notB xor nand 0 1 0 1 0 1 0 1 1 0 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 4 Binary Logical Operators (cont.) Input Operations AB and and 00 0 01 xnor B ? A ? or or one 1 0 1 0 1 0 1 0 0 1 1 0 0 1 1 10 0 0 0 0 1 1 1 1 11 1 1 1 1 1 1 1 1 5 SAL Logical Operations SAL supports all of the named logical operations: NOT, AND, OR, NAND, NOR, XOR, XNOR: NOT, not x,y and x,y,z or x,y,z nand x,y,z nor x,y,z xor x,y,z xnor x,y,z Logical operations are done in parallel for each bit position: each xor A,B,C ⇒ A = B xor C ⇒ A0 = B0 xor C0, A1 = xor xor B1 xor C1, ... A31 = B31 xor C31 6 Extract (Mask) Logical operations can be used to extract value of certain bit positions, setting the rest to 0. Often called masking, masking because certain bits are set to 0 (are masked) because • supposed we are doing arithmetic using only the lower 16 bits of a 32 bit integer ⇒ set the higher 16 bits to 0: set .data int: .word mask: .word result: .word 0x12345678 0x0000ffff ... and result,int,mask # result = 0x00005678 7 Merge Sometimes necessary to change specific bits to new value: • mask the old bits using logical AND mask AND • then merge the new bits using logical OR: then OR Example: replace least significant hex digit with hex 4: Example: .data int: .word 0x12345678 mask: .word 0xfffffff0 newval: .word 0x00000004 result: .word ... and result,int,mask # result is 0x12345670 or result,result,newval # result is 0x12345674 8 Shift Operations Sometimes necessary to move bits to a different position within a word. different SAL allows 3 types of bit shifting: SAL • Logical shift • Arithmetic shift • Rotate Can shift to right or left by a specified number of bit positions (0 to 31) number 9 Logical Shift Logical shift left: Logical sll x,y,AMT • Shift each bit AMT positions to the left, where 0 <=AMT <= 31 Shift AMT • throw away AMT bits that shift past the MSB • insert AMT new 0s in the AMT LSB-positions Example: sll x,y,7 y: 10110011100011110000111110000011 y: 1011001 x: 11000111100001111100000110000000 sll left allows unsigned multiple by powers of 2, faster sll left than mul mul 10 Logical Shift Logical shift left: Logical sll x,y,AMT • Shift each bit AMT positions to the left, where 0 <=AMT <= 31 • throw away AMT bits that shift past the MSB • insert AMT new 0s in the AMT LSB-positions Example: sll x,y,7 y: 10110011100011110000111110000011 y: 1011001 x: 11000111100001111100000110000000 x: 1100011110000111110000011 sll left allows unsigned multiple by powers of 2, faster sll left than mul mul 11 Logical Shift (cont.) sll allows simple bit test: sll allows bit sll x,y,(31 - bit#) sll bit#) bltz x, bit_is_set Logical shift right: Logical # move bit to sign bit position move # if result is negative, bit is a ‘1’ if srl x,y,AMT • Shift each bit AMT positions to the right, where 0 <= AMT Shift <= AMT <= 31 31 • throw away AMT bits that shift past the LSB • introduce AMT new 0s in the AMT MSB-positions Example: srl x,y,7 y: 10110011100011110000111110000011 y: 1011001110001111000011111 x: 00000001011001110001111000011111 x: 0000000 srl allows unsigned divide by powers of 2, much srl allows faster than div div 12 Logical Shift (cont.) Logical shifts allow method for bit field extract extract To extract bits p through q: To through sll x,y,(31 - p) sll srl x,x,(31-p+q) Example: extract bits 16 through 23 sll x,y,(31-23) = sll x,y,8 srl x,x,(31-23+16) = srl x,y,24 y: 10110011100011110000111110000011 y: 10110011 x: 10001111000011111000001100000000 x: 00000000000000000000000010001111 13 Logical Shift (cont.) Logical shifts allow alternate method for bit field extract. To extract bits p through q: through sll x,y,(31 - p) sll srl x,x,(31-p+q) Example: extract bits 16 through 23 sll x,y,(31-23) = sll x,y,8 srl x,x,(31-23+16) = srl x,y,24 y: 10110011100011110000111110000011 y: 10110011 10001111000011111000001100000000 x: 00000000000000000000000010001111 14 Logical Shift (cont.) Logical shifts allow method for bit field extract. To extract bits p through q: through sll x,y,(31 - p) sll srl x,x,(31-p+q) Example: extract bits 16 through 23 sll x,y,(31-23) = sll x,y,8 srl x,x,(31-23+16) = srl x,y,24 y: 10110011100011110000111110000011 y: 10110011 10001111000011111000001100000000 x: 00000000000000000000000010001111 x: 000000000000000000000000 15 Rotate Rotate Left: Rotate rol x,y,AMT • Shift each bit AMT positions to the left, where 0 <= AMT <= 31 • AMT bits that shift past the MSB wrap around to the AMT LSBpositions Example: rol x,y,7 y: 10110011100011110000111110000011 y: 10110011100011110000 01100111000111100001111100000111 11001110001111000011111000001110 10011100011110000111110000011101 00111000111100001111100000111011 01110001111000011111000001110110 11100011110000111110000011101100 x: 11000111100001111100000111011001 16 Rotate Rotate Left: Rotate rol x,y,AMT • Shift each bit AMT positions to the left, where 0 <= AMT <= 31 • AMT bits that shift past the MSB wrap around to the AMT LSBpositions Example: rol x,y,7 y: 10110011100011110000111110000011 y: 10110011100011110000 01100111000111100001111100000111 1100011110000 11001110001111000011111000001110 10011100011110000111110000011101 00111000111100001111100000111011 01110001111000011111000001110110 11100011110000111110000011101100 x: 11000111100001111100000111011001 17 Rotate Rotate Left: Rotate rol x,y,AMT • Shift each bit AMT positions to the left, where 0 <= AMT <= 31 • AMT bits that shift past the MSB wrap around to the AMT LSBpositions Example: rol x,y,7 y: 10110011100011110000111110000011 y: 10110011100011110000 01100111000111100001111100000111 1100011110000 11001110001111000011111000001110 10011100011110000111110000011101 00111000111100001111100000111011 01110001111000011111000001110110 11100011110000111110000011101100 x: 11000111100001111100000111011001 18 Rotate Rotate Left: Rotate rol x,y,AMT • Shift each bit AMT positions to the left, where 0 <= AMT <= 31 • AMT bits that shift past the MSB wrap around to the AMT LSBpositions Example: rol x,y,7 y: 10110011100011110000111110000011 y: 10110011100011110000 01100111000111100001111100000111 1100011110000 11001110001111000011111000001110 10011100011110000111110000011101 00111000111100001111100000111011 01110001111000011111000001110110 11100011110000111110000011101100 x: 11000111100001111100000111011001 19 Rotate Rotate Left: Rotate rol x,y,AMT • Shift each bit AMT positions to the left, where 0 <= AMT <= 31 • AMT bits that shift past the MSB wrap around to the AMT LSBpositions Example: rol x,y,7 y: 10110011100011110000111110000011 y: 10110011100011110000 01100111000111100001111100000111 1100011110000 11001110001111000011111000001110 10011100011110000111110000011101 00111000111100001111100000111011 01110001111000011111000001110110 11100011110000111110000011101100 x: 11000111100001111100000111011001 20 Rotate Rotate Left: Rotate rol x,y,AMT • Shift each bit AMT positions to the left, where 0 <= AMT <= 31 • AMT bits that shift past the MSB wrap around to the AMT LSBpositions Example: rol x,y,7 y: 10110011100011110000111110000011 y: 10110011100011110000 01100111000111100001111100000111 1100011110000 11001110001111000011111000001110 10011100011110000111110000011101 00111000111100001111100000111011 01110001111000011111000001110110 11100011110000111110000011101100 x: 11000111100001111100000111011001 21 Rotate Rotate Left: Rotate rol x,y,AMT • Shift each bit AMT positions to the left, where 0 <= AMT <= 31 • AMT bits that shift past the MSB wrap around to the AMT LSBpositions Example: rol x,y,7 y: 10110011100011110000111110000011 y: 10110011100011110000 01100111000111100001111100000111 1100011110000 11001110001111000011111000001110 10011100011110000111110000011101 00111000111100001111100000111011 01110001111000011111000001110110 11100011110000111110000011101100 x: 11000111100001111100000111011001 22 Rotate Rotate Left: Rotate rol x,y,AMT • Shift each bit AMT positions to the left, where 0 <= AMT <= 31 • AMT bits that shift past the MSB wrap around to the AMT LSBpositions Example: rol x,y,7 y: 10110011100011110000111110000011 y: 10110011100011110000 01100111000111100001111100000111 1100011110000 11001110001111000011111000001110 10011100011110000111110000011101 00111000111100001111100000111011 01110001111000011111000001110110 11100011110000111110000011101100 x: 11000111100001111100000111011001 x: 1100011110000111110000011 23 Rotate Rotate Left: Rotate rol x,y,AMT • Shift each bit AMT positions to the left, where 0 <= AMT <= 31 • AMT bits that shift past the MSB wrap around to the AMT LSBpositions Example: rol x,y,7 y: 10110011100011110000111110000011 y: 1011001 x: 11000111100001111100000111011001 x: 1100011110000111110000011 Rotate Right: Rotate ror x,y,AMT • Same as rol, but reverse direction. Are rol and ror both Same rol but rol and ror both necessary? 24 Arithmetic Shift Arithmetic Shift Right: Arithmetic sra x,y,AMT • Shift each bit AMT positions to the right, where 0 <=AMT <= 31 • throw away AMT bits that shift past the LSB • replicate the sign bit into the AMT MSB-positions Example: sra x,y,7 y: 10110011100011110000111110000011 y: x: 11111111011001110001111000011111 x: 11111111 sra “almost” allows signed division by powers of 2: sra • sra the value -5 by 1, we get -3? the by 3? 11111011 -> 11111101 11111101 25 One-Bit Addition We can specify the operation of a one-bit adder: We • Inputs: operands A and B; Carry In from prior bit Inputs: position position • Outputs: Sum, Carry Out to next bit position Inputs Ai Bi CarryIni Outputs CarryOuti Ai Bi Sumi 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 1 Couti Cini Sumi 26 Building A Multi-bit Adder A simple multi-bit adder is built by connecting simple bit several single-bit adders, setting Cin0 to 0 several bit Cin A3 B3 A2 B2 A1 B1 A0 B0 Cout3 Cin0 Sum3 Sum2 carry a +b -sum Sum1 Sum0 0110 0011 +0001 ----0100 27 One-Bit Subtraction Same as addition, except B’input to one-bit bit adder is either B or B, depending on sub control or depending sub control input: input: • sub = 1 ⇒ subtraction, with B input to adder subtraction, • sub = 0 ⇒ addition, with B input to adder addition, Truth table for B converter: Inputs Inputs sub Bi Outputs Bi’ 0 0 0 0 1 0 1 1 1 Ai Bi’ Couti Cini 1 1 sub Bi 0 Sumi B converter is simple hardware converter 28 Building A Multi-bit Adder/Subtractor A multi-bit adder/subtracter is built by simply multi bit connecting several one-bit adder/subtracters, connecting bit and setting Cin0 equal to sub. equal sub • Simple hardware is a reason for two’s complement B2 B3 B1 • • A3 • B2’ a=3, b=2 A0 A1 A2 B3’ B0 B1’ B0’ • Sum3 Sum2 Sum1 sub carry carry a b’ -sum 1111 1111 0011 1101 ---0001 Sum0 29 Unsigned Multiplication Binary multiplication follows the same basic process as decimal multiplication process • Multiplicand is multiplied by the current digit of is the multiplier. multiplier Partial-product terms are put in proper position: 0110 x 1011 1011 ------------0110 0110 0000 0110 ------------1000010 #A= 6 # B = 11 # Product = 66 30 Unsigned Multiplication • For fixed-digit multiply (computers), all digit digits are present in multiplier, multiplicand, partial-product terms. multiplicand, product We just don’t show all the zeros when We show doing hand multiplication: 00000110 00000110 x 00001011 ---------00000110 00001100 00000000 00110000 ---------01000010 #A= 6 # B = 11 # Product = 66 31 Partial Product Terms Partial product terms are either zero, or the multiplicand times a power of 2 the • Recall that each power of 2 is one left shift 00000110 x 00001011 ---------00000110 00001100 00000000 00110000 ---------01000010 #A= 6 # B = 11 # # # # A x 20 A x 21 0 3 Ax2 # Product= 66 32 Partial Product Terms (cont.) We can add the partial product terms to the product as they are generated: the 00000110 x 00001011 ---------00000000 +00000110 ---------00000110 +00001100 ---------00010010 +00000000 ---------00010010 +00110000 ---------01000010 #A= 6 # B = 11 # Product = 6 # Product = 18 # Product = 18 # Product = 66 33 Basic Multiply Algorithm Multiply can be done using a series of shift and shift and add operations: add product = 0 while multiplier is non-zero if multiplier LSB = 1 product = product + multiplicand multiplier = multiplier >> 1 # look at next bit multiplicand = multiplicand << 1 # times 2 34 Multiply Algorithm Example 00000110 x 00001011 ---------- # A is the multiplicand # B is the multiplier Cycle 1: A= 00000110, B = 00001011 If condition is true, Product = If true 00000110 Cycle 2: A= 00001100, B = 00000101 If condition is true, Product = If true 00010010 Cycle 3: A= 00011000, B = 00000010 If condition is false, Product = 00010010 Cycle 4: A= 00110000, B = 00000001 If condition is true, Product = If true 01000010 Cycle 5: A= 01100000, B = 00000000 Loop ends, Product = Loop 01000010 35 Book’s Multiply Algorithm Figure 5.8 32-bit x 32-bit multiply, for 64 bit result 32 bit (stored in two integers) Multiplicand is pre-shifted by 32 bits: Multiplicand product_high product_low multiplicand 36 Book’s Multiply Algorithm (cont.) For each cycle For • Shift the product one position to the right Shift • If the current multiplier bit is 1, the pre-shifted shifted multiplicand is added to the product: multiplicand 0000...0000110 A: 0000...0000110 B: 0000...0001011 • At the end of cycle, the current multiplier bit At becomes the next bit to the left. • At the start of next cycle, product is shifted: At 00000...000011 A: 0000...0000110 0 B: 0000...0001011 37 Book’s Multiply Algorithm (cont.) The algorithm loops 32 times The In the end, the first partial product term (term 0) has been shifted 32 times and is properly positioned positioned The ith product term (i from 0 to 31) has been shifted 32-i times, and is properly positioned. shifted The book’s algorithm is the dual of the basic The algorithm algorithm: • rather than shifting the multiplicand left and rather keeping the product stationary, the product is shifted right and the multiplicand is stationary 38 Signed Multiplication Basic shift and add algorithm only works for positive numbers positive To include negative numbers with basic method must: method • Save XOR of sign bits to get product sign bit • Convert multiplier and multiplicand to positive • Do shift and add algorithm • Negate result if product sign bit is 1 General method for signed numbers, Booth’s Algorithm, is covered in EEC170 Booth 39 Unsigned Division Division can be done as a series of shift and subtract operations and 01000010 ÷ 00000110 --------- # dividend = 66 # divisor = 6 First step: determine number of significant bits in quotient: significant • Shift divisor left until shifted divisor is > Shift dividend dividend 01000010 01100000 # dividend # shifted divisor • Number of shifts is number of significant bits Number in quotient in 40 Unsigned Division Main loop of division algorithm: Main remainder = dividend quotient = 0 For i=1 to number of significant bits divisor = divisor >> 1 quotient = quotient << 1 # make room for next quotient bit If divisor ≤ remainder If remainder = remainder - divisor remainder quotient = quotient + 1 41 Division Example 01000010 ÷ 01100000 01100000 # R = current remainder # D = shifted divisor # 4 significant bits significant Cycle 1: R = 01000010, D = 00110000 D ≤ R is true, quotient = 00000001 is true quotient Cycle 2: R = 00010010, D = 00011000 D ≤ R is false, quotient = 00000010 is quotient Cycle 3: R = 00010010, D = 00001100 D ≤ R is true, quotient = 00000101 is true quotient Cycle 4: R = 00000110, D = 00000110 D ≤ R is true, quotient = 00001011 is true quotient 42 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online