FLOATING POINT ARITHMETIC. TOPICS Binary representation of floating point Numbers Computer representation of floating point numbers Floating point instructions.

Slides:



Advertisements
Similar presentations
Set 16 FLOATING POINT ARITHMETIC. TOPICS Binary representation of floating point Numbers Computer representation of floating point numbers Floating point.
Advertisements

Lecture - 2 Number systems and computer data formats
Faculty of Computer Science © 2006 CMPUT 229 Floating Point Representation Operating with Real Numbers.
1 IEEE Floating Point Revision Guide for Phase Test Week 5.
CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (2)
Computer ArchitectureFall 2007 © September 5, 2007 Karem Sakallah CS 447 – Computer Architecture.
Floating Point Numbers
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
The IEEE Format for storing float (single precision) data type Use the “enter” key to proceed through the show.
Binary Representation and Computer Arithmetic
Simple Data Type Representation and conversion of numbers
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Fixed-Point Arithmetics: Part II
COMPSCI 210 Semester Tutorial 1
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 10 Department of Computer Science and Software Engineering University of.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Floating Point Arithmetic
Computer Systems Architecture Copyright © Genetic Computer School 2008 CSA 1- 0 Lesson 1 Number System.
The Teacher CP4 Binary and all that… CP4 Revision.
1 Lecture 2  Complement  Floating Point Number  Character Encoding.
The Teacher CP4 Binary and all that… CP4 Revision.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Floating Point Representation.
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Introduction to Number System
Fractions in Binary.
 Lecture 2 Processor Organization  Control needs to have the  Ability to fetch instructions from memory  Logic and means to control instruction sequencing.
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
Dr Mohamed Menacer College of Computer Science and Engineering Taibah University CE-321: Computer.
Data Representation: Floating Point for Real Numbers Computer Organization and Assembly Language: Module 11.
Real Numbers SignExponentMantissa.
Lecture 6: Floating Point Number Representation Information Representation: Floating Point Number Representation Lecture # 7.
Learning Objectives 3.3.1f - Describe the nature and uses of floating point form 3.3.1h - Convert a real number to floating point form Learn how to normalise.
ECE291 Computer Engineering II Lecture 11 Dr. Zbigniew Kalbarczyk University of Illinois at Urbana- Champaign.
Chapter 14: The Arithmetic Coprocessor, MMX, and SIMID Technologies.
Chapter 9 Computer Arithmetic
William Stallings Computer Organization and Architecture 8th Edition
Floating Points & IEEE 754.
Floating Point Representations
Department of Computer Science Georgia State University
Fundamentals of Computer Science
Introduction To Computer Science
William Stallings Computer Organization and Architecture 7th Edition
Chapter 6 Floating Point
Outline Introduction Floating Point Arithmetic Adder Multiplier.
Using the 80x87 Math Chip in Assembly
ECEG-3202 Computer Architecture and Organization
Starter Using the fingers on only one hand, what is the highest number you can count to? Rules: You must start at 1 You must count sequentially (i.e.
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Presentation transcript:

FLOATING POINT ARITHMETIC

TOPICS Binary representation of floating point Numbers Computer representation of floating point numbers Floating point instructions

BINARY REPRESENTATION OF FLOATING POINT NUMBERS Converting decimal fractions into binary representation. Consider a decimal fraction of the form: 0.d1d2...dn We want to convert this to a binary fraction of the form: 0.b1b2...bn (using binary digits instead of decimal digits)

Algorithm for conversion Let X be a decimal fraction: 0.d 1 d 2..d n i = 1 Repeat until X = 0 or i = required no. of binary fractional digits { Y = X * 2 X = fractional part of Y B i = integer part of Y i = i + 1 }

EXAMPLE 1 Convert 0.75 to binary X = 0.75 (initial value) X* 2 = Set b1 = 1, X = 0.5 X* 2 = 1.0. Set b2 = 1, X = 0.0 The binary representation for 0.75 is thus 0.b1b2 = 0.11b

Let's consider what that means... In the binary representation 0.b 1 b 2...b m b 1 represents 2 -1 (i.e., 1/2) b 2 represents 2 -2 (i.e., 1/4)... b m represents 2 -m (1/(2 m )) So, 0.11 binary represents = 1/2 + 1/4 = 3/4 = 0.75

EXAMPLE 2 Convert the decimal value 4.9 into binary Part 1: convert the integer part into binary: 4 = 100b

Part 2. Convert the fractional part into binary using multiplication by 2: X =.9*2 = 1.8. Set b 1 = 1, X = 0.8 X*2 = 1.6. Set b 2 = 1, X = 0.6 X*2 = 1.2. Set b 3 = 1, X = 0.2 X*2 = 0.4. Set b 4 = 0, X = 0.4 X*2 = 0.8. Set b 5 = 0, X = 0.8, which repeats from the first line above.

Since X is now repeating the value 0.8, we know the representation will repeat. The binary representation of 4.9 is thus:

COMPUTER REPRESENTATION OF FLOATING POINT NUMBERS In the CPU, a 32-bit floating point number is represented using IEEE standard format as follows: S | EXPONENT | MANTISSA where S is one bit, the EXPONENT is 8 bits, and the MANTISSA is 23 bits.

The mantissa represents the leading significant bits in the number. The exponent is used to adjust the position of the binary point (as opposed to a "decimal" point)

The mantissa is said to be normalized when it is expressed as a value between 1 and 2. I.e., the mantissa would be in the form 1.xxxx.

The leading integer of the binary representation is not stored. Since it is always a 1, it can be easily restored.

The "S" bit is used as a sign bit and indicates whether the value represented is positive or negative.

If a number is smaller than 1, normalizing the mantissa will produce a negative exponent. But 127 is added to all exponents in the floating point representation, allowing all exponents to be represented by a positive number.

Example 1. Represent the decimal value 2.5 in 32-bit floating point format. 2.5 = 10.1b In normalized form, this is: 1.01 * 2 1 The mantissa: M = (23 bits without the leading 1) The exponent: E = = 128 = b The sign: S = 0 (the value stored is positive) So, 2.5 =

Example 2: Represent the number b in floating point form b = * 2 -4 Mantissa: M = (23 bits with the integral 1 not represented) Exponent: E = = b S = 1 Result:

Exercise 1: represent in floating point format. Exercise 2: represent 4.9 in floating point format.

FLOATING POINT INSTRUCTIONS Floating point Architecture: 8 80-bit stack registers ST(0), ST(1),..,ST(7) (ST(0) can be abbreviated as ST) To use the floating point stack, we: Push data from memory onto the stack Process data Pop data from stack to memory.

Some floating point instructions: INSTRUCTIONDESCRIPTION Push and pops FLD, FSTPPush and pop floating point data FILD, FISTPPush and pop integer data Arithmetic In all 4 cases below, the stack is popped subsequently. So the ST(1) shown becomes ST(0) FADDST(1) = ST(0) + ST(1) FSUBST(1) = ST(0) - ST(1) FMULST(1) = ST(0) * ST(1) FDIVST(1) = ST(0) / ST(1)

Trigonometry FSINST(0) = sine of ST(0) radians FCOSST(0) = cosine of ST(0) radians FTANST(0) = tan of ST(0) radians FLDPI Push value of  onto stack

Example 1. X DW 3.4 Y DW 2 'This is an integer, while 2.0 is flt. pt. Z DW ? To evaluate Z = X + Y FLD X 'ST(0) = X FILD Y 'ST(0) = Y, ST(1) = Y FADD 'ST(0) = X + Y FSTP Z 'Z = X + Y

Example 2. To evaluate X * Y - U / V X DD 3.9 Y DD 2.8 U DD 7.3 V DD 4.62 'code follows FLD U'st(0) = U FLD V'st(0) = V, st(1) = U FDIV 'st(0) = U / V FLD X'st(0) = X, st(1) = U / V FLD Y'st(0) = Y, st(1) = X, st(2) = U / V FMUL'st(0) = Y * X, st(1) = U / V FSUB'st(0) = Y * X - U / V FSTP Z'Z = result, st(0) = empty

Util.lib contains the following subroutines for inputting and outputting floating point numbers: GetFP This inputs a no. such as 33 or 3.56 from the keyboard, and pushes it, in binary floating point form, onto the stack. PutFP This pops the floating point number from the top of the stack, and outputs it in ascii.

Note The example following assumes that you have a copy of mymacros.txt in your masm615/programs directory, and that, if the program in the example is named circle.asm, you compile the program using:mymacros.txt ml circle.asm util.lib

Example 3. Calculating Area of a Circle title calculation area of circle with inputted radius extrn getfp:near, putfp:near include mymacros.txt.model small.stack 100h.data radius dd ?.386.code circleproc startup

display "Enter the radius: " call getfp 'could have used the macro: inftp fstp radius fld radius ; instead of the above 3 instructions, we could have ; used fld st(0) or simply fld st fmul fldpi fmul display "The area of the circle is " call putfp 'could have used the macro: outftp endup circleendp end circle

ADDITIONAL FLOATING POINT INSTRUCTIONS In declaring a floating point variable use “real4” rather than “dd”, e.g.: num real4 7.3 count real4 ?

fabsreplaces st (i.e. st(0)) by its absolute value fsqrt replaces st by its square root frndint rounds st up or down to the nearest integer

We have already covered fadd used without arguments. Given,e.g.: m real4 3.6 n dd 21 fadd m will set st(0) = st(0) + m fiadd m will do the same after converting the integer n to floating point fadd st(3) will set st(0) = st(0) + st(3)

The same variations apply to: fmul fdiv fld e.g: fld st will push the value of st onto the stack, so now the top two members of the stack will have this value.