Upload
vaughn
View
31
Download
0
Embed Size (px)
DESCRIPTION
Computer Engineering Department. Computer Arithmetic Lecture 10. 000000 $s2 $s3 00000 00000 27. 000000 $s2 $s3 00000 00000 26. op rs rt rd shamt funct. MIPS Divide Instruction. - PowerPoint PPT Presentation
Citation preview
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 1
Computer ArithmeticLecture 10
Computer Engineering Department
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 2
div $s2, $s3MIPS Divide Instruction
000000 $s2 $s3 00000 00000 27
op rs rt rd shamt funct
000000 $s2 $s3 00000 00000 26
The division quotient is placed in processor dedicated register lo and the remainder is placed in processor register hi
div uses signed integers and result is a signed 64-bit number. Overflow and division by 0 are checked in software
MIPS uses divu for unsigned divisions
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 3
10 101 1010 –1000
–1000
Division: Paper & Pencil
Divisor1000ten 1001010ten Dividend
A number can be subtracted, creating quotient bit on each step
Binary => 1 * divisor or 0 * divisor
Dividend = Quotient x Divisor + Remainder=> | Dividend | = | Quotient | + | Divisor |
We assume for now unsigned 32-bit integers.
3 versions of divide, successive refinement
1001ten Quotient
10 Remainder (or Modulo result)
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 4
Division Hardware (Version 1) 64-bit Divisor register, 64-bit ALU, 64-bit Remainder
register, 32-bit Quotient register
Initially holds dividend
Initially holds 0sInitially holds divisor
If Reminder63 = 1, Quotient0=0 Reminder63 = 0, Quotient0=1
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 5
Divide Algorithm Version 1
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 6
Observations on Divide Version 1
1/2 bits in divisor always 0=> 1/2 of 64-bit adder is wasted => 1/2 of divisor is wasted
Instead of shifting divisor to right, shift the remainder to left?
1st step cannot produce a 1 in quotient bit (otherwise too big for the register) => switch order to shift first and then subtract, can save 1 iteration
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 7
Division Hardware (Version 2) 32-bit Divisor register, 32-bit ALU, 64-bit
Remainder register, 32-bit Quotient register
If Reminder63 = 1, Quotient0=0 Reminder63 = 0, Quotient0=1
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 8
Observations on Divide Version 2 We can eliminate Quotient register by combining with
Remainder as it is shifted left Start by shifting the Remainder left as before. Thereafter loop contains only two steps because the
shifting of the Remainder register shifts both the remainder in the left half and the quotient in the right half
The consequence of combining the two registers together and the new order of the operations in the loop is that the remainder will be shifted left one time too many.
Thus the final correction step must shift back only the remainder in the left half of the register
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 9
Division Hardware (Version 3)
32-bit Divisor reg, 32 -bit ALU, 64-bit Remainder reg, (0-bit Quotient reg)
At the end right halfholds quotient
At the start dividend is here
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 10
Divide Algorithm Version 3
Iteration Divisor Remainder reg oper?
0. 0010 0000 0111 in. val0a 0010 0000 1110 shft lf 1 1a 0010 1110 1110 Rem=Rem-Div1b 0010 0000 1110 Rem=Rem+Div 0001 1100 sll Rem, R0=02a 0010 1111 1100 Rem=Rem-Div2b 0010 0001 1100 Rem=Rem+Div 0011 1000 sll Rem, R0=03a 0010 0001 1000 Rem=Rem-Div3b 0010 0011 0001 sll Rem, R0=1
4a 0010 0001 0001 Rem=Rem-Div4b 0010 0010 0011 sll Rem, R0=1Shift left halfOf Rem right 1 0001 0011
Divide 0000 0111 by 0010 (2s 1110)
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 11
Observations on Divide Version 3 Same Hardware as Multiply: just need ALU to add or subtract,
and 64-bit register to shift left or shift right Hi and Lo registers in MIPS combine to act as 64-bit register
for multiply and divide Signed Divides: Simplest is to remember signs, make positive,
and complement quotient and remainder if necessary Note: Dividend and Remainder must have same sign Note: Quotient negated if Divisor sign & Dividend sign
disagreee.g., –7 ÷ 2 = –3, remainder = –1
And 7 ÷ (– 2)= – 3, remainder = 1 Possible for quotient to be too large: if divide 64-bit integer by
1, quotient is 64 bits (“called saturation”)
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 12
Review: MIPS Instructions, so farCategory Instruction Op Code Example Meaning
Arithmetic
(R & I format)
add 0 and 32 add $s1, $s2, $s3 $s1 = $s2 + $s3
add unsigned 0 and 33 addu $s1, $s2, $s3 $s1 = $s2 + $s3
subtract 0 and 34 sub $s1, $s2, $s3 $s1 = $s2 - $s3
subt unsigned 0 and 35 subu $s1, $s2, $s3 $s1 = $s2 - $s3
add immediate 8 addi $s1, $s2, 6 $s1 = $s2 + 6
add immediate unsigned
9 addiu $s1, $s2, 6 $s1 = $s2 + 6
multiply 0 and 24 mult $s1, $s2 hi || lo = $s1 * $s2
multiply unsigned
0 and 25 multu $s1, $s2 hi || lo = $s1 * $s2
divide 0 and 26 div $s1, $s2 lo = $s1/$s2, remainder in hi
divide unsigned
0 and 27 divu $s1, $s2 lo = $s1/$s2, remainder in hi
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 13
Review: MIPS ISA, continuedCategory Instr Op Code Example Meaning
Shift
(R format)
sll 0 and 0 sll $s1, $s2, 4 $s1 = $s2 << 4
srl 0 and 2 srl $s1, $s2, 4 $s1 = $s2 >> 4
sra 0 and 3 sra $s1, $s2, 4 $s1 = $s2 >> 4
Data Transfer
(I format)
load word 35 lw $s1, 24($s2) $s1 = Memory($s2+24)
store word 43 sw $s1, 24($s2) Memory($s2+24) = $s1
load byte 32 lb $s1, 25($s2) $s1 = Memory($s2+25)
load byte unsigned
36 lbu $s1, 25($s2) $s1 = Memory($s2+25)
store byte 40 sb $s1, 25($s2) Memory($s2+25) = $s1
load upper imm 15 lui $s1, 6 $s1 = 6 * 216
move from hi 0 and 16 mfhi $s1 $s1 = hi
move to hi 0 and 17 mthi $s1 hi = $s1
move from lo 0 and 18 mflo $s1 $s1 = lo
move to lo 0 and 19 mtlo $s1 lo = $s1
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 14
Review: MIPS ISA- continuedCategory Instr Op Code Example Meaning
Cond. Branch
(I & R format)
br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L
br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L
set on less than 0 and 42 slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else
$s1=0
set on less than unsigned
0 and 43 sltu $s1, $s2, $s3 if ($s2<$s3) $s1=1 else
$s1=0
set on less than immediate
10 slti $s1, $s2, 6 if ($s2<6) $s1=1 else
$s1=0
set on less than imm. unsigned
11 sltiu $s1, $s2, 6 if ($s2<6) $s1=1 else
$s1=0
Uncond. Jump (J & R format)
jump 2 j 2500 go to 10000
jump and link 3 jal 2500 go to 10000; $ra=PC+4
jump register 0 and 8 jr $s1 go to $s1
jump and link reg 0 and 9 jalr $s1, $s2 go to $s1, $s2=PC+4
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 15
Floating-Point What can be represented in N bits? Unsigned 0 to 2N
2s Complement - 2N-1 to 2N-1 - 1 With MIPS singed integers the 32-bit architecture allows
us to represent numbers in the range ±2.15Χ109
But, what about very large numbers?9,349,398,989,787,762,244,859,087,678
What about very small numbers?0.0000000000000000000000045691
Floating point representation allows much larger range at the expense of accuracy
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 16
Scientific Notation
1.02 x 10 -1.673 x 1023 -24
radix (base)
Normalized notation – single number to theleft of decimal point (no leading 0s)
significand
Sign, magnitude
Exponent - how many digits the decimal point is moved to left to get to 1
Sign, magnitude
±1.xxxxx….. 2 yyyyyyy
radix (base 2)
Binary notation (binary floating point)
Number of xs determines accuracy
Number of ys determines range
Decimal notation
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 17
MIPS Register bit allocation Representation of floating point means that the binary point “floats” - to get a non-0 bit before it. The binary point is not fixed. Since number of bits in register is fixed - we need to compromise
1 8 bits 23 bits
sign
Signed exponentmantissa:sign + magnitude, normalizedbinary significand
S E M
Representation of 2.0tenx10-38 to 2.0tenx1038. In double-precision two registers are used. This increases the range to 2.0ten x10-308 to 2.0tenx10308. Significand now has 52 bits.
1 11 bits 20 bits
sign
Signed exponent
S E M
M
32 bits
When exponent is too large – or too small – an exception Overflow, or underflow
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 18
IEEE 754 Floating Point StandardRepresentation of floating point numbers in IEEE 754 standard - assures uniformity across computer implementations.
The 1 in significand is implicit - 0ten is given as a reserved representation 00…..000two
The rest of the numbers are N = (-1) 2 (1.M)
S E-127
Placing the sign bit and exponent first makes easier integer comparisons for sorting
Using an exponent bias allows exponent to be unsigned, smallest being 00…000two largest being 11…111two. Makes comparisons easier
Double precision bias is 1023ten. Underflow and overflow can still occur
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 19
IEEE 754 - continued If the 23 significand bits are numbered from left-to-right then the floating point number represented by these bits is
N = (-1)(1+s1•2-1+s2•2-2+ .. +s23•2-23)2(E-bias)S
So the register containing the bits
1 1000 0011 111000….. 0
represents
N = (-1) (1+2-1+2-2+2-3)2(27+21+20-127)
N = -1.875 2(131-127)
N = -1.87524 = -1.875 16= -30
27/02/2009 CA&O Lecture 10 By Engr. Umbreen sabir 20
Exercise Show the IEEE 754 representation of 10ten in single and double
precision
The sign bit is 0, the exponent is 3+127 = 130= 1000 0010two
10ten = 1010two = 1.01 23 in normalized notation
0 1000 0010 01000….. 0
23 bits sp
20 bits
0 100 0000 0010 01000….. 0 dp
0 0000 00 00000….. 0 32 bits
In double precision the exponent is 3+1023= 1026= 100 0000 0010two