arithmetic for computer (alu)access.ee.ntu.edu.tw/course/computer_organization_93... · 2010. 7....
TRANSCRIPT
臺大電機吳安宇教授- 計算機結構
Chap. 3Arithmetic for computer (ALU)
臺大電機系吳安宇教授
v1. 2004/10/4v2. 2004/10/6
V3. 2004/10/20
臺大電機吳安宇教授- 計算機結構
Outline
n 3.1 introductionn 3.2 Signed and Unsigned Numbersn 3.3 Addition and Subtractionn 3.4 Multiplicationn 3.5 Divisionn 3.6 Floating Point
臺大電機吳安宇教授- 計算機結構
Introduction
n (A) Representation of numbersn 1. How to represent a “negative” number?n 2. What is the ”largest” number that can be
represented by a computer word?n 3. How to deal with “overflow” ?n 4. How to represent fractions and real numbers?
n (B) Arithmetic algorithmsn 1. Addition/Subtractionn 2. Multiplication/Division
n (C) Hardware/MIPS instructions for (A)&(B)
臺大電機吳安宇教授- 計算機結構
Outline
n 3.1 introductionn 3.2 Signed and Unsigned Numbersn 3.3 Addition and Subtractionn 3.4 Multiplicationn 3.5 Divisionn 3.6 Floating Point
臺大電機吳安宇教授- 計算機結構
Positive numbersn 10112=1x23+0x22+1x21+1x20
=8+2+1 =1110
n In MIPS word ( = 32 bits)
n Bit31= Leftmost bit = Most significant bit (MSB)n Bit0 = Rightmost bit = Least significant bit (LSB)
We can represent positive integers (without negative number): 0 = 0000……00002
1 = 0000……00012
2 = 0000……00102
::
232-1=4,294,967,29510=1111……11112
10110000……00000000
臺大電機吳安宇教授- 計算機結構
2’s Complement Representation
MSB=sign bitn 0: Positive number or zeron 1: Negative number
n Formula
n Examplen 1111 1111 …… 1111 11002
= -231+230+229+……+22 = -410
n Flip and plus one (negation)0000 0000 …… 0000 0011 +1 = 410
Value in Base-10
= -231xbit31 + 230 xbit30+229xbit29+……+20xbit0
臺大電機吳安宇教授- 計算機結構
Negation shortcutn Negate 210, and then check the result by negating -210
n 210=0000 0000 0000 0000 0000 0000 0000 00102
1111 1111 1111 1111 1111 1111 1111 11012
+ 12
= 1111 1111 1111 1111 1111 1111 1111 11102
= -210
0000 0000 0000 0000 0000 0000 0000 00012
+ 12
= 0000 0000 0000 0000 0000 0000 0000 00102
= 210
臺大電機吳安宇教授- 計算機結構
Sign extension shortcut
n Convert 16-bit binary version of 210 and -210 to 32-bit binary numbers.n 0000 0000 0000 00102=210
Make 16 copies of the value in MSB. Then, make 2’s complementè 0000 0000 0000 0000 0000 0000 0000 00102=210
1111 1111 1111 11012
+ 12
= 1111 1111 1111 11102
= -210
Make 16 copies of the value in MSBè 1111 1111 1111 1111 1111 1111 1111 1110=-210
臺大電機吳安宇教授- 計算機結構
Signed numbersn Signed integer (+/-) : Normal number
Unsigned integer (+) : Memory addressing
=> slt, slti : set on less than, signed integersltu (sltiu) : set on less than, (immediate) unsigned integer
n Example$s1 = 1111 1111 …… 11112 (= -1 for signed or (232-1) for unsigned)$s2 = 0000 0000 …… 00012
slt $t0,$s1,$s2 # signed comparison è $8=1sltu $t0,$s1,$s2 # unsigned comparison è $8=0
If($s2<100),$s1=1; else $s1=0sltiu $s1,$s2,100sltiu
If($s2<$s3),$s1=1; else $s1=0sltu $s1,$s2,$s3sltu
Meaning (unsigned comparison)ExampleInst
臺大電機吳安宇教授- 計算機結構
Sign extensionn lb (load byte from memory to register)
Sign extension: fill with x’s (x=0 or 1)
n lbu (load byte unsigned from memory to register)fill with 0’s
n lh (load half)
n lhu (load half unsigned)
. . . .x . . . xxxxxxxxxxxxxxxxxxxxxxxx
. . . . . . . . 000000000000000000000000
. . . . . . . . . . . .x . . . xxxxxxxxxxxxxxxx
. . . . . . . . . . . . . . . . 0000000000000000
臺大電機吳安宇教授- 計算機結構
臺大電機吳安宇教授- 計算機結構
Outline
n 3.1 introductionn 3.2 Signed and Unsigned Numbersn 3.3 Addition and Subtractionn 3.4 Multiplicationn 3.5 Divisionn 3.6 Floating Point
臺大電機吳安宇教授- 計算機結構
Addition and Subtractionn Example
n Adding 6 to 7
臺大電機吳安宇教授- 計算機結構
Addition and Subtractionn Example
n subtracting 6 from 7
臺大電機吳安宇教授- 計算機結構
Overflow
n Overflow : Sum/difference of two 32-bit numbers is too large to be represented in 32 bits.
n Example:
Bit 31 30 3 2 1 0
n 0111 1111……1111 = 2,147,483,647+) 0000 0000……0010 = 2
10……………… 0001 = - 2,147,483,647check sign bit
臺大電機吳安宇教授- 計算機結構
Overflow conditionn Exception Also called interrupt. An unscheduled event that
disrupts program execution; used to detect overflow.n Interrupt An exception that comes from outside of the processor.
(Some architectures use the term interrupt for all exceptions)n Add (add), add immediate (addi), and subtract (sub) cause
exceptions on overflow.n Add unsigned (addu), add immediate unsigned (addiu), and
subtract unsigned (subu) do not cause exceptions on overflow.
n Adding operands of different signs à “no” overflow can occur
臺大電機吳安宇教授- 計算機結構
How to handle overflow in HW?1. MIPS detects overflow with “exception” (interrupt) which is
essentially an unplanned procedure call.2. Steps : (HW interrupt)
� The address of the instruction that caused overflow is saved in a register.
� Jump to the service routine for that exception� Return to the program
3. MIPS uses� A register called “exception program counter” (EPC) to
contain the address of the instruction that causes the exception.
� Instruction “move from system control (MFCO)” to copy EPC into a register so that MIPS software can return to the offending instruction via “jr” instruction.
臺大電機吳安宇教授- 計算機結構
Summaryn Unsigned number is usually used for memory
address and logic operations. In MIPS, the max address number is
232 = 4,294,967,29610
à Usually, we can ignore overflow
臺大電機吳安宇教授- 計算機結構
Outline
n 3.1 introductionn 3.2 Signed and Unsigned Numbersn 3.3 Addition and Subtractionn 3.4 Multiplicationn 3.5 Divisionn 3.6 Floating Pointn 3.7 Floating Point in the IA-32
臺大電機吳安宇教授- 計算機結構
Multiplicationn Multiplying 10002x10012
Multiplicandà 1000 à 8Multiplier à x) 1001 à 9
100000000
0000001000000
product à 1001000 à 72n Steps:
1. If multiplier bit =0, place 0=1, place multiplicand
2. Do summation to get partial product
n n-bit multiplicand × m-bit multiplier à (n+m)-bit product
臺大電機吳安宇教授- 計算機結構
First version of multiplication HW
臺大電機吳安宇教授- 計算機結構
First version of multiplication algorithm
臺大電機吳安宇教授- 計算機結構
0010 x 0011
臺大電機吳安宇教授- 計算機結構
Second version of multiplication HW
臺大電機吳安宇教授- 計算機結構
0010 x 0011Multiplicand Multiplier
Multiplier/
1
臺大電機吳安宇教授- 計算機結構
Multiplication
n Differences between 1st & 2nd version:n 64-bit multiplicand à 32 bitn 64-bit ALU à 32 bitn Instead of shifting the multiplicand to the left (x2), we shift the
“product” to the right
n Multiply in MIPSn MIPS provides a separate pair of 32-bit registers to contain the
64-bit product, called Hi and Lo. n To produce a properly signed or unsigned product, MIPS has
two instructions: multiply (mult) and multiply unsigned (multu).n To fetch the integer 32-bit product, the programmer uses move
from lo (mflo). n The MIPS assembler generates a pseudo instruction for multiply
that specifies three general purpose registers, generating mfloand mfhi instructions to place the product into registers.
臺大電機吳安宇教授- 計算機結構
Outline
n 3.1 introductionn 3.2 Signed and Unsigned Numbersn 3.3 Addition and Subtractionn 3.4 Multiplicationn 3.5 Divisionn 3.6 Floating Pointn 3.7 Floating Point in the IA-32
臺大電機吳安宇教授- 計算機結構
Divisionn Dividing 1,001,010 by 1000
n Dividend = Quotient x Divisor + Remainderwhere Remainder < Divisor
1001
10 1000 1010 101 10
1000000 10010101000
−
−
Quotient
Dividend
Divisor
Remainder
臺大電機吳安宇教授- 計算機結構
First version of Division HW
Dividend
Set 1/Set 0
臺大電機吳安宇教授- 計算機結構
0000 0111 ÷ 0010
Restore/set0NegativeRestore/set0Negative
Restore/set0Negative
set1Positive
set1Postitive
臺大電機吳安宇教授- 計算機結構
First version of Division algorithm
臺大電機吳安宇教授- 計算機結構
An improved version of division
Dividend
QuotientRemainder Set 0/1
Add/subtract
臺大電機吳安宇教授- 計算機結構
c. f. 2nde version of multiplication HW
Add
臺大電機吳安宇教授- 計算機結構
Divisionn The same sequential hardware can be used for
both multiply and divide. The only requirement is a 64-bit register that can shift left or right and a 32-bit ALU that adds or subtracts.
n MIPS uses the 32-bit Hi and Lo registers for both multiply and divide.
n MIPS (Lo = quotient, Hi = Remainder)Div $2,$3 # Lo=$2 / $3 , Hi=$2 mod $3 (signed)Divu $2,$3 # Lo=$2 / $3 , Hi=$2 mod $3 (unsigned)mfhi $1mflo $4
臺大電機吳安宇教授- 計算機結構
Outline
n 3.1 introductionn 3.2 Signed and Unsigned Numbersn 3.3 Addition and Subtractionn 3.4 Multiplicationn 3.5 Divisionn 3.6 Floating Point
臺大電機吳安宇教授- 計算機結構
Normalized numbern In addition to signed and unsigned integers, we have
“Real numbers” in mathematicsπ=3.14159……e=2.71828……3,155,760,000 = 3.15576×109
0.000 000 001 = 1.0×10-9
n A number in scientific notation that has no leading 0 is called a “normalized” number
Ex. 1.0×10-9 (ˇ) à Normalized scientific notation0.1×10-10 ( X )10.0x10-8 ( X )
臺大電機吳安宇教授- 計算機結構
Representationn In binary digits
n ± 1.xxxxxxx2 × 2yyyy (base 2)n Representation in MIPS
23 bits(F)
8 bits(E)
1 bit(S)
fractionexponentS
22~030~2331
N=(-1)S*F*2E
(2’s complement à biased by 127)
臺大電機吳安宇教授- 計算機結構
Accuracy & Rangen Trade-off between “accuracy” and “range”
n Increasing the size of fraction enhances accuracy.n Increasing the size of exponent increases the range.
n Overflow: number is too large to be represented by the hardware.
n Underflow: number is too small to be represented by the hardware.
n Exercise
à∞xx-∞ß
Maxpositive No.
Minpositive No.
0Maxnegative No.
Minnegative No.
臺大電機吳安宇教授- 計算機結構
Double precision
n Double precision floating-point number (two 32-bit words, by IEEE 754 floating-point standard)
20 bits(F)
11bits(E)
1 bit(S)
fractionexponentS19~030~2031
32bits
fraction (cont’d)
word0
word1
臺大電機吳安宇教授- 計算機結構
Representing floating-point numbers
(IEEE 754 floating-point standard)
1. 0 à S=E=F=02. Hidden 1 is added (Recall N= ± 1.xxxxxxx2 × 2yyyy ) 3. N=(-1)S x [ 1 + Fraction ] x 2E
=(-1)S x [ 1 + (S1x2-1) + (S2x2-2)+…+…] x 2E
臺大電機吳安宇教授- 計算機結構
Representing floating-point numbers
A. 2’s complement exponent is used+1.0x2 -1 +1.0x2+1 (no good for compare)
31 30~23 22~0
B. Biased notation Exponent:
000………000111111110 000………000000000010
+128à255
Real meaning+bias(127)
No-tation
…………+1à128
0à127
…………-126à1
Reserved for 0Denormalized No.
à0
∴N=(-1)S x (1+ Fraction) x 2 (Exponent-Bias)
where bias = 127 (for single precision)= 1023 (for double precision)
臺大電機吳安宇教授- 計算機結構
Floating Point Conversion
n Ex. Represent -0.7510 in IEEEE 754 standard-0.7510 = -0.112
= -0.11x20 (scientific notation)= -1.1x2-1 (normalized scientific notation)
add 127 to exponentà (-1)1x(1+.1000…0)x2(126)
à1000………00011111101
22~030~2331
臺大電機吳安宇教授- 計算機結構
Floating Point Conversion
n Ex. What’s the number in base 10 ?
n Sign bit = 1 (Negative no.)
n Exponent = 129 à Real exponent = 129-127 = 2
n Fraction = 2^(-2) = 0.25
N=(-1)S x (1 + Fraction) x 2(Exponent-Bias)
= -11 x (1 + 0.25) x 22
= -1 x 1.25 x 4 = (-5.0)10
010………0010000001122~030~2331
臺大電機吳安宇教授- 計算機結構
Floating-Point Addition
Ex. 9.999 x 101 + 1.610 x 10-1 (4 fraction digits)n Step1: Align the decimal point with the larger
vexponent (Note: large number is more important !)1.610x10-1 à 0.016x10+1
n Step2: Adding the Fractions9.999
+) 0.016 10.015 sum=10.015x10+1
n Step3: Write in “normalized form”sum = 1.0015x102
n Step4: Truncate fraction to 4 digitssum=1.002x102
臺大電機吳安宇教授- 計算機結構
Floating Point Multiplication
n Ex: 1.110x1010 + 9.200x10-5
(4 digits for fraction, 2 digits for exponent)n Step1: Adding exponent
10+(-5)=5 à new exponentn Step2: Multiplying fractions
1.110x) 9.200 à product = 10.21200x10+5
10.2120010
n Step3: Normalize the result10.21200x10+5=1.0212x10+6
check overflow: exponent is too largeunderflow: large negative exponent
n Step4: Rounding exponent: 1.021x10+6
n Step5: Add sign: + 1.021x10+6
臺大電機吳安宇教授- 計算機結構
HW for floating-point addition
臺大電機吳安宇教授- 計算機結構
Flowchart for floating-point addition
臺大電機吳安宇教授- 計算機結構
Flowchart for Floating Point Multiplication
臺大電機吳安宇教授- 計算機結構
HW #3 (2004/10/6)n TA will teach basic MIPS assembly rules and
PCSpim next week (10/13), 10-12amn Due date: Oct. 22 (Friday), 5pm in E2-232,
to TA 沈文中 (in front of lab, put in box)n Problem set A:
1. Write down your MIPS assembly code for SORTING 50 numbers (check Fig. 2.36) from small to big numbers.
2. The input data format are: 50, 1, 48, 3, 46, 5, ….., 4, 47, 2, 49. 3. The output data should be: 1, 2, 3, …., 49, 50, in your memory.4. Use PCSpim to verify your results, and PRINT your sorted
results.5. Hand in: (a) printed source code (b) printed sorting results in
PCSpim environment (results on screen)
臺大電機吳安宇教授- 計算機結構
HW #3 (2004/10/6)
Problem set B:1. Follow the flowchart style of Fig. 3.6., draw the flowchart of
Fig.3.7 (the refined multiplier hardware)2. In many low-cost CPU, there is no multiplier hardware inside the
ALU. Hence, the multiplication must be performed by subroutine/procedure call.
1. If the MIPS does not have the multiplier hardware, follow Fig. 3.7 to write down a subroutine for multiplication (Hint: Use Shift, Add, plus some Loop control, and masking technique (ADD, OR))
2. Write down your MIPS assembly code for multiplying two 16-bit numbers ($s1 = 0000e366_hex, $s2 = 0000d3ef_hex), and store the results in $s5.
3. Use PCSpim to verify your results, and PRINT your sorted results.4. Hand in: (a) The flowchart, (b) printed source code, (c) printed sorting
results in PCSpim environment (results on screen).
臺大電機吳安宇教授- 計算機結構
Homework #4 (2004/10/20)
n 3.1, 3.2, 3.3n 3.7, 3.9n 3.29, 3.30n Total 7 problems
n Due date: 10/29 (Friday by 2pm) to TA Shen. No late submissions