Professional Documents
Culture Documents
Computer Arithmetic
Number Representation
Binary numbers (base 2) - integers
0000 0001 0010 0011 0100 0101 0110 0111
1000 1001 . . .
in decimal from 0 to 2n-1 for n bits
MIPS represents numbers as 32-bit constants.
0 ten is in MIPS
0000 0000 0000 0000 0000 0000 0000 0000two
Bit 31 (most significant bit) Bit 3 Bit 0
Thus the largest (unsigned) number is
1111 1111 1111 1111 1111 1111 1111 1111two or
4,294,967,295 ten = 232 -1
Number Representation
Bits are just bits (have no inherent meaning)
conventions define the relationships between bits and numbers
But numbers can also be signed: if the sign bit is 0 the
number is positive, if it is 1 the number is negative.
Thus the signed number
1111 1111 1111 1111 1111 1111 1111 1111two is
-1ten
With 32 bits the range of integers becomes –231 (-2,147,483,648)
is 1000 0000 0000 0000 0000 0000 0000 0000two
to 231-1 (2,147,483,647)
Computers use the two’s complement to represent the X signed
number as (x31 -231)+(x30 230)+…+(x1 21)+(x0 20)
MIPS Representations
32-bit signed numbers (2’s complement):
0000 0000 0000 0000 0000 0000 0000 0000two = 3ten -3ten = 0
To get the two’s complement all 0s become 1s, and all 1s
become 0s, then a 1 is added to the least significant bit
Converting n-bit numbers into numbers with more than n bits:
MIPS 16-bit immediate gets converted to 32 bits for
arithmetic
copy the most significant bit (the sign bit) into the other bits
0010 -> 0000 0010
1010 -> 1111 1010
MIPS Representations
Exercise:
what is the decimal’s equivalent of the two’s complement of
1111 1111 1111 1111 1111 1110 0000 1100two
We negate first
Sign bit
sltiu 11 constant
I-Type: op Rs Rt Immed 16
+ 11…
11101 (-310) 00…
Overflow
Adding two numbers of different sign does not yield an
overflow
Subtracting operands of same sign does not yield an
overflow
Overflow (when number of available bits is not
sufficient – addition causes carry out of MSB
and subtraction causes a borrow into MSB)
Examples of overflow:
A+B<0 (when A,B0);
A+B0 (when A,B<0)
A-B<0 when A0, B<0
A-B0 when A<0, B0
MIPS Instructions
Category Instr Op Example Meaning
Code
Arithmetic add unsigned 0 and 33 addu $s1, $s2, $s3 $s1 = $s2 + $s3
(R & I subtr unsigned 0 and 35 subu $s1, $s2, $s3 $s1 = $s2 - $s3
format)
add imm. 9 addiu $s1, $s2, 6 $s1 = $s2 + 6
unsigned
Data load byte 36 lbu $s1, 25($s2) $s1 = Memory($s2+25)
Transfer unsigned
Cond. set on less than 0 and 43 sltu $s1, $s2, $s3 if ($s2<$s3) $s1=1 else
Branch (I unsigned
$s1=0
& R format)
set on less than 11 sltiu $s1, $s2, 6 if ($s2<6) $s1=1 else
$s1=0
imm. unsigned
Sign extend - addiu, sltiu
Zero extend - lbu
No overflow detected - addu, subu, addiu, sltu,
sltiu
because unsigned numbers are used for addresses
Overflow Detection and Effects
Some instructions (add, addi, sub) detect overflow and
cause an interrupt (exception)
When an overflow interrupt occurs control jumps to predefined
memory address for exceptions
Address of instruction causing the overflow is saved for
possible resumption. The offending address is stored in the
exception program counter $epc (not part of the Register
File)
To debug the program, the contents of this register can be moved to
a general purpose register “move from system control” (R-Type
operation) mfc0 $k1, $epc #$k1=$epc $k0, $k1
OS-reserved registers in Register File
After debugging, and restoring registers, control can return with a
jump to the OS-reserved register
jr $k1
Example
Remember that xor sets a bit to 1 if the two corresponding bits
differ. Code can detect an overflow
addu $t0, $t1, $t2
xor $t3, $t1, $t2 #check if sign of $t1 and$t2 differs
slt $t3, $t3, $zero #if sign differed, $t1=1
bne $t3, $zero, No_overflow #signs differed so no
overflow
xor $t3, $t0, $t1 #$t3 is negative is sign of sum is
different that that of $t1
slt $t3, $t3, $zero #$t3=1 if sum sign is different
than that of $t1
bne $t3, $zero, Overflow # all 3 signs are different
Arithmetic Logic Unit (ALU)
The device that performs the computer arithmetic and logic
operations
It has four
building blocks
For logic
operations only
operation
a•b
a+b
1-bit Binary Adder
carry_in
a b carry_in carry_out Sum
0 0 0 0 0
a
+ Sum 0 0 1 0 1
b 0 1 0 0 1
0 1 1 1 0
carry_out
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
a•b
3
Modifying the ALU Cell for slt
Subtraction is same as
adding negative b thus
Binvert goes Hi,
and CarryIn is 1
Less
Set
Overflow
detection Overflow
First perform a
subtraction
Make the result 1 if the
subtraction yields a
negative result
Make the result 0 if the
subtraction yields a
positive result
Modifying the ALU for beq
Bnegate Operation
Output Zero
goes Hi for
equality
MIPS Multiply Instruction
mult $s2, $s3 # hi||lo = $s2 * $s3
op rs rt rd shamt funct
32 0s 32-bit multiplicand
0000000………… 00000 101100011…………… … 1100
1001two x 1001two
Multiplier Multiplicand Product
0 1001 00001001 00000000
1 1001 00001001 00000000
1001 00010010 00001001
0100 00010010 00001001
2 0100 00010010 00001001
0010 00100100 00001001
3 0010 00100100 00001001
0001 01001000 00001001
4 0001 01001000 00001001
0000 10010000 01010001
Observations on Multiply Version 1
1 clock cycle per step => 100 clocks per multiply
Ratio of multiply to add 1:5 to 1:100
1/2 bits in multiplicand always 0
=> 64-bit adder is wasted
0’s inserted in right of multiplicand as it is shifted left
=> least significant bits of product never changed once
formed
Instead of shifting multiplicand to left, shift product to
right?
Multiply Hardware Version 2
32-bit Multiplicand register, 32 -bit ALU, 64-bit
Product register, 32-bit Multiplier register
If Multiplier0 = 1
Multiply Algorithm Version 2
1001two x 1001two
Iter. Multiplicand Product
0 1001 0000 1001
1 1001 0000 1001
add 1001 1001 1001
shift 1001 0100 1100
2 1001 0100 1100
shift 1001 0010 0110
3 1001 0010 0110
shift 1001 0001 0011
4 1001 0001 0011
add 1001 1010 0011
shift 1001 0101 0001
Multiplication of signed integers
What about signed multiplication?
Easiest solution is to make both positive & remember
whether to complement product when done (leave out the
sign bit, run for 31 steps)
Apply definition of 2’s complement. Need to sign-extend
partial products and subtract at the end
Booth’s Algorithm is elegant way to multiply signed
numbers using same hardware as before and save cycles
It can handle multiple bits at a time, thus it is faster
Motivation for Booth’s Algorithm
Example 2 x 6 = 0010 x 0110two:
0010
x 0110
+ 0000 shift (0 in multiplier)
+ 0010 add (1 in multiplier)
+ 0010 add (1 in multiplier)
Now the test in the algorithm depends on two bits. Results are placed
in the left half of the product register.
Current Bit Bit to the Right Explanation Example Op
1 0 Begins run of 1s 0001111000 subtract
1 1 Middle of run of 1s 0001111000 no op
0 1 End of run of 1s 0001111000 add
0 0 Middle of run of 0s 0001111000 no op
Originally for Speed (when shift was faster than add)
Replace a string of 1s in multiplier with an initial subtract when we
first see a 10 and then later add for the first 01
Booths Example (2 x 7) mythical bit
Operation Multiplicand Product register next operation?
0. initial value 0010 0000 0111 0 10 -> subtract
1a. P = P - m 1110 + 1110 1110
0111 0 shift P (sign extend)
1b. 0010 1111 0011 1 11 -> nop, shift
2. 0010 1111 1001 1 11 -> nop, shift
3. 0010 1111 1100 1 01 -> add
4a. 0010 +0010
0001 1100 1 shift
4b. 0010 0000 1110 0 done
Booths Example (2 x -3) (1111 1010two)
Operation Multiplicand Product next?
+ 0010
If Reminder63 = 1, Quotient0=0
Initially holds dividend Reminder63 = 0, Quotient0=1
Divide Algorithm Version 1
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
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
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
Division Hardware (Version 3)
32-bit Divisor reg, 32 -bit ALU, 64-bit Remainder reg,
(0-bit Quotient reg)
At the
start
dividend
is here
Divide Algorithm Version 3
Divide 0000 0111 by 0010 (2s 1110)
Iteration Divisor Remainder reg oper?
Remainder register initially holds dividend
0. 0010 0000 0111 in. val
0a 0010 0000 1110 shft lf 1
1a 0010 1110 1110 Rem=Rem-Div
1b 0010 0000 1110 Rem=Rem+Div
0001 1100 sll Rem, R0=0
2a 0010 1111 1100 Rem=Rem-Div
2b 0010 0001 1100 Rem=Rem+Div
0011 1000 sll Rem, R0=0
3a 0010 0001 1000 Rem=Rem-Div
3b 0010 0011 0001 sll Rem, R0=1
remainder quotient
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
disagree
e.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”)
Review: MIPS Instructions, so far
Category Instruction Op Code Example Meaning
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
Arithmetic add immediate 8 addi $s1, $s2, 6 $s1 = $s2 + 6
(R & I add immediate 9 addiu $s1, $s2, 6 $s1 = $s2 + 6
format) unsigned
multiply 0 and 24 mult $s1, $s2 hi || lo = $s1 * $s2
multiply 0 and 25 multu $s1, $s2 hi || lo = $s1 * $s2
unsigned
divide 0 and 26 div $s1, $s2 lo = $s1/$s2,
remainder in hi
divide 0 and 27 divu $s1, $s2 lo = $s1/$s2,
unsigned remainder in hi
Review: MIPS ISA, continued
Category Instr Op Code Example Meaning
Shift sll 0 and 0 sll $s1, $s2, 4 $s1 = $s2 << 4
(R srl 0 and 2 srl $s1, $s2, 4 $s1 = $s2 >> 4
format)
sra 0 and 3 sra $s1, $s2, 4 $s1 = $s2 >> 4
Data load word 35 lw $s1, 24($s2) $s1 = Memory($s2+24)
Transfer store word 43 sw $s1, 24($s2) Memory($s2+24) = $s1
(I format)
load byte 32 lb $s1, 25($s2) $s1 = Memory($s2+25)
load byte 36 lbu $s1, 25($s2) $s1 = Memory($s2+25)
unsigned
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
Review: MIPS ISA- continued
Category Instr Op Code Example Meaning
Cond. br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L
Branch br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L
(I & R
set on less than 0 and 42 slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else
format)
$s1=0
set on less than 0 and 43 sltu $s1, $s2, $s3 if ($s2<$s3) $s1=1 else
unsigned $s1=0
set on less than 10 slti $s1, $s2, 6 if ($s2<6) $s1=1 else
immediate $s1=0
set on less than imm. 11 sltiu $s1, $s2, 6 if ($s2<6) $s1=1 else
unsigned $s1=0
Uncond. jump 2 j 2500 go to 10000
Jump (J jump and link 3 jal 2500 go to 10000; $ra=PC+4
& R format)
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
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
Scientific Notation
Normalized notation – single number to the Decimal notation
left of decimal point (no leading 0s) Exponent - how many digits the decimal point is moved
to left to get to 1
Sign, magnitude
23 -24
1.02 x 10 -1.673 x 10
yyyyyyy
±1.xxxxxtwo × 2
radix (base 2)
Number of xs determines accuracy
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 S E F
fraction:
Signed exponent sign + magnitude, normalized
binary significand
When exponent is too large – or too small – an exception
Overflow, or underflow
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 S E F
Signed exponent F
32 bits
IEEE 754 Floating Point Standard
Representation of floating point numbers in IEEE 754 standard -
assures uniformity across computer implementations.
represents
N = (-1) (1+2 +2 +2 )2
-1 (27
-2 +2 1
-3+20
-127)
N = -1.875 2 (131-127)
N = -1.87524 = -1.875 16= -30
Exercis
Show the IEEE 754 representation
e of 10ten in single and double
precision
R2000 CPU
Basic Addition Algorithm
For addition (or subtraction) there are specific steps that are taken to
make sure the proper digits are added:
(2) this means that the significand of the smaller number is shifted to
the right until the decimal points are aligned.
(4) the result needs to be normalized, which means the decimal point
is shifted left and exponent increases.
then add
9.999•101
+ 0.016•101
10.015•101
Round off
significand
Final result
Floating Point Multiplication
Biased exponents are added then one
bias is subtracted
1.110 •1010 x 9.200 •10-5
New exponent is 10-5=5
when using bias 137 (which is
10+127) + 122 (is -5+127)-127=132
Significands are multiplied
10.212000ten
Unormalized product is 10.212ten •105
Normalized is 1.0212ten •106. Rounded
to four digits 1.021ten •106
Sign is determined by the sign of both
operands + 1.021ten •106
MIPS Floating Point Instructions
Floating point registers have special load and store instructions,
but still use integer registers to store the base address for the loaded
word
To load two floating point numbers, subtract them and place the
result into memory the code is
lwc1 $f4, 100($s2) #loads a 32-bit floating
point number into $f4
lwc1 $f6, 200($s2) #loads another 32-bit
floating point number into $f6
sub.s $f10, $f4, $f6 #$f10=$f4-$f6
swc1 $f10, 240($s2) #store 32-bit floating
point into memory
MIPS Floating Point Instructions
Flow operations also have floating point variants
Compare less than single and Compare less than double