Docsity

Multiplication and Division-Assembly Language Programming-Lecture Slides, Slides of Assembly Language Programming

Partial preview of the text

Download Multiplication and Division-Assembly Language Programming-Lecture Slides and more Slides Assembly Language Programming in PDF only on Docsity!

Chapter 4.2 - Mult, Div, Float

1

OverviewMultiplying Hardware & SoftwareDividing Hardware & SoftwareIntroduction to Floating PointDoing Floating Point ArithmeticMIPS Floating Point InstructionsThe Dangers of Floating Point

Chapter 4.2 - Mult, Div, Float

2

MULTIPLY • Paper and pencil example (unsigned):^1000 Multiplicand

U 1001 Multiplier^

M (^100000000000)  100001001000 Product^

P

-^ Binary multiplication is easy:^ –^ P == 0^ ^ place all 0's i^

(0^ ^ multiplicand)

-^ P == 1^ ^ place a copy of i^

U^ (1^ ^ multiplicand)

  • Shift the multiplicand left before adding to product–^ Could we multiply via

add,^ sll?

Chapter 4.2 - Mult, Div, Float

4

Multiply in MIPS• Can multiply variable by any constant using MIPS

sll^ and^ add instructions:^ i'^ =^ i^

*****^10 ;^ /*^ assume

i:^ $s0^ */ sll^ $t0,^ $s0,^3

(^3) # i * 2 add^ $t1,^ $zero,^

$t sll^ $t0,^ $s0,^1

(^1) # i * 2 add^ $s0,^ $t1,^ $t0 • MIPS multiply instructions:^ mult

,^ multu

-^ mult^ $t0,^ $t1^ – puts 64-bit product in pair of new registers

hi,^ lo; copy to $n by mfhi,^ mflo – 32-bit integer result in register

lo

M I P S

Chapter 4.2 - Mult, Div, Float

5

Is Shift Right Arith.

Divide by 2?

-^ Shifting right by^ n

bits would seem to be the same as dividing by 2

n

-^ Problem is signed integers^ – Zero fill (srl) is wrong for negative numbers•^ Shift Right Arithmetic (

sra ); sign extends (replicates sign bit); but
does it work? • Divide -5 by 4 via^ sra 2

; result should be -11111 1111 1111 1111 1111 1111 1111 10111111 1111 1111 1111 1111 1111 1111 1110

-^ = -2, not -1; Off by 1, so

doesn’t work

•^ Is it always off by 1??

Chapter 4.2 - Mult, Div, Float

7

Multiply AlgorithmVersion 2

1. TestMultiplier^0 2. Shift the Product register right 1 bit. 3. Shift the Multiplier register right 1 bit.No: < 32 repetitionsYes: 32 repetitionsDone Multiplier^ = 0^0 Multiplier0 = 1 1a. Add multiplicand to the left half of product &place the result in the left half of Product register

Start 32ndrepetition? Product^ Multiplier Multiplicand^ 0000 0000^0011 0010 1:^ 0010 0000^0011 0010 2:^ 0001 0000^0011 0010 3:^ 0001 0000^0001 0010 1:^ 0011 0000^0001 0010 2:^ 0001 1000^0001 0010 3:^ 0001 1000^0000 0010 1:^ 0001 1000^0000 0010 2:^ 0000 1100^0000 0010 3:^ 0000 1100^0000 0010 1:^ 0000 1100^0000 0010 2:^ 0000 0110^0000 0010 3:^ 0000 0110^0000 0010 0000 0110^0000

Chapter 4.2 - Mult, Div, Float

8

Divide: Paper & Pencil^1001 QuotientDivisor 1000 1001010 Dividend–1000^101011010 –1000^10 Remainder

(or^ Modulo^ result) See^ how^ big^ a^ number

can^ be^ subtracted,

creating quotient^ bit^ on^

each^ step Binary => 1 * divisor or 0 * divisor Dividend^ =^ Quotient

^ Divisor^ +^ Remainder

|^ Dividend^ |^ =^ |

Quotient^ |^ +^ |^

Divisor^ |

Chapter 4.2 - Mult, Div, Float

10

Divide Algorithm^ Quotient = 0; 32 bit

divisor^ starts in left½ of Divisor reg. and it is shiftedright 1 on each step; Remainder = dividend ; If Remainder < 0, we need to add Divisorback to dividend ; else 1 is generatedfor Quotient;Shift Divisor right 1 bit;Repeat 33 times Start: Place Dividend in Remainder1. Subtract the Divisor register from theRemainder register, and place the resultin the Remainder register.Remainder < 0Remainder0 TestRemainder 2b. Restore the original value by adding the2a. Shift theDivisor register to the Remainder register, &Quotient registerplace the sum in the Remainder register. Alsoto the left settingshift the Quotient register to the left, settingthe new rightmostthe new least significant bit to 0.bit to 1.3. Shift the Divisor register right 1 bit. n +1No: <^ n +1 repetitionsrepetition?Yes: n+1 repetitions ( n^ = 4 here)Done Let $s0 = Dividend,$s1 = Divisor,$s2 = Remainder,$s3 = Quotient,$s4 = RepetitionsStart:move^ $s2, $s0Loop:sub^ $s2, $s2, $s

Step 1bltz $s2, Label2bsll $s3, $s3, 1 # Step 2aori $s3, $s3, 1j Label

Label2b:add^ $s2, $s2, $s

Step 2bsll $s3, $s3, 1

Label3:slr^ $s1, $s1, 1

Step 3addi $s4, $s4, -1^ # Reps.Bgtz $s4, Loop

Chapter 4.2 - Mult, Div, Float

11

Divide Algorithm Example Remainder Quotient^ Divisor 0000 0111 00000 0010

0000 1:^1110

00000 0010 0000 2:^0000

00000 0010 0000 3:^0000

00000 0001 0000 1:^1111

00000 0001 0000 2:^0000

00000 0001 0000 3:^0000

00000 0000 1000 1:^1111

00000 0000 1000 2:^0000

00000 0000 1000 3:^0000

00000 0000 0100 1:^0000

00000 0000 0100 2:^0000

00001 0000 0100 3:^0000

00001 0000 0010 1:^0000

00001 0000 0010 2:^0000

00011 0000 0010 3:^0000

00011 0000 0010

Answer:Quotient = 3Remainder = 1 docsity.com

Chapter 4.2 - Mult, Div, Float

13

What is in a number?• What can be represented in N bits?N^ • Unsigned 0 to^2 - 1 N-1^ N-1^ • 2s Complement^ - 2to^2 - 1 N-1N-1 • 1s Complement^ -2+1 to^2 -1 -M^ N - M - 1 • Excess M 2 to^2 • (E = e + M)N/4^ • BCD 0 to^10 - 1 • But, what about? • very large numbers?9,349,398,989,787,762,244,859,087,678 • very small number?^ 0.0000000000000000000000045691 • rationals 2/3 • irrationals^2 • transcendentals^ e,^ 

Chapter 4.2 - Mult, Div, Float

14

Recall Scientific Notation^ 6.02 x 10

exponent 23 radix (base)

(sign, magnitude) Mantissa^ decimal point

(sign, magnitude)

-^ Normal form:no leading 0s (digit 1 to left of decimal point) •^ Alternatives to representing 1/1,000,000,000^ Normalized:^

-91.0 × 10-8-10Not normalized: 0.1 × 10, 10.0 × 10

Chapter 4.2 - Mult, Div, Float

16

Single Precision FP Representation• Start with a single word (32-bits)

0 3130 23 22S^ Exponent

Significand 1 bit^ 8 bits^

23 bits

S^ ° Meaning: (-1)x Mantissa x 2

E

°^ Can now represent numbers as small as-38^ 2.0 x 10^ to as large as 2.0 x 10

(^38) °^ Relationship between Mantissa and Significand bits?Between E and Exponent? °^ In C type^ float

Chapter 4.2 - Mult, Div, Float

17

Floating Point Number Representation•^ What if result too large? (> 2.0x

Overflow!Overflow^ ^ Exponent larger than can be represented in8-bit Exponent field• What if result too small? (>0, < 2.0x
-38^ )
Underflow!Underflow^ ^ Negative Exponent too small• How to reduce chances of overflow or underflow?

Chapter 4.2 - Mult, Div, Float

19

MIPS follows IEEE 754 F.P. Standard• To pack more bits, make leading 1 of mantissa implicit fornormalized numbers^ 1 + 23 bits single, 1 + 52 bits double0 has no leading 1, so reserve exponent value 0 just for number 0.0Meaning: (almost correct) S^^ (-1) ×^ (1 + Significand)

Exponent × 2 , where 0 < Significand < 1 • If label significand bits left-to-right as s

, s, s, ... then value is: 123 S^ -1(-1) ×^ (1+(s ×^2 ) + (s^1

-2-3 × 2 ) + (s ×^2 ) +^ ) 22

Exponent × 2

Chapter 4.2 - Mult, Div, Float

20

Representing Exponent• Want to compare Fl. Pt. numbers as if they were integers, to helpin sorting Sign first part of numberExponent next, so bigger exponent

^ bigger number (^20 10) 1.1 × 10 > 1.9^ ×^10 • What About Negative Exponents?-1^ Use 2’s comp? 1.0 ×^2 vs. 1.0^ × +1^2 (1/2 v. 2)

0 1111 1111^ 000 0000 0000 0000 0000 00001/2 0 0000 0001^ 000 0000 0000 0000 0000 0000 2 This notation using integer compare of1/2^ vs. 2 makes 1/2 > 2! Doesn’t work!