Real Arithmetic. Fractional binary numbers. Fractional binary numbers examples. Binary real numbers

Similar documents
80x87 Instruction Set (x87 - Pentium)

Figure 8-1. x87 FPU Execution Environment

Overview of architecture Interfacing with Data types, instructions. Programming.

The x87 Floating-Point Unit

MATH CO-PROCESSOR Rajiv R Bhandari

Floating Point Instructions

Instruction Set extensions to X86. Floating Point SIMD instructions

LAB WORK NO. 11 THE USAGE OF THE MATHEMATICAL COPROCESSOR

The course that gives CMU its Zip! Floating Point Arithmetic Feb 17, 2000

Build a program in Release mode and an executable file project.exe is created in Release folder. Run it.

AM2: Programming with Contemporary Instruction Set

MAHALAKSHMI ENGINEERING COLLEGE TIRUCHIRAPALLI UNIT III MULTIPROCESSOR CONFIGURATIONS PART A (2 Marks)

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

Computer Science Final Examination Wednesday December 13 th 2006

1/26 Tuesday, January 26, :17 PM

AM2: Protected-Mode Programming

M80C BIT CHMOS III NUMERIC PROCESSOR EXTENSION

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Data Representation Floating Point

Lab 6: Conditional Processing

Floating Point January 24, 2008

Data Representation Floating Point

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

SOEN228, Winter Revision 1.2 Date: October 25,

Floating-Point Arithmetic

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

80C BIT MATH COPROCESSOR

Conditional Processing

Introduction to 8086 Assembly

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 6: Conditional Processing

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Finite arithmetic and error analysis

The Instruction Set. Chapter 5

Module 3 Instruction Set Architecture (ISA)

TDT4255 Computer Design. Lecture 4. Magnus Jahre

Floating point. Today. IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

History of the Intel 80x86

Complex Instruction Set Computer (CISC)

Chapter 3. Floating Point. Floating Point Standard. Morgan Kaufmann Publishers 13 November, Chapter 3 Arithmetic for Computers 1

System Programming CISC 360. Floating Point September 16, 2008

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

Name: CMSC 313 Fall 2001 Computer Organization & Assembly Language Programming Exam 1. Question Points I. /34 II. /30 III.

Introduction to IA-32. Jo, Heeseung

Basic Execution Environment

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

INTRODUCTION TO IA-32. Jo, Heeseung

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

Chapter 3 Arithmetic for Computers

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Giving credit where credit is due

3.1 DATA MOVEMENT INSTRUCTIONS 45

Giving credit where credit is due

1. Required Knowledge to Write in Assembly. 4. Assembler used (gas/masm): (a) Assembler directives (prefixed

Chapter 6 (Part a) Conditional Processing

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2006/09/18. with slides by Kip Irvine

Component Operation 16

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Floating Point Numbers

Program Control Instructions

CS429: Computer Organization and Architecture

Intel Architecture Software Developer s Manual

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Intel Instruction Set (gas)

Selection and Iteration. Chapter 7 S. Dandamudi

Chapter 3. Arithmetic for Computers

EC 333 Microprocessor and Interfacing Techniques (3+1)

Chapter 3. Arithmetic for Computers

Addressing Modes on the x86

Chapter 3: Addressing Modes

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Data Representation Floating Point

Floating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon

mith College Computer Science Fixed-Point & Floating-Point Number Formats CSC231 Dominique Thiébaut

Representing and Manipulating Floating Points

Basic Assembly Instructions

We will first study the basic instructions for doing multiplications and divisions

Summary: Direct Code Generation

Assembly Language: IA-32 Instructions

Compiler construction. x86 architecture. This lecture. Lecture 6: Code generation for x86. x86: assembly for a real machine.

Representing and Manipulating Floating Points

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

Assembly II: Control Flow

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

EEM336 Microprocessors I. Addressing Modes

Chapter 3. Arithmetic for Computers

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Microcontroller Intel [Instruction Set]

Branching and Looping

12.1 Chapter Overview Tables Function Computation via Table Look-up. Calculation Via Table Lookups

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

Transcription:

Fractional binary numbers 2 i 2 i 1 Real Arithmetic Computer Organization and Assembly Languages g Yung-Yu Chuang 4 2 1 b i b i 1 b 2 b 1 b 0. b 1 b 2 b 3 b j Representation 1/2 1/4 1/8 2 j Bits to right of binary point represent fractional i powers of 2 b 2 k k 2 Represents rational number: k j 2 Binary real numbers Binary real to decimal real Decimal real to binary real Fractional binary numbers examples Value Representation 5-3/4 101.1111 2 2-7/8 10.111 2 63/64 0.111111 2 Value Representation 1/3 0.0101010101[01] 0101010101[01] 2 1/5 0.001100110011[0011] 2 1/10 0.0001100110011[0011] 0001100110011[0011] 2 4.5625 = 100.1001 2 3 4

Fixed-point numbers sign integer part fractional part radix point 0 000 0000 0000 0110 0110 0000 0000 0000 = 110.011 only 2 16 to 2-16 Not flexible, not adaptive to applications Fast computation, just integer operations. It is often a good way to speed up in this way If you know the working range beforehand. IEEE floating point IEEE Standard 754 Established in 1985 as uniform standard d for floating point arithmetic Before that, many idiosyncratic formats Supported by all major CPUs Driven by Numerical Concerns Nice standards for rounding, overflow, underflow Hard to make go fast Numerical analysts predominated over hardware types in defining standard 5 6 IEEE floating point format IEEE floating point format IEEE defines two formats with different precisions: single and double 23.85 = 10111.110110 2 =1.0111110110x2 4 e = 127+4=83h 0 100 0001 1 011 1110 1100 1100 1100 1100 special values IEEE double precision 7 8

Denormalized numbers Summary of Real Number Encodings Number smaller than 1.0x2-126 can t be presented by a single with normalized form. However, we can represent it with denormalized format. 1.0000..00x2-126 the least normalized number 0.1111..11x2-126 the largest denormalized number 1.001x2-129 =0.001001x2-126 NaN -Normalized -Denorm 0 0 +0 +Denorm (3.14+1e20)-1e20=0 3.14+(1e20-1e20)=3.14 +Normalized + NaN 9 10 IA-32 floating point architecture Original 8086 only has integers. It is possible to simulate real arithmetic using software, but it is slow. 8087 floating-point i t processor (and 80287, 80387) was sold separately at early time. Since 80486, FPU (floating-point unit) was integrated into CPU. FPU data types Three floating-point types 11 12

FPU data types Four integer types FPU registers Data register Control register Status register Tag register 13 14 Data registers Postfix expression Load: push, TOP-- 79 0 Store: pop, TOP++ R0 Instructions access the R1 TOP stack using ST(i) R2 ST(0) 010 relative to TOP If TOP=0 and push, TOP wraps to R7 R3 R4 R5 ST(1) ST(2) If TOP=7 and pop, TOP R6 wraps to R0 R7 When overwriting occurs, generate an exception Real values are transferred to and from memory and stored in 10-byte temporary format. When storing, convert back to integer, long, real, long real. 15 (5*6)-4 5 6 * 4-6 4 5 5 5 6 30 * 30 4 26-16

Special-purpose registers Special-purpose registers Last data pointer stores the memory address of the operand for the last non-control instruction. Last instruction pointer stored the address of the last non-control instruction. Both are 48 bits, 32 for offset, 16 for segment selector. 1 1 0 1 1 17 18 Control register Initial 037Fh for compatibility only The instruction FINIT will initialize it to 037Fh. 19 Rounding FPU attempts to round an infinitely accurate result from a floating-point calculation Round to nearest even: round toward to the closest one; if both are equally close, round to the even one Round down: round toward to - Round up: round toward to + Truncate: round toward to zero Example suppose 3 fractional bits can be stored, and a calculated l value equals +1.0111. rounding up by adding.0001 produces 1.100 rounding down by subtracting ti.0001 produces 1.011 20

Rounding method original value rounded value Round to nearest even 1.0111 1.100 Round down 1.0111 1.011 Round up 1.0111 1.100 Truncate 1.0111 1.011 method original value rounded value Round to nearest even -1.0111-1.100 Round down -1.0111-1.100 Round up -1.0111-1.011 Truncate -1.0111-1.011 21 Floating-Point Exceptions Six types of exception conditions #I: Invalid operation #Z: Divide by zero detect before execution #D: Denormalized operand #O: Numeric overflow #U: Numeric underflow detect t after execution #P: Inexact precision Each has a corresponding mask bit if set when an exception occurs, the exception is handled automatically by FPU if clear when an exception occurs, a software exception handler is invoked 22 Status register FPU data types bigval REAL10 1.212342342234234243E+864 fld bigval C 3 -C 0 : condition bits after comparisons 23 24

FPU instruction set Instruction mnemonics begin with letter F Second letter identifies ifi data type of memory operand B = bcd I = integer no letter: floating point Examples FBLD load binary coded decimal FISTP store integer and pop p stack FMUL multiply floating-point operands 25 FPU instruction set Fop {destination}, {source} Operands zero, one, or two fadd fadd [a] fadd st, st(1) no immediate operands no general-purpose registers (EAX, EBX,...) (FSTSW is the only exception which stores FPU status word to AX) destination must be a stack register integers must be loaded from memory onto the stack and converted to floating-point before being used in calculations 26 Classic stack (0-operand) ST(0) as source, ST(1) as destination. Result is stored at ST(1) and ST(0) is popped, leaving the result on the top. (with 0 operand, fadd=faddp) Memory operand (1-operand) ST(0) as the implied destination. The second operand is from memory. 27 28

Register operands (2-operand) Example: evaluating an expression Register: operands are FP data registers, one must be ST. Register pop: p the same as register with a ST pop afterwards. 29 30 Load FLDPI stores π FLDL2T stores log 2 (10) FLDL2E stores log 2 (e) FLDLG2 stores log 10 (2) FLDLN2 stores ln(2) 32

load array REAL8 10 DUP(?) fld array ; direct fld [array+16] ; direct-offset fld REAL8 PTR[esi] ; indirect fld array[esi] ; indexed fld array[esi*8] ; indexed, scaled fld REAL8 PTR[ebx+esi]; base-index fld array[ebx+esi] ; base-index-displacement Store 33 34 Store fst dblone ; 200.0 fst dbltwo ; 200.00 fstp dblthree ; 200.0 fstp dblfour ; 32.0 Arithmetic instructions FCHS FABS ; change sign of ST ; ST= ST 35 36

Floating-Point add FADD adds source to destination No-operand version pops the FPU stack after addition Examples: Floating-Point subtract FSUB subtracts t source from destination. No-operand version pops the FPU stack after subtracting Example: fsub mysingle ; ST -= mysingle fsub array[edi*8] ; ST -= array[edi*8] 37 38 Floating-point multiply/divide Miscellaneous instructions FMUL Multiplies li source by destination, stores product in destination FDIV Divides destination by source, then pops the stack x REAL4 2.75 five REAL4 5.2 fld five ; ST0=5.2 fld x ; ST0=2 2.75, ST1=5 5.2 fscale ; ST0=2.75*32=88 ; ST1=5.2 39 40

Example: compute distance Example: expression ; compute D=sqrt(x^2+y^2) fld x ; load x fld st(0) ; duplicate x fmul ; x*x fld y ; load y fld st(0) ; duplicate y fmul ; y*y fadd ; x*x+y*y * fsqrt fst D 41 ; expression:vald = vala + (valb * valc). vala REAL8 1.5 valb REAL8 2.5 valc REAL8 3.0 vald REAL8? ; will be +6.0 fld vala ; ST(0) = vala fchs ; change sign of ST(0) fld valb ; load valb into ST(0) fmul valc ; ST(0) *= valc fadd ; ST(0) += ST(1) fstp vald ; store ST(0) to vald 42 Example: array sum N = 20 array REAL8 N DUP(1.0) sum REAL8 0.0 mov ecx, N mov esi, OFFSET array fldz ; ST0 = 0 lp: fadd REAL8 PTR [esi]; ST0 += *(esi) add esi, 8 ; move to next double loop lp fstp sum ; store result Comparisons 43 44

Comparisons Comparisons The above instructions change FPU s status register of FPU and the following instructions are used to transfer them to CPU. SAHF copies C 0 into carry, C 2 into parity and C 3 to zero. Since the sign and overflow flags are not set, use conditional jumps for unsigned integers (ja, jae, jb, jbe, je, jz). 45 46 Branching after FCOM Example: comparison Required steps: 1. Use the FSTSW instruction to move the FPU status word into AX. 2. Use the SAHF instruction to copy AH into the EFLAGS register. 3. Use JA, JB, etc to do the branching. Pentium Pro supports two new comparison instructions that directly modify CPU s FLAGS. FCOMI ST(0), src ; src=stn FCOMIP ST(0), src Example fcomi ST(0), ST(1) jnb Label1 47 x REAL8 1.0 y REAL8 2.0 ; if (x>y) return 1 else return 0 fld x ; ST0 = x fcomp y ; compare ST0 and y fstsw ax ; move C bits into FLAGS sahf jna else_part ; if x not above y,... then_part: mov eax, 1 jmp end_if else_part: mov eax, 0 end_if: 48

Example: comparison Comparing for equality x REAL8 1.0 y REAL8 2.0 ; if (x>y) return 1 else return 0 fld y ; ST0 = y fld x ; ST0 = x ST1 = y fcomi ST(0), ST(1) jna else_part ; if x not above y,... then_part: mov eax, 1 jmp end_if else_part: mov eax, 0 end_if: 49 Not to compare floating-point values directly because of precision limit. For example, sqrt(2.0)*sqrt(2.0)!= 2.0 fld two instruction FPU stack ST(0): +2.0000000E+000 fsqrt ST(0): +1.4142135+000 fmul ST(0), ST(0) ST(0): +2.0000000E+000 fsub two ST(0): +4.4408921E-016 50 Comparing for equality Example: quadratic formula Calculate the absolute value of the difference between two floating-point values epsilon REAL8 1.0E-12 val2 REAL8 0.0 val3 REAL8 1.001E-13 ; difference value ; value to compare ; considered equal to val2 ; if( val2 == val3 ), display "Values are equal". fld epsilon fld val2 fsub val3 fabs fcomi ST(0),ST(1) ja skip mwrite <"Values are equal",0dh,0ah> skip: 51 52

Example: quadratic formula Example: quadratic formula 53 54 Other instructions F2XM1 ; ST=2 ST(0) -1; ST in [-1,1] FYL2X ; ST=ST(1)*logST(1)*l 2 (ST(0)) FYL2XP1 ; ST=ST(1)*log 2 (ST(0)+1) FPTAN ; ST(0)=1;ST(1)=tan(ST) FPATAN ; ST=arctan(ST(1)/ST(0)) FSIN ; ST=sin(ST) in radius FCOS ; ST=sin(ST) in radius FSINCOS ; ST(0)=cos(ST);ST(1)=sin(ST) 55