SlideShare a Scribd company logo
 Paper and Pencil Example:
Multiplicand 11002 = 12
Multiplier × 11012 = 13
1100
0000
1100
1100
Product 100111002 = 156
 m-bit multiplicand × n-bit multiplier = (m+n)-bit product
 Accomplished via shifting and addition
 Consumes more time and more chip area than addition
Unsigned Integer Multiplication
Binary multiplication is easy
0 × multiplicand = 0
1 × multiplicand = multiplicand
Sequential Unsigned Multiplication
 Initialize Product = 0
 Check each bit of the Multiplier
 If Multiplier bit = 1 then Product = Product + Multiplicand
 Rather than shifting the multiplicand to the left
Instead, Shift the Product to the Right
Has the same net effect and produces the same result
Minimizes the hardware resources
 One cycle per iteration (for each bit of the Multiplier)
 Addition and shifting can be done simultaneously
 Initialize HI = 0
 Initialize LO = Multiplier
 Final Product = HI and LO registers
 Repeat for each bit of Multiplier
Sequential Multiplication Hardware
= 0
Start
LO[0]?
HI = HI + Multiplicand
32nd
Repetition?
Done
= 1
No
Yes
HI = 0, LO=Multiplier
Shift (Carry, HI, LO) Right 1 bit32-bit ALU
Control
64 bits
32 bits
write
add
LO[0]
Multiplicand
shift right
32 bits
HI LO
32 bits
carry
Sequential Multiplier Example
 Consider: 11002 × 11012 , Product = 100111002
 4-bit multiplicand and multiplier are used in this example
 4-bit adder produces a 5-bit sum (with carry)
1 1 0 0Shift Right (Carry, HI, LO) by 1 bit 0 1 1 1 1 0 0 1
LO[0] = 0 => Do Nothing
1 1 0 0Shift Right (Carry, HI, LO) by 1 bit 0 0 1 1 0 0 1 1
1 1 0 0Shift Right (Carry, HI, LO) by 1 bit 0 1 1 0 0 1 1 0
1 1 0 0Shift Right (Carry, HI, LO) by 1 bit 1 0 0 1 1 1 0 0
2
1 1 0 0 0 0 0 0 1 1 0 1Initialize (HI = 0, LO = Multiplier)0
1
3
4
Multiplicand Product = HI, LOCarryIteration
0 1 1 0 0 1 1 0 1LO[0] = 1 => ADD +
0 1 1 1 1 0 0 1 1LO[0] = 1 => ADD +
1 0 0 1 1 1 0 0 1LO[0] = 1 => ADD +
Next . . .
 Unsigned Integer Multiplication
 Signed Integer Multiplication
 Faster Integer Multiplication
 Integer Division
 Integer Multiplication and Division in MIPS
Signed Integer Multiplication
 So far, we have dealt with unsigned integer multiplication
 First Attempt:
 Convert multiplier and multiplicand into positive numbers
 If negative then obtain the 2's complement and remember the sign
 Perform unsigned multiplication
 Compute the sign of the product
 If product sign < 0 then obtain the 2's complement of the
product
 Better Version:
 Use the unsigned multiplication hardware
 When shifting right, extend the sign of the product
 If multiplier is negative, the last step should be a subtract
Signed Multiplication (Pencil & Paper)
 Case 1: Positive Multiplier
Multiplicand 11002 = -4
Multiplier × 01012 = +5
11111100
111100
Product 111011002 = -20
 Case 2: Negative Multiplier
Multiplicand 11002 = -4
Multiplier × 11012 = -3
11111100
111100
00100 (2's complement of 1100)
Product 000011002 = +12
Sign-extension
Sign-extension
 ALU produces 32-bit result + Sign bit
 Check for overflow
 No overflow  Extend sign-bit of result
 Overflow  Invert sign bit
Sequential Signed Multiplier
= 0
Start
LO[0]?
First 31 iterations: HI = HI + Multiplicand
Last iteration: HI = HI – Multiplicand
32nd
Repetition?
Done
= 1
No
Yes
HI = 0, LO = Multiplier
Shift Right (Sign, HI, LO) 1 bit32-bit ALU
Control
64 bits
32 bits
write
add, sub
LO[0]
Multiplicand
shift right
32 bits
HI LO
32 bitssign
Signed Multiplication Example
 Consider: 11002 (-4) × 11012 (-3), Product = 000011002
 Check for overflow: No overflow  Extend sign bit
 Last iteration: add 2's complement of Multiplicand
1 1 0 0Shift (Sign, HI, LO) right 1 bit 1 1 0 1 1 0 0 1
LO[0] = 0 => Do Nothing
1 1 0 0Shift (Sign, HI, LO) right 1 bit 1 1 1 1 0 0 1 1
1 1 0 0Shift (Sign, HI, LO) right 1 bit 1 1 1 0 0 1 1 0
Shift (Sign, HI, LO) right 1 bit 0 0 0 0 1 1 0 0
2
1 1 0 0 0 0 0 0 1 1 0 1Initialize (HI = 0, LO = Multiplier)0
1
3
4
Multiplicand Product = HI, LOSignIteration
1 1 1 0 0 1 1 0 1LO[0] = 1 => ADD +
1 1 0 1 1 0 0 1 1LO[0] = 1 => ADD +
0 1 0 0 0 0 0 0 1 1 0 0 1LO[0] = 1 => SUB (ADD 2's compl) +
Next . . .
 Unsigned Integer Multiplication
 Signed Integer Multiplication
 Faster Integer Multiplication
 Integer Division
 Integer Multiplication and Division in MIPS
Faster Integer Multiplier
Uses Multiple Adders (Cost vs. Performance)
 Can be pipelined
Using Multiple Adders
 32-bit adder for each bit of the multiplier
 AND multiplicand with each bit of multiplier
 Product = accumulated shifted sum
 Each adder produces a 33-bit output
 Most significant bit is a carry bit
 Array multiplier can be optimized
 Additions can be done in parallel
 Multiple-level tree reduction to produce final product
 Carry save adders reduce delays
Carry Save Adders
 Used when adding multiple numbers (as in multipliers)
 All the bits of a carry-save adder work in parallel
 The carry does not propagate as in a carry-propagate adder
 This is why a carry-save is faster than a carry-propagate adder
 A carry-save adder has 3 inputs and produces two outputs
 It adds 3 numbers and produces partial sum and carry bits
Carry-Propagate Adder
+
a0 b0
s0
+
a1 b1
s1
+
a31 b31
s31
. . .cout cin
Carry-Save Adder
. . .+
a31 b31
s'31c'31
c31
+
a1 b1
s'1c'1
c1
+
a0 b0
s'0c'0
c0
Tree Multiplier - 1 of 2
 Suppose we want to multiply two numbers A and B
 Example on 4-bit numbers: A = a3 a2 a1 a0 and B = b3 b2 b1 b0
 Step 1: AND (multiply) each bit of A with each bit of B
 Requires n2
AND gates and produces n2
product bits
 Position of aibj = (i+j). For example, Position of a2b3 = 2+3 = 5
a0b0a1b0a2b0a3b0
a0b1a1b1a2b1a3b1
a0b2a1b2a2b2a3b2
a0b3a1b3a2b3a3b3
A × B
Tree Multiplier – 2 of 2
Step 2: Use carry save adders to add the partial products
 Reduce the partial products to just two numbers
Step 3: Add last two numbers using a carry-propagate adder
P0
+
a2b0 a1b1
++
+
a3b0 a2b1 a0b0
+
a1b0 a0b1
+
+
+++
+
a3b1 a2b2
+
a3b2
a2b3
a3b3
a1b3 a0b3
a1b2 a0b2
P1P2P3P4P5P6P7
Carry Save Adder
Carry Propagate Adder
Next . . .
 Unsigned Integer Multiplication
 Signed Integer Multiplication
 Faster Integer Multiplication
 Integer Division
 Integer Multiplication and Division in MIPS
Try to see how big a
number can be
subtracted, creating a
digit of the quotient on
each attempt
= 19 Quotient
Divisor 10112 110110012 = 217 Dividend
-1011
10
101
1010
10100
-1011
1001
10011
-1011
10002 = 8 Remainder
Unsigned Division (Paper & Pencil)
Binary division is
accomplished via
shifting and subtraction
Dividend =
Quotient × Divisor
+ Remainder
217 = 19 × 11 + 8
100112
Sequential Division
 Uses two registers: HI and LO
 Initialize: HI = Remainder = 0 and LO = Dividend
 Shift (HI, LO) LEFT by 1 bit (also Shift Quotient LEFT)
 Shift the remainder and dividend registers together LEFT
 Has the same net effect of shifting the divisor RIGHT
 Compute: Difference = Remainder – Divisor
 If (Difference ≥ 0) then
 Remainder = Difference
 Set Least significant Bit of Quotient
 Observation to Reduce Hardware:
 LO register can be also used to store the computed Quotient
Sequential Division Hardware
 Initialize:
 HI = 0, LO = Dividend
 Results:
 HI = Remainder
 LO = Quotient
Start
Difference?
2. HI = Remainder = Difference
Set least significant bit of LO
32nd
Repetition?
Done
< 0≥ 0
No
Yes
1. Shift (HI, LO) Left
Difference = HI – Divisor
shift left
Divisor
32-bit ALU
LO
32 bits
write
sub
32 bits
Difference
sign
set lsb
HI
32 bits
Control
2: Diff < 0 => Do Nothing
Unsigned Integer Division Example
 Example: 11102 / 00112 (4-bit dividend & divisor)
 Result Quotient = 01002 and Remainder = 00102
 4-bit registers for Remainder and Divisor (4-bit ALU)
2: Rem = Diff, set lsb of LO 1 0 0 10 0 0 0
2: Diff < 0 => Do Nothing
2: Diff < 0 => Do Nothing
2
0 0 0 0 1 1 1 0Initialize0
1
3
4
HI DifferenceLOIteration
0 0 1 1
Divisor
1 1 1 01: Shift Left, Diff = HI - Divisor 0 0 0 1 1 1 0 0 0 0 1 1
0 0 0 01: Shift Left, Diff = HI - Divisor 0 0 1 1 1 0 0 0 0 0 1 1
1 1 1 01: Shift Left, Diff = HI - Divisor 0 0 0 1 0 0 1 0 0 0 1 1
1 1 1 11: Shift Left, Diff = HI - Divisor 0 0 1 0 0 1 0 0 0 0 1 1
Signed Integer Division
 Simplest way is to remember the signs
 Convert the dividend and divisor to positive
 Obtain the 2's complement if they are negative
 Do the unsigned division
 Compute the signs of the quotient and remainder
 Quotient sign = Dividend sign XOR Divisor sign
 Remainder sign = Dividend sign
 Negate the quotient and remainder if their sign is negative
 Obtain the 2's complement to convert them to negative
Signed Integer Division Examples
1. Positive Dividend and Positive Divisor
 Example: +17 / +3 Quotient = +5 Remainder = +2
1. Positive Dividend and Negative Divisor
 Example: +17 / –3 Quotient = –5 Remainder = +2
1. Negative Dividend and Positive Divisor
 Example: –17 / +3 Quotient = –5 Remainder = –2
1. Negative Dividend and Negative Divisor
 Example: –17 / –3 Quotient = +5 Remainder = –2
The following equation must always hold:
Dividend = Quotient × Divisor + Remainder
Next . . .
 Unsigned Integer Multiplication
 Signed Integer Multiplication
 Faster Multiplication
 Integer Division
 Integer Multiplication and Division in MIPS
Integer Multiplication in MIPS
 Multiply instructions
 mult $s1,$s2 Signed multiplication
 multu $s1,$s2 Unsigned multiplication
 32-bit multiplication produces a 64-bit Product
 Separate pair of 32-bit registers
 HI = high-order 32-bit of product
 LO = low-order 32-bit of product
 MIPS also has a special mul instruction
 mul $s0,$s1,$s2 $s0 = $s1 × $s2
 Put low-order 32 bits into destination register
 HI & LO are undefined
Multiply
Divide
$0
HI LO
$1
..
$31
Integer Division in MIPS
 Divide instructions
 div $s1,$s2 Signed division
 divu $s1,$s2 Unsigned division
 Division produces quotient and remainder
 Separate pair of 32-bit registers
 HI = 32-bit remainder
 LO = 32-bit quotient
 If divisor is 0 then result is unpredictable
 Moving data from HI/LO to MIPS registers
 mfhi Rd (move from HI to Rd)
 mflo Rd (move from LO to Rd)
Multiply
Divide
$0
HI LO
$1
..
$31
Integer Multiply/Divide Instructions
Instruction Meaning Format
mult Rs, Rt Hi, Lo = Rs × Rt op6
= 0 Rs5
Rt5
0 0 0x18
multu Rs, Rt Hi, Lo = Rs × Rt op6
= 0 Rs5
Rt5
0 0 0x19
mul Rd, Rs, Rt Rd = Rs × Rt 0x1c Rs5
Rt5
Rd5
0 0x02
div Rs, Rt Hi, Lo = Rs / Rt op6
= 0 Rs5
Rt5
0 0 0x1a
divu Rs, Rt Hi, Lo = Rs / Rt op6
= 0 Rs5
Rt5
0 0 0x1b
mfhi Rd Rd = Hi op6
= 0 0 0 Rd5
0 0x10
mflo Rd Rd = Lo op6
= 0 0 0 Rd5
0 0x12
 Signed arithmetic: mult, div (Rs and Rt are signed)
 LO = 32-bit low-order and HI = 32-bit high-order of multiplication
 LO = 32-bit quotient and HI = 32-bit remainder of division
 Unsigned arithmetic: multu, divu (Rs and Rt are unsigned)
 NO arithmetic exception can occur
Integer to String Conversion
 Objective: convert an unsigned 32-bit integer to a string
 How to obtain the decimal digits of the number?
 Divide the number by 10, Remainder = decimal digit (0 to 9)
 Convert decimal digit into its ASCII representation ('0' to '9')
 Repeat the division until the quotient becomes zero
 Digits are computed backwards from least to most significant
 Example: convert 2037 to a string
 Divide 2037/10 quotient = 203 remainder = 7 char = '7'
 Divide 203/10 quotient = 20 remainder = 3 char = '3'
 Divide 20/10 quotient = 2 remainder = 0 char = '0'
 Divide 2/10 quotient = 0 remainder = 2 char = '2'
Integer to String Procedure
#--------------------------------------------------------------
# int2str: Converts an unsigned integer into a string
# Input: $a0 = unsigned integer
# In/Out: $a1 = address of string buffer (12 bytes)
#--------------------------------------------------------------
int2str:
move $t0, $a0 # $t0 = dividend = unsigned integer
li $t1, 10 # $t1 = divisor = 10
addiu $a1, $a1, 11 # start at end of string buffer
sb $zero, 0($a1) # store a NULL byte
convert:
divu $t0, $t1 # LO = quotient, HI = remainder
mflo $t0 # $t0 = quotient
mfhi $t2 # $t2 = remainder
addiu $t2, $t2, 0x30 # convert digit to a character
addiu $a1, $a1, -1 # point to previous byte
sb $t2, 0($a1) # store digit character
bnez $t0, convert # loop if quotient is not 0
jr $ra # return to caller

More Related Content

What's hot

Synchronous sequential Circuits
Synchronous sequential CircuitsSynchronous sequential Circuits
Synchronous sequential Circuits
Indira Priyadarshini
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil KawareProf. Swapnil V. Kaware
 
PAL And PLA ROM
PAL And PLA ROMPAL And PLA ROM
PAL And PLA ROM
RONAK SUTARIYA
 
D Flip Flop
D Flip Flop D Flip Flop
D Flip Flop
Pradhan Rishi Sharma
 
Low power high_speed
Low power high_speedLow power high_speed
Low power high_speednanipandu
 
flip flop circuits and its applications
flip flop circuits and its applicationsflip flop circuits and its applications
flip flop circuits and its applications
Gaditek
 
Memory & I/O interfacing
Memory & I/O  interfacingMemory & I/O  interfacing
Memory & I/O interfacing
deval patel
 
8251 USART
8251 USART8251 USART
8251 USART
ShivamSood22
 
Memory interfacing of microprocessor 8085
Memory interfacing of microprocessor 8085Memory interfacing of microprocessor 8085
Memory interfacing of microprocessor 8085
Nilesh Bhaskarrao Bahadure
 
halfadder & halfsubtractor using 4:1 MUX
halfadder & halfsubtractor using 4:1 MUXhalfadder & halfsubtractor using 4:1 MUX
halfadder & halfsubtractor using 4:1 MUX
U Reshmi
 
Counters
CountersCounters
Counters
Ravi Maurya
 
Counters &amp; time delay
Counters &amp; time delayCounters &amp; time delay
Counters &amp; time delay
Hemant Chetwani
 
Counters
CountersCounters
Counters
Abhilash Nair
 
Computer architecture instruction formats
Computer architecture instruction formatsComputer architecture instruction formats
Computer architecture instruction formats
Mazin Alwaaly
 
SHIFT REGISTERS
SHIFT REGISTERSSHIFT REGISTERS
SHIFT REGISTERS
kumari36
 
8251 USART
8251 USART8251 USART
8251 USART
coolsdhanesh
 
8051 serial communication
8051 serial communication8051 serial communication
8051 serial communicationasteriskbimal
 
Minimum mode and Maximum mode Configuration in 8086
Minimum mode and Maximum mode Configuration in 8086Minimum mode and Maximum mode Configuration in 8086
Minimum mode and Maximum mode Configuration in 8086
Jismy .K.Jose
 
Overview of Shift register and applications
Overview of Shift register and applicationsOverview of Shift register and applications
Overview of Shift register and applications
Karthik Kumar
 
Semiconductor memory
Semiconductor memorySemiconductor memory
Semiconductor memory
Then Murugeshwari
 

What's hot (20)

Synchronous sequential Circuits
Synchronous sequential CircuitsSynchronous sequential Circuits
Synchronous sequential Circuits
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware
 
PAL And PLA ROM
PAL And PLA ROMPAL And PLA ROM
PAL And PLA ROM
 
D Flip Flop
D Flip Flop D Flip Flop
D Flip Flop
 
Low power high_speed
Low power high_speedLow power high_speed
Low power high_speed
 
flip flop circuits and its applications
flip flop circuits and its applicationsflip flop circuits and its applications
flip flop circuits and its applications
 
Memory & I/O interfacing
Memory & I/O  interfacingMemory & I/O  interfacing
Memory & I/O interfacing
 
8251 USART
8251 USART8251 USART
8251 USART
 
Memory interfacing of microprocessor 8085
Memory interfacing of microprocessor 8085Memory interfacing of microprocessor 8085
Memory interfacing of microprocessor 8085
 
halfadder & halfsubtractor using 4:1 MUX
halfadder & halfsubtractor using 4:1 MUXhalfadder & halfsubtractor using 4:1 MUX
halfadder & halfsubtractor using 4:1 MUX
 
Counters
CountersCounters
Counters
 
Counters &amp; time delay
Counters &amp; time delayCounters &amp; time delay
Counters &amp; time delay
 
Counters
CountersCounters
Counters
 
Computer architecture instruction formats
Computer architecture instruction formatsComputer architecture instruction formats
Computer architecture instruction formats
 
SHIFT REGISTERS
SHIFT REGISTERSSHIFT REGISTERS
SHIFT REGISTERS
 
8251 USART
8251 USART8251 USART
8251 USART
 
8051 serial communication
8051 serial communication8051 serial communication
8051 serial communication
 
Minimum mode and Maximum mode Configuration in 8086
Minimum mode and Maximum mode Configuration in 8086Minimum mode and Maximum mode Configuration in 8086
Minimum mode and Maximum mode Configuration in 8086
 
Overview of Shift register and applications
Overview of Shift register and applicationsOverview of Shift register and applications
Overview of Shift register and applications
 
Semiconductor memory
Semiconductor memorySemiconductor memory
Semiconductor memory
 

Similar to 05 multiply divide

ece552_08_integer_multiply.ppt
ece552_08_integer_multiply.pptece552_08_integer_multiply.ppt
ece552_08_integer_multiply.ppt
ansariparveen06
 
ece552_08_integer_multiply.ppt
ece552_08_integer_multiply.pptece552_08_integer_multiply.ppt
ece552_08_integer_multiply.ppt
AshokRachapalli1
 
ece552_08_integer_multiply.ppt
ece552_08_integer_multiply.pptece552_08_integer_multiply.ppt
ece552_08_integer_multiply.ppt
AnalBhandari
 
Booth Multiplication.pptx
Booth Multiplication.pptxBooth Multiplication.pptx
Booth Multiplication.pptx
SamiulAlam34
 
Comp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptComp Arithmetic Basic.ppt
Comp Arithmetic Basic.ppt
skatiarrahaman
 
Binaty Arithmetic and Binary coding schemes
Binaty Arithmetic and Binary coding schemesBinaty Arithmetic and Binary coding schemes
Binaty Arithmetic and Binary coding schemes
Dr. Anita Goel
 
Encoding Schemes for Multipliers
Encoding Schemes for MultipliersEncoding Schemes for Multipliers
Encoding Schemes for Multipliers
Silicon Mentor
 
Unit ii ca--arithmetic
Unit ii ca--arithmeticUnit ii ca--arithmetic
Unit ii ca--arithmetic
Praba haran
 
CA UNIT II.pptx
CA UNIT II.pptxCA UNIT II.pptx
CA UNIT II.pptx
ssuser9dbd7e
 
Binary Arithmetic
Binary ArithmeticBinary Arithmetic
Binary Arithmeticgavhays
 
chapter 3.pptx
chapter 3.pptxchapter 3.pptx
chapter 3.pptx
MohebMalik1
 
3810-08.ppt
3810-08.ppt3810-08.ppt
3810-08.ppt
SrinivasanCSE
 
Integer Representation
Integer RepresentationInteger Representation
Integer Representationgavhays
 
Binary Arithmetic
Binary ArithmeticBinary Arithmetic
Binary Arithmetic
Meenakshi Paul
 
Number system
Number systemNumber system
Number system
Mantra VLSI
 
1's and 2's Complements
1's and 2's Complements 1's and 2's Complements
1's and 2's Complements
ZareenRauf1
 
Chapter 2_Number system (EEEg4302).pdf
Chapter 2_Number system (EEEg4302).pdfChapter 2_Number system (EEEg4302).pdf
Chapter 2_Number system (EEEg4302).pdf
TamiratDejene1
 
Integer represention
Integer representionInteger represention
Integer represention
Saif Ullah
 

Similar to 05 multiply divide (20)

2's complement
2's complement2's complement
2's complement
 
ece552_08_integer_multiply.ppt
ece552_08_integer_multiply.pptece552_08_integer_multiply.ppt
ece552_08_integer_multiply.ppt
 
ece552_08_integer_multiply.ppt
ece552_08_integer_multiply.pptece552_08_integer_multiply.ppt
ece552_08_integer_multiply.ppt
 
ece552_08_integer_multiply.ppt
ece552_08_integer_multiply.pptece552_08_integer_multiply.ppt
ece552_08_integer_multiply.ppt
 
09 Arithmetic
09  Arithmetic09  Arithmetic
09 Arithmetic
 
Booth Multiplication.pptx
Booth Multiplication.pptxBooth Multiplication.pptx
Booth Multiplication.pptx
 
Comp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptComp Arithmetic Basic.ppt
Comp Arithmetic Basic.ppt
 
Binaty Arithmetic and Binary coding schemes
Binaty Arithmetic and Binary coding schemesBinaty Arithmetic and Binary coding schemes
Binaty Arithmetic and Binary coding schemes
 
Encoding Schemes for Multipliers
Encoding Schemes for MultipliersEncoding Schemes for Multipliers
Encoding Schemes for Multipliers
 
Unit ii ca--arithmetic
Unit ii ca--arithmeticUnit ii ca--arithmetic
Unit ii ca--arithmetic
 
CA UNIT II.pptx
CA UNIT II.pptxCA UNIT II.pptx
CA UNIT II.pptx
 
Binary Arithmetic
Binary ArithmeticBinary Arithmetic
Binary Arithmetic
 
chapter 3.pptx
chapter 3.pptxchapter 3.pptx
chapter 3.pptx
 
3810-08.ppt
3810-08.ppt3810-08.ppt
3810-08.ppt
 
Integer Representation
Integer RepresentationInteger Representation
Integer Representation
 
Binary Arithmetic
Binary ArithmeticBinary Arithmetic
Binary Arithmetic
 
Number system
Number systemNumber system
Number system
 
1's and 2's Complements
1's and 2's Complements 1's and 2's Complements
1's and 2's Complements
 
Chapter 2_Number system (EEEg4302).pdf
Chapter 2_Number system (EEEg4302).pdfChapter 2_Number system (EEEg4302).pdf
Chapter 2_Number system (EEEg4302).pdf
 
Integer represention
Integer representionInteger represention
Integer represention
 

More from Piyush Rochwani

Unit 2
Unit 2Unit 2
Unit 3
Unit 3Unit 3
Biometrics based key generation
Biometrics based key generationBiometrics based key generation
Biometrics based key generation
Piyush Rochwani
 
Sequential and combinational alu
Sequential and combinational alu Sequential and combinational alu
Sequential and combinational alu Piyush Rochwani
 
Pipelining and co processor.
Pipelining and co processor.Pipelining and co processor.
Pipelining and co processor.Piyush Rochwani
 
Page replacement algorithms
Page replacement algorithmsPage replacement algorithms
Page replacement algorithmsPiyush Rochwani
 
Solid state solid state drives
Solid state solid state drivesSolid state solid state drives
Solid state solid state drivesPiyush Rochwani
 

More from Piyush Rochwani (20)

Unit 2
Unit 2Unit 2
Unit 2
 
Unit 3
Unit 3Unit 3
Unit 3
 
Biometrics based key generation
Biometrics based key generationBiometrics based key generation
Biometrics based key generation
 
Serial transmission
Serial transmissionSerial transmission
Serial transmission
 
Sequential and combinational alu
Sequential and combinational alu Sequential and combinational alu
Sequential and combinational alu
 
Memory virtualization
Memory virtualizationMemory virtualization
Memory virtualization
 
Risc
RiscRisc
Risc
 
Raid
Raid Raid
Raid
 
Pipelining and co processor.
Pipelining and co processor.Pipelining and co processor.
Pipelining and co processor.
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
 
Page replacement algorithms
Page replacement algorithmsPage replacement algorithms
Page replacement algorithms
 
8086 Microprocessor
8086 Microprocessor8086 Microprocessor
8086 Microprocessor
 
Dma
DmaDma
Dma
 
Control unit
Control unitControl unit
Control unit
 
Memory types
Memory typesMemory types
Memory types
 
Solid state solid state drives
Solid state solid state drivesSolid state solid state drives
Solid state solid state drives
 
Coa INTERUPT
Coa INTERUPTCoa INTERUPT
Coa INTERUPT
 
Cisc(a022& a023)
Cisc(a022& a023)Cisc(a022& a023)
Cisc(a022& a023)
 
06 floating point
06 floating point06 floating point
06 floating point
 
Air pollution in mumbai
Air pollution in mumbaiAir pollution in mumbai
Air pollution in mumbai
 

Recently uploaded

De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 

Recently uploaded (20)

De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 

05 multiply divide

  • 1.  Paper and Pencil Example: Multiplicand 11002 = 12 Multiplier × 11012 = 13 1100 0000 1100 1100 Product 100111002 = 156  m-bit multiplicand × n-bit multiplier = (m+n)-bit product  Accomplished via shifting and addition  Consumes more time and more chip area than addition Unsigned Integer Multiplication Binary multiplication is easy 0 × multiplicand = 0 1 × multiplicand = multiplicand
  • 2. Sequential Unsigned Multiplication  Initialize Product = 0  Check each bit of the Multiplier  If Multiplier bit = 1 then Product = Product + Multiplicand  Rather than shifting the multiplicand to the left Instead, Shift the Product to the Right Has the same net effect and produces the same result Minimizes the hardware resources  One cycle per iteration (for each bit of the Multiplier)  Addition and shifting can be done simultaneously
  • 3.  Initialize HI = 0  Initialize LO = Multiplier  Final Product = HI and LO registers  Repeat for each bit of Multiplier Sequential Multiplication Hardware = 0 Start LO[0]? HI = HI + Multiplicand 32nd Repetition? Done = 1 No Yes HI = 0, LO=Multiplier Shift (Carry, HI, LO) Right 1 bit32-bit ALU Control 64 bits 32 bits write add LO[0] Multiplicand shift right 32 bits HI LO 32 bits carry
  • 4. Sequential Multiplier Example  Consider: 11002 × 11012 , Product = 100111002  4-bit multiplicand and multiplier are used in this example  4-bit adder produces a 5-bit sum (with carry) 1 1 0 0Shift Right (Carry, HI, LO) by 1 bit 0 1 1 1 1 0 0 1 LO[0] = 0 => Do Nothing 1 1 0 0Shift Right (Carry, HI, LO) by 1 bit 0 0 1 1 0 0 1 1 1 1 0 0Shift Right (Carry, HI, LO) by 1 bit 0 1 1 0 0 1 1 0 1 1 0 0Shift Right (Carry, HI, LO) by 1 bit 1 0 0 1 1 1 0 0 2 1 1 0 0 0 0 0 0 1 1 0 1Initialize (HI = 0, LO = Multiplier)0 1 3 4 Multiplicand Product = HI, LOCarryIteration 0 1 1 0 0 1 1 0 1LO[0] = 1 => ADD + 0 1 1 1 1 0 0 1 1LO[0] = 1 => ADD + 1 0 0 1 1 1 0 0 1LO[0] = 1 => ADD +
  • 5. Next . . .  Unsigned Integer Multiplication  Signed Integer Multiplication  Faster Integer Multiplication  Integer Division  Integer Multiplication and Division in MIPS
  • 6. Signed Integer Multiplication  So far, we have dealt with unsigned integer multiplication  First Attempt:  Convert multiplier and multiplicand into positive numbers  If negative then obtain the 2's complement and remember the sign  Perform unsigned multiplication  Compute the sign of the product  If product sign < 0 then obtain the 2's complement of the product  Better Version:  Use the unsigned multiplication hardware  When shifting right, extend the sign of the product  If multiplier is negative, the last step should be a subtract
  • 7. Signed Multiplication (Pencil & Paper)  Case 1: Positive Multiplier Multiplicand 11002 = -4 Multiplier × 01012 = +5 11111100 111100 Product 111011002 = -20  Case 2: Negative Multiplier Multiplicand 11002 = -4 Multiplier × 11012 = -3 11111100 111100 00100 (2's complement of 1100) Product 000011002 = +12 Sign-extension Sign-extension
  • 8.  ALU produces 32-bit result + Sign bit  Check for overflow  No overflow  Extend sign-bit of result  Overflow  Invert sign bit Sequential Signed Multiplier = 0 Start LO[0]? First 31 iterations: HI = HI + Multiplicand Last iteration: HI = HI – Multiplicand 32nd Repetition? Done = 1 No Yes HI = 0, LO = Multiplier Shift Right (Sign, HI, LO) 1 bit32-bit ALU Control 64 bits 32 bits write add, sub LO[0] Multiplicand shift right 32 bits HI LO 32 bitssign
  • 9. Signed Multiplication Example  Consider: 11002 (-4) × 11012 (-3), Product = 000011002  Check for overflow: No overflow  Extend sign bit  Last iteration: add 2's complement of Multiplicand 1 1 0 0Shift (Sign, HI, LO) right 1 bit 1 1 0 1 1 0 0 1 LO[0] = 0 => Do Nothing 1 1 0 0Shift (Sign, HI, LO) right 1 bit 1 1 1 1 0 0 1 1 1 1 0 0Shift (Sign, HI, LO) right 1 bit 1 1 1 0 0 1 1 0 Shift (Sign, HI, LO) right 1 bit 0 0 0 0 1 1 0 0 2 1 1 0 0 0 0 0 0 1 1 0 1Initialize (HI = 0, LO = Multiplier)0 1 3 4 Multiplicand Product = HI, LOSignIteration 1 1 1 0 0 1 1 0 1LO[0] = 1 => ADD + 1 1 0 1 1 0 0 1 1LO[0] = 1 => ADD + 0 1 0 0 0 0 0 0 1 1 0 0 1LO[0] = 1 => SUB (ADD 2's compl) +
  • 10. Next . . .  Unsigned Integer Multiplication  Signed Integer Multiplication  Faster Integer Multiplication  Integer Division  Integer Multiplication and Division in MIPS
  • 11. Faster Integer Multiplier Uses Multiple Adders (Cost vs. Performance)  Can be pipelined
  • 12. Using Multiple Adders  32-bit adder for each bit of the multiplier  AND multiplicand with each bit of multiplier  Product = accumulated shifted sum  Each adder produces a 33-bit output  Most significant bit is a carry bit  Array multiplier can be optimized  Additions can be done in parallel  Multiple-level tree reduction to produce final product  Carry save adders reduce delays
  • 13. Carry Save Adders  Used when adding multiple numbers (as in multipliers)  All the bits of a carry-save adder work in parallel  The carry does not propagate as in a carry-propagate adder  This is why a carry-save is faster than a carry-propagate adder  A carry-save adder has 3 inputs and produces two outputs  It adds 3 numbers and produces partial sum and carry bits Carry-Propagate Adder + a0 b0 s0 + a1 b1 s1 + a31 b31 s31 . . .cout cin Carry-Save Adder . . .+ a31 b31 s'31c'31 c31 + a1 b1 s'1c'1 c1 + a0 b0 s'0c'0 c0
  • 14. Tree Multiplier - 1 of 2  Suppose we want to multiply two numbers A and B  Example on 4-bit numbers: A = a3 a2 a1 a0 and B = b3 b2 b1 b0  Step 1: AND (multiply) each bit of A with each bit of B  Requires n2 AND gates and produces n2 product bits  Position of aibj = (i+j). For example, Position of a2b3 = 2+3 = 5 a0b0a1b0a2b0a3b0 a0b1a1b1a2b1a3b1 a0b2a1b2a2b2a3b2 a0b3a1b3a2b3a3b3 A × B
  • 15. Tree Multiplier – 2 of 2 Step 2: Use carry save adders to add the partial products  Reduce the partial products to just two numbers Step 3: Add last two numbers using a carry-propagate adder P0 + a2b0 a1b1 ++ + a3b0 a2b1 a0b0 + a1b0 a0b1 + + +++ + a3b1 a2b2 + a3b2 a2b3 a3b3 a1b3 a0b3 a1b2 a0b2 P1P2P3P4P5P6P7 Carry Save Adder Carry Propagate Adder
  • 16. Next . . .  Unsigned Integer Multiplication  Signed Integer Multiplication  Faster Integer Multiplication  Integer Division  Integer Multiplication and Division in MIPS
  • 17. Try to see how big a number can be subtracted, creating a digit of the quotient on each attempt = 19 Quotient Divisor 10112 110110012 = 217 Dividend -1011 10 101 1010 10100 -1011 1001 10011 -1011 10002 = 8 Remainder Unsigned Division (Paper & Pencil) Binary division is accomplished via shifting and subtraction Dividend = Quotient × Divisor + Remainder 217 = 19 × 11 + 8 100112
  • 18. Sequential Division  Uses two registers: HI and LO  Initialize: HI = Remainder = 0 and LO = Dividend  Shift (HI, LO) LEFT by 1 bit (also Shift Quotient LEFT)  Shift the remainder and dividend registers together LEFT  Has the same net effect of shifting the divisor RIGHT  Compute: Difference = Remainder – Divisor  If (Difference ≥ 0) then  Remainder = Difference  Set Least significant Bit of Quotient  Observation to Reduce Hardware:  LO register can be also used to store the computed Quotient
  • 19. Sequential Division Hardware  Initialize:  HI = 0, LO = Dividend  Results:  HI = Remainder  LO = Quotient Start Difference? 2. HI = Remainder = Difference Set least significant bit of LO 32nd Repetition? Done < 0≥ 0 No Yes 1. Shift (HI, LO) Left Difference = HI – Divisor shift left Divisor 32-bit ALU LO 32 bits write sub 32 bits Difference sign set lsb HI 32 bits Control
  • 20. 2: Diff < 0 => Do Nothing Unsigned Integer Division Example  Example: 11102 / 00112 (4-bit dividend & divisor)  Result Quotient = 01002 and Remainder = 00102  4-bit registers for Remainder and Divisor (4-bit ALU) 2: Rem = Diff, set lsb of LO 1 0 0 10 0 0 0 2: Diff < 0 => Do Nothing 2: Diff < 0 => Do Nothing 2 0 0 0 0 1 1 1 0Initialize0 1 3 4 HI DifferenceLOIteration 0 0 1 1 Divisor 1 1 1 01: Shift Left, Diff = HI - Divisor 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 01: Shift Left, Diff = HI - Divisor 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 01: Shift Left, Diff = HI - Divisor 0 0 0 1 0 0 1 0 0 0 1 1 1 1 1 11: Shift Left, Diff = HI - Divisor 0 0 1 0 0 1 0 0 0 0 1 1
  • 21. Signed Integer Division  Simplest way is to remember the signs  Convert the dividend and divisor to positive  Obtain the 2's complement if they are negative  Do the unsigned division  Compute the signs of the quotient and remainder  Quotient sign = Dividend sign XOR Divisor sign  Remainder sign = Dividend sign  Negate the quotient and remainder if their sign is negative  Obtain the 2's complement to convert them to negative
  • 22. Signed Integer Division Examples 1. Positive Dividend and Positive Divisor  Example: +17 / +3 Quotient = +5 Remainder = +2 1. Positive Dividend and Negative Divisor  Example: +17 / –3 Quotient = –5 Remainder = +2 1. Negative Dividend and Positive Divisor  Example: –17 / +3 Quotient = –5 Remainder = –2 1. Negative Dividend and Negative Divisor  Example: –17 / –3 Quotient = +5 Remainder = –2 The following equation must always hold: Dividend = Quotient × Divisor + Remainder
  • 23. Next . . .  Unsigned Integer Multiplication  Signed Integer Multiplication  Faster Multiplication  Integer Division  Integer Multiplication and Division in MIPS
  • 24. Integer Multiplication in MIPS  Multiply instructions  mult $s1,$s2 Signed multiplication  multu $s1,$s2 Unsigned multiplication  32-bit multiplication produces a 64-bit Product  Separate pair of 32-bit registers  HI = high-order 32-bit of product  LO = low-order 32-bit of product  MIPS also has a special mul instruction  mul $s0,$s1,$s2 $s0 = $s1 × $s2  Put low-order 32 bits into destination register  HI & LO are undefined Multiply Divide $0 HI LO $1 .. $31
  • 25. Integer Division in MIPS  Divide instructions  div $s1,$s2 Signed division  divu $s1,$s2 Unsigned division  Division produces quotient and remainder  Separate pair of 32-bit registers  HI = 32-bit remainder  LO = 32-bit quotient  If divisor is 0 then result is unpredictable  Moving data from HI/LO to MIPS registers  mfhi Rd (move from HI to Rd)  mflo Rd (move from LO to Rd) Multiply Divide $0 HI LO $1 .. $31
  • 26. Integer Multiply/Divide Instructions Instruction Meaning Format mult Rs, Rt Hi, Lo = Rs × Rt op6 = 0 Rs5 Rt5 0 0 0x18 multu Rs, Rt Hi, Lo = Rs × Rt op6 = 0 Rs5 Rt5 0 0 0x19 mul Rd, Rs, Rt Rd = Rs × Rt 0x1c Rs5 Rt5 Rd5 0 0x02 div Rs, Rt Hi, Lo = Rs / Rt op6 = 0 Rs5 Rt5 0 0 0x1a divu Rs, Rt Hi, Lo = Rs / Rt op6 = 0 Rs5 Rt5 0 0 0x1b mfhi Rd Rd = Hi op6 = 0 0 0 Rd5 0 0x10 mflo Rd Rd = Lo op6 = 0 0 0 Rd5 0 0x12  Signed arithmetic: mult, div (Rs and Rt are signed)  LO = 32-bit low-order and HI = 32-bit high-order of multiplication  LO = 32-bit quotient and HI = 32-bit remainder of division  Unsigned arithmetic: multu, divu (Rs and Rt are unsigned)  NO arithmetic exception can occur
  • 27. Integer to String Conversion  Objective: convert an unsigned 32-bit integer to a string  How to obtain the decimal digits of the number?  Divide the number by 10, Remainder = decimal digit (0 to 9)  Convert decimal digit into its ASCII representation ('0' to '9')  Repeat the division until the quotient becomes zero  Digits are computed backwards from least to most significant  Example: convert 2037 to a string  Divide 2037/10 quotient = 203 remainder = 7 char = '7'  Divide 203/10 quotient = 20 remainder = 3 char = '3'  Divide 20/10 quotient = 2 remainder = 0 char = '0'  Divide 2/10 quotient = 0 remainder = 2 char = '2'
  • 28. Integer to String Procedure #-------------------------------------------------------------- # int2str: Converts an unsigned integer into a string # Input: $a0 = unsigned integer # In/Out: $a1 = address of string buffer (12 bytes) #-------------------------------------------------------------- int2str: move $t0, $a0 # $t0 = dividend = unsigned integer li $t1, 10 # $t1 = divisor = 10 addiu $a1, $a1, 11 # start at end of string buffer sb $zero, 0($a1) # store a NULL byte convert: divu $t0, $t1 # LO = quotient, HI = remainder mflo $t0 # $t0 = quotient mfhi $t2 # $t2 = remainder addiu $t2, $t2, 0x30 # convert digit to a character addiu $a1, $a1, -1 # point to previous byte sb $t2, 0($a1) # store digit character bnez $t0, convert # loop if quotient is not 0 jr $ra # return to caller