Professional Documents
Culture Documents
CSE341 Lecture Notes Fall 2009 Arithmetic For Computers: Ex: Write - 38 in 32 Bits
CSE341 Lecture Notes Fall 2009 Arithmetic For Computers: Ex: Write - 38 in 32 Bits
CSE341 Lecture Notes Fall 2009 Arithmetic For Computers: Ex: Write - 38 in 32 Bits
Lets go to 8 bits
1
11111 111 Borrow
1 Carry Row
0 = 0000 0000
3 = 0000 0011
0000 0000 0000 0000 0000 0000 0100 0101 (69)
--------------
1111 1101
0000 0000 0000 0000 0000 0000 0000 1100 (12)
+----------------------------------------
Lets calculate -10
11111 110 Borrow
0000 0000 0000 0000 0000 0000 0101 0001 (81)
0 = 0000 0000
10= 0000 1010
---------------
Ex: find 69-12 do subtraction via addition 69+(-12) -10= 1111 0110
1111 1111 1111 1111 1111 1111 11 1 You can do the same with decimals
0000 0000 0000 0000 0000 0000 0100 0101 (69) -349
0 = 0000 0000
1111 1111 1111 1111 1111 1111 1111 0100 (-12) 349= 0000 0349
+------------------------------------------- -------------------
9999 9651
0000 0000 0000 0000 0000 0000 0011 1001 (57) 2
CSE341 Lecture Notes Fall 2009
Arithmetic for Computers
Never happens if signs are different because with different Overflow is ignored in unsigned numbers!
signs the result is always less than both operands.
Solution to understand overflow is to have a signal or
In subtraction: Overflow can occur if signs are different. exception. (or interrupt)
Never happens if signs are the same. Think of subtraction Add (add), add immediate (addi), and subtract
as addition of negated subtracted (sub) cause exceptions on overflow
x-y = x+ (-y).
Add unsigned (addu), add immediate unsigned
How do we detect it? (addiu)m and subtract unsigned (subu) do NOT
cause exceptions on overflow
If you add two positive numbers and the result is negative
C compiler simply dont care about overflow.
A carry out occurred into the sign bit Everything will be generated using addu, addiu, subu
Overflow occurs in subtraction when we subtract a MIPS detects overflow with an exception.
addu $t0, $t1, $t2 #$t0 = sum but don' trap It is easy to detect overflow for unsigned numbers but
usually ignored in address arithmetic
xor $t3, $t1, $t2 # t3 = 1 if $t1
and $t2 different Two's complement overflow detection is done by
slt $t3, $t3, $zero # if t3 is exceptions. EPC etc.
negative signs differ
bne $t3, $zero, No_oflw # if positive go to Arithmetic on bytes and halfwords for lbu, lhu is done
no_oflw again with add, sub, mult, div, then use AND to mask
bits
xor $t3, $t0, $t1 # signs with sum
match?11
slt $t4, $t3, $zero # t3 is negative ,
Modern computers have some multimedia support
dont match, t4 = 1 because it is important to have saturated sums,
Bne $t4, $zero, Overflow # sum is subtracts in parallel .
different than ops
E.g. SIMD single instruction multiple data. MMX
# 2'comp -1 : 2^32-$t1-1
The resulting values are saturated for multimedia
6
CSE341 Lecture Notes Fall 2009
Arithmetic for Computers
0110 =6
0000 0101 =5
Keep Multiplicand fix, Move the product
add 0110 0101 instead. Save 64-bit alu and 64 bit
multiplicands registers.
0011 0010
add 0011 0010 Do it in parallel.
add 0111 1001 just use the lower 32 bits of the product
register for multiplier.
0011 1100
add 0011 1100 7
0001 1110 = 30
Equivalent C Code for multiplication
c=0;
for(i=1;i<n;i=i+1)
{
if(b&0x01!=0)
c=c+a;
b=b>>1;
a=a<<1;
}
8x3 = 8+8+8
8
CSE341 Lecture Notes Fall 2009
Arithmetic for Computers
Signed Multiplication
2nd way, remember sign extension
Two ways 1 way
st
Signed numbers can be infinitely sign-
Negate the negative number using the two's extended
complement.
Find the product and then find the negative. Positive numbers prefixed by an infinite
number of 0s
If two negatives, dont negate the result
Sign-extend a and b to 2n digits for
If one negative negate the result multiplication. Must loop 2n times as a
result But can still exit early when b==0
Ex: if there is no overflow just multiply
1101 = -3 Ex: extend sign bits
0010 = 2 1111 1000 = -8
X---------------------- 1111 1001 = -7
0000 X----------------------
1101 1111111 1000
+---------------------- 00000 000
11010 =? -6 000000 00
1111100 0
Or 11111000
0011 = 3 take 2'comp(-3)
2'comp 11111000
0010 = 2 11111000
X---------------------- +----------------------
0000 ...0011 1000= 56
0011
+----------------------
00110 = 6
11010 =-6 Take 2'comp
9
CSE341 Lecture Notes Fall 2009
Arithmetic for Computers
MIPS multiplication
Can test HI value to see if product
Multiply (mult and multu) produces a 64 bit overflows 32 bits
double precision product
mult $t2, $t1
mult $s0, $s1# hi||lo = $s0 * $s1 Mfhi $t0
Beq $t0,$zero,no overflow
Multu unsigned.
no overflow:
Low-order word of the product is left in mflo $s1
processor register lo
and the high-order word is left in register hi mult $t2, $t1
Mfhi $t0
Instructions mfhi rd and mflo rd are mflo $t4
provided to move the product to (user sra $t4, $t4,31
accessible) registers in the register file Beq $t0, $t4,no overflow
#do your overflow handling
mfhi $s0 moves the result in hi to $s0. no overflow:
mflo dst
Mflo $t0 moves the result in low to $t0.
mul rd, rs, rt
Is pseudo instruction does
R-type ,mult [hi|lo] = R[rs]*R[rt]
Mul $s2, $S0, $S1
000000 10000 10001 00000 00000 01 1000
Mult $s0,$s1 # multiply s0,s1
6 bits 5bits 5bits 5bits 5bits 6bits Mflo $s2 # put the lower
32 bits of the result to $s2
Long division approach remainder = dividend-(quotientxdivisior)
If divisor ≤ dividend bits
Subtract dividend = divident-divisor -7/2: Quotient = -3, remainder = -1
1 bit in quotient,
Shift divident right It is not Quotient:-4+ Remainder:1 because
Else absolute value of the quotient should not change
0 bit in quotient, bring down next with respect to sign of the divisors,
dividend bit
To understand if divisor<= dividend, subtract
Initially dividend
12
CSE341 Lecture Notes Fall 2009
Arithmetic for Computers
13
CSE341 Lecture Notes Fall 2009
Arithmetic for Computers
Equivalent c code
d=0;
b=b<<n;
for(i=0;i<n;i++)
{
b=b>>1;
d=d<<1;
if(a>=b)
{
a=a-b;
d=d+1;
Initially dividend }
}
14
CSE341 Lecture Notes Fall 2009
Arithmetic for Computers
0010 =2
0000 0110 =6
0000 1100
sub 1110 1100
0000 1100
Similar to multiplication, 0001 1000
use the same sub 1111 1100
hardware 0001 1000
0011 0000
sub 0001 0001
Can’t use parallel 0010 0010
hardware as in multiplier,sub 0000 0011
because
Conditional subtraction 15
CSE341 Lecture Notes Fall 2009
Arithmetic for Computers
MIPS Division
Instructions
div rs, rt
divu rs, rt
No overflow or divide-by-0
checking
16
CSE341 Lecture Notes Fall 2009
Floating Point
Floating point
Why have a standard?
We need to support fractions / non-integral numbers: Simplifies exchange of data
reals. Simplifies arithmetic operations
Increases accuracy by not wasting digits
3.14151926
0.000000000000...000166 or 1.66x10^-27 Decimal point-> binary point
5,155,760000 or 5.15576x10^9
These numbers are called floating point because the Fraction ← → Exponent
place of the point is not fixed.
Precision ← → Range
Normalized: non-zero single digit, helps standardizing
the representation. How you would do it in 32 bits
We can show binary numbers as well in scientific 1 bit for sign
notation X bits for exponent
Y bits for fraction
1.01001 x 21011 → exponential
|->fraction
+/- Exponent Fraction 17
CSE341 Lecture Notes Fall 2009
Floating Point
Representation cont...
MIPS floating point number Double precision takes 2 words!
11-bits exponent
s Exponent Fraction
52 bit fraction: Greater precision!
31 3 23 22 0
0 double:2.0 x 10-308, single:2.0 x10-38
double:2.0 x 10308, single:2.0 x1038
(-1)s x (1+ F) x 2E
s Exponent 11bits Fraction 20+32 = 52 bits
0<Fraction<1
31 3
0
20 19 0
1<Significant< 2
31 Fraction 0
18
CSE341 Lecture Notes Fall 2009
Floating Point
Encoding ordered for the sorting
Encode IEEE uses a bias For a<>b check
1. sign
(-1)s x (1+ F) x 2Power-Bias 2. exponent
Decode 3. fraction
(-1)s x (1+ (s1x2-1)+(s2x2-2)+...+(s20x2-20) x
2Exponent-Bias Can use integer ops.
IEEE 754 floating standard uses special However negative exponent looks bigger than
sequences for unusual events. positive will cause a problem.
0, infinity, not a number NaN 0/0 To make everything ordered
IEEE uses a bias
22
CSE341 Lecture Notes Fall 2009
Floating Point
FIGURE 3.15 Floating-point addition. The normal path is to execute steps 3 and 4 once, but if rounding causes the
sum to be unnormalized, we must repeat step 3. Copyright © 2009 Elsevier, Inc. All rights reserved.
24
FIGURE 3.16 Block diagram of an arithmetic unit dedicated to floating-point addition. The steps of Figure 3.15
correspond to each block, from top to bottom. First, the exponent of one operand is subtracted from the other using the
small ALU to determine which is larger and by how much. This difference controls the three multiplexors; from left to
right, they select the larger exponent, the significand of the smaller number, and the significand of the larger number.
The smaller significand is shifted right, and then the significands are added together using the big ALU. The
normalization step then shifts the sum left or right and increments or decrements the exponent. Rounding then creates
the final result, which may require normalizing again to produce the final result. Copyright © 2009 Elsevier, Inc. All rights
reserved.
SKIP THIS 25
Example : multiply X and Y
Example: Add 8 bit floating points
sign exponent fraction
Variable sign exponent X 0 1001 010
fraction Y 0 0111 110
X 0
1001 110 X is 1.010 x 22 ,
Y 0 Y is 1.110 X 20
0111 000
In the past, floating point operations were Arithmetic: All R-Types but different See
Reference chart.
performed using a separate chip 'coprocessor 1' opcode+format+ft+fs+fd+funct
FPA (Floating Point Accelerator).
Now merged. FP addition
single: add.s, double: add.d
add.s $f2,$f4,$f6 #f2 =
You can store values in ordinary registers f4+f6
(since 32 bits, at least for single but for Decim opcode/format/funct 17/16/6/4/2/0
arithmetic should use only fp registers) Hex:: opcode/format/funct 11/10/6/4/2/0
FP subtract
MIPS has 32 single precision (32 bit) fp Single: sub.s, double: sub.d
registers: $f0 – $f31
FP multiplication
Single: mul.s, double: mul.d
$f0 is not special (it can hold any bit pattern, mul.d $f2,$f4,$f6
not just zero). ??? #[f2|f3]=[f4|f5]x[f6|f7]
FP division
Because 0.0 is same with 0 $zero
Single: div.s, double: div.d
mul.s $f2,$f4,$f6
Single precision floating point load, store, #[f2|f3]=[f4|f5]x[f6|f7]
arithmetic use $f0-$f31.
(No Hı|Lo) in Mul and Div result is in fd directly
compare if equal
Load: c.eq.s – c.eq.d
Lwc1 $f1,100($a2)
#load one word f1 = address
a2+100
Not equal
c.neq.s – c.neq.d
lwc1 $f1,12($sp)
# load 4 bytes (1 word) to f1
Less than
c.lt.s – c.lt.d
ldc1 $f2, 0($sp)
# load 8 bytes (2 words)to f2&f3 Greater than
c.gt.s – c.gt.d
Ex: REMEMBER
Branch
lwc1 $f4,8($a0) #loads a word
--- same effect Comparison sets a single bit condition
lw $t4,8($a0) #loads the same word : branch uses that bit only to branch
lwc1 $f4, 16($sp) #load x into f4 s.s fd,addr # store register fd to addr
lwc1 $f6, 12($sp) # load into
mul.s $f2,$f4,$f6 # f2 = f4+f6
swc1 $f2, 4($sp) #store f2 to z li.s fd,val # load register $fd with val
31