870 likes | 883 Views
Conversion to Larger Number of Bits. Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number in right most 16 bits and fill with 0’s. Conversion to Larger Number of Bits. Ex: Immediate Field (signed 16 bit) to 32 bit
E N D
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number in right most 16 bits and fill with 0’s.
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number in right most 16 bits and fill with 0’s. 0011 1101 0101 0010 0000 0000 0000 0000 0011 1101 0101 0010
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number in right most 16 bits and fill with 0’s. Negative numbers have implied 1’s to the left because it’s the complement of a positive number’s 0’s. So, put 16 bit number in right most 16 bits and fill with 1’s.
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number in right most 16 bits and fill with 0’s. Negative numbers have implied 1’s to the left because it’s the complement of a positive number’s 0’s. So, put 16 bit number in right most 16 bits and fill with 1’s. 1100 1110 1010 1110 1111 1111 1111 1111 1100 1110 1010 1110
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number in right most 16 bits and fill with 0’s. Negative numbers have implied 1’s to the left because it’s the complement of a positive number’s 0’s. So, put 16 bit number in right most 16 bits and fill with 1’s. Or, put 16 bits in the right most 16 bits and fill with the sign bit. This is called sign extension.
Division – Positive numbers Quotient Divisor 0010 00000111 Dividend
Division – Positive numbers Trial 1 Quotient Divisor 0010 00000111 Dividend or Remainder 1110 1110 < 0 Change Quotient bit to 0 and restore This must be true or the quotient would exceed 4 bits, so start with shift
Division – Positive numbers Trial 1 Quotient Divisor 0010 00000111 Dividend or Remainder 1110 1110 < 0 Change Quotient bit to 0 and restore, then shift and try again
Division – Positive numbers Trial 01 Quotient Divisor 0010 00000111 Dividend or Remainder 1110 1111 < 0 Change Quotient bit to 0 and restore, then shift and try again
Division – Positive numbers Trial 001 Quotient Divisor 0010 00000111 Dividend or Remainder 1110 0001 > 0 shift and use new remainder
Division – Positive numbers Trial 001 Quotient Divisor 0010 00000111 Dividend or Remainder 1110 00011 New Remainder
Division – Positive numbers Trial 0011 Quotient Divisor 0010 00000111 Dividend or Remainder 1110 00011 1110 0001 Remainder
Same basic hardware as Multiply Divisor 0010 0010 00000111 4 bit ALU Control Test Remainder 00000111 Shift R / L Initialize with Dividend 0111
1. Shift Remainder left Divisor 0010 0010 00000111 4 bit ALU Control Test Remainder 00000111 Shift R / L Dividend 0111
Shift Remainder left • Subtract the Divisor Divisor 2’s complement 1110 0010 0010 00000111 1110 4 bit ALU Control Test Remainder 00001110 Shift R / L Dividend 0111
Shift Remainder left • Subtract the Divisor • < 0, add Divisor Divisor 1110 0010 0010 00000111 1110 1110 0010 4 bit ALU Control Test Remainder 11101110 Shift R / L Dividend 0111
Shift Remainder left • Subtract the Divisor • < 0, add Divisor • Shift left, Set 0 Divisor 1110 0010 0010 00000111 1110 1110 0010 0000 4 bit ALU Control Test Remainder 00001110 Shift R / L Dividend 0111
Subtract the Divisor Divisor 1110 0010 0 0010 00000111 1110 4 bit ALU Control Test Remainder 00011100 Shift R / L Dividend 0111
Subtract the Divisor • < 0, add Divisor Divisor 1110 0010 0 0010 00000111 1110 1111 0010 4 bit ALU Control Test Remainder 11111100 Shift R / L Dividend 0111
Subtract the Divisor • < 0, add Divisor • Shift, Set 0 Divisor 1110 0010 0 0010 00000111 1110 1111 0010 0001 4 bit ALU Control Test Remainder 00011100 Shift R / L Dividend 0111
Subtract the Divisor Divisor 1110 0010 00 0010 00000111 1110 4 bit ALU Control Test Remainder 00111000 Shift R / L Dividend 0111
Subtract the Divisor • >0, shift left, set 1 Divisor 1110 0010 00 0010 00000111 1110 0001 4 bit ALU Control Test Remainder 00011000 Shift R / L Dividend 0111
Subtract the Divisor Divisor 1110 0010 001 0010 00000111 1110 00011 1110 4 bit ALU Control Test Remainder 00110001 Shift R / L Dividend 0111
Subtract the Divisor • >0, Shift left, set 1 Divisor 1110 0010 001 0010 00000111 1110 00011 1110 0001 4 bit ALU Control Test Remainder 00010001 Shift R / L Dividend 0111
Subtract the Divisor • >0, Shift left, set 1 • Shift left half right Divisor 1110 0010 0011 0010 00000111 1110 00011 1110 00010 4 bit ALU Control Test Remainder 00100011 Shift R / L Dividend 0111
Quotient = 0011 • Remainder = 0001 Divisor 1110 0010 0011 0010 00000111 1110 00011 1110 0001 4 bit ALU Control Test Remainder 00010011 Shift R / L Dividend 0111
Non-Restoring Division r = Remainder d = Divisor r – d < 0, Qi = 0,restore, shift and subtract Divisor
Non-Restoring Division r = Remainder d = Divisor r – d < 0, Qi = 0,restore, shift and subtract Divisor (r – d + d)*2 – d = (r – d)*2 + 2d – d = ( r – d)*2 + d
Non-Restoring Division r = Remainder d = Divisor r – d < 0, Qi = 0,restore, shift and subtract Divisor (r – d + d)*2 – d = (r – d)*2 + 2d – d = ( r – d)*2 + d If r – d < 0, Qi = 0, shift r-d and add Divisor
Non-Restoring Division r = Remainder d = Divisor r – d < 0, Qi = 0,restore, shift and subtract Divisor (r – d + d)*2 – d = (r – d)*2 + 2d – d = ( r – d)*2 + d If r – d < 0, Qi = 0, shift r-d and add Divisor • Algorithm for Non- restoring Division • Subtract Divisor from Remainder • If r-d > = 0, Qi = 1, shift and subtract divisor • If r-d < 0, Qi =0, shift and add divisor • 4. After n bits, if Remainder is negative, restore
Non –Restoring Division • Shift Remainder Register left 1 bit • Subtract the Divisor Register from the left half • of the Remainder Register Test Remainder >= 0 < 0 3b. Shift Remainder left and set bit 0 to 0 3a. Shift Remainder left and set bit 0 to 1 Yes n Repetitions ? No n Repetitions ? Yes No 4b. Add Divisor to left half of Remainder Register 4a. Subtract Divisor from left half of Remainder Register 5b. Shift left half of Remainder right one bit and add Divisor to left half of Remainder 5a. Shift left half of Reminder right one bit
Non –Restoring Division Divisor 0010 0010 00000111 4 bit ALU Control Test Remainder 00000111 Shift R / L Initialize with Dividend 0111
1. Shift Remainder left Divisor 0010 0010 00000111 4 bit ALU Control Test Remainder 00000111 Shift R / L Dividend 0111
Shift Remainder left • Subtract the Divisor Divisor 2’s complement 1110 0010 0010 00000111 1110 4 bit ALU Control Test Remainder 00001110 Shift R / L Dividend 0111
Shift Remainder left • Subtract the Divisor • < 0, Shift left, Set 0 2’s complement Divisor 1110 0010 0010 00000111 1110 1110 4 bit ALU Control Test Remainder 11101110 Shift R / L Dividend 0111
Add divisor 2’s Complement Divisor 1110 0010 0 0010 00000111 1110 11101 4 bit ALU Control Test Remainder 11011110 Shift R / L Dividend 0111
Add divisor 2’s Complement Divisor 1110 0010 0 0010 00000111 1110 11101 0010 4 bit ALU Control Test Remainder 11011110 Shift R / L Dividend 0111
Add divisor • < 0, Shift left, set 0 2’s Complement Divisor 1110 0010 0 0010 00000111 1110 11101 0010 1111 4 bit ALU Control Test Remainder 11111110 Shift R / L Dividend 0111
Add divisor 2’s Complement Divisor 1110 0010 00 0010 00000111 1110 11101 0010 11111 0010 4 bit ALU Control Test Remainder 11111100 Shift R / L Dividend 0111
Add divisor • > 0, Shift left, set 1 2’s Complement Divisor 1110 0010 00 0010 00000111 1110 11101 0010 11111 0010 0001 4 bit ALU Control Test Remainder 00011100 Shift R / L Dividend 0111
Subtract divisor 2’s Complement Divisor 1110 0010 001 0010 00000111 1110 11101 0010 11111 0010 00011 1110 4 bit ALU Control Test Remainder 00111001 Shift R / L Dividend 0111
Subtract divisor • >0, Shift left, set 1 2’s Complement Divisor 1110 0010 001 0010 00000111 1110 11101 0010 11111 0010 00011 1110 0001 4 bit ALU Control Test Remainder 00011001 Shift R / L Dividend 0111
Subtract divisor • >0, Shift left, set 1 • Shift Left Half right 2’s Complement Divisor 1110 0010 0011 0010 00000111 1110 11101 0010 11111 0010 00011 1110 0001 4 bit ALU Control Test Remainder 00110011 Shift R / L Dividend 0111
Subtract divisor • >0, Shift left, set 1 • Shift Left Half right 2’s Complement Divisor 1110 0010 0011 0010 00000111 1110 11101 0010 11111 0010 00011 1110 0001 4 bit ALU Control Test Remainder 00010011 Shift R / L Dividend 0111
MIPS Instructions for Multiply and Divide Hi,Lo are two 32 bit registers for Product and Remainder divide div $s1, $s2 # Lo = $s1/$s2 = quotient # Hi = $s1 mod$s2 = remainder For signed numbers, 1. determine the sign of the quotient 2. convert to positive representation 3. divide 4. determine the sign and convert to 2’s complement if needed
MIPS Instructions for Multiply and Divide Hi,Lo are two 32 bit registers for Product and Remainder divide div $s1, $s2 # Lo = $s1/$s2 = quotient # Hi = $s1 mod$s2 = remainder divide unsigned divu $s1, $s2 # unsigned version of div
MIPS Instructions for Multiply and Divide Hi,Lo are two 32 bit registers for Product and Remainder multiply mult $s1, $s2 # Hi,Lo = $s1 x $s2 multiply unsigned multu $s1, $s2 # Hi,Lo = $s1 x $s2 ( unsigned) move from Hi mfhi $s3 # $s3 = Hi move from Lo mflo $s3 # $s3 = Lo
Short Cuts for Multiply and Divide For Positive Numbers 1. Multiply by 2k is the same as shift k to the left, 0 fill 2. Divide by 2k is the same as shift k to the right, 0 fill
Short Cuts for Multiply and Divide For Positive Numbers 1. Multiply by 2k is the same as shift k to the left, 0 fill 2. Divide by 2k is the same as shift k to the right, 0 fill For 2’s Complement Numbers It does not always work!