This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 12. Floating Point Operations, Instruction Timing Background One of the goals of this lab is experiment with floating point operations on the x86. Hardware support for floating point operations is included on every modern CPU as they are a necessity for many applications. NOTE: The in-class lecture may or may not have covered the details of x86 floating point operations by the time you take this lab. However, do not stress over this - the lab looks at very simple uses of the floating-point instructions "All men are created equal" is a truth that Americans hold to be self-evident. Substitute "instructions" for "men" and "All instructions are created equal" is not self-evident -- in fact - the statement "All instructions are created equal, except some instructions are more equal than others±" is closer to the truth. In many cases, one reason for going to the trouble of writing something in assembly code is for performance -- and in order to write high performance code you need to understand which instructions or instruction sequences are fast or slow. This lab looks at a method for timing x86 instruction sequences to help you understand which instructions are slow or fast in terms of execution speed. Objectives Understand: A. Simple uses of the x86 floating point instructions. B. Differences in execution time of various instruction types. Pre-Lab The basics of floating point operations are covered very well in the Irvine textbook, Chapter 15. Be sure to read this chapter before attempting this lab. Also read the online lecture notes concerning the IEEE floating-point number format. ±Read George Orwell's Animal Farm if you get a chance.2 Lab A. x86 Floating Point Operation The program below (fpexam.asm) illustrates some simple uses of the x86 floating point instructions. .model small .586 .stack 200h .data fres dd ? ;; floating point result op1 dd 2.789 op2 dd 12.1 op3 dd 3.141592654 op4 dd 5.0 op5 dd 1.570796327 rstring db "Float Result is: ",0 ares db 30 dup (?) .code ;; ftoa procedure found in 'float.lib' extrn ftoa:proc, writestring:proc, crlf:proc main proc mov ax,@data mov ds,ax finit fld op1 ;; load into st0 fld op2 ;; load into st1 A1: fdiv ;; st1 = st1/st0, pop stack so st1 goes into st0 fstp fres ;; save single result as single precision ;; pop stack also call fprint fld op3 B1: fmul op4 ;; st0 = st0 * operand, no stack pop fstp fres call fprint fld op3 C1: fsqrt ;; square root of pi fstp fres call fprint fld op3 D1: fsin ; sin (pi) fstp fres call fprint fld op3 E1: fcos ; cos (pi) fstp fres call fprint ;; Mov ax, 4c00h ;exit to DOS Int 21h main endp3 fprint proc mov dx,offset rstring call writestring mov ax,word ptr fres mov dx, word ptr fres+2 mov cx, 10 ;; 10 digits of precision mov di,offset ares call ftoa mov dx,offset ares call writestring call crlf ret fprint endp end main The program does the following floating-point operations: divide, multiply, square root, sine and cosine. The operands come for the single precision floating point values labeled as op1, op2, op3, and op4. The result of each operation is printed to the screen using the fprint procedure. How does this program work? 1. The finit instruction initializes the Floating Point Unit (FPU). The FPU has 8 registers named ST0-ST7 and which are arranged as a stack. Register ST0 is the top of the stack. 2. The instructions "fld op1" and "fld op2" loads operand op1 into register ST0, and op2 into register ST1. Note that op1 and op2 are stored in memory as 32-bit values (single precision floating point numbers). In order for MASM to generate a floating point format instead of using an integer, you MUST use a decimal point in the number when specifying its value (ie. op1 dd 2.0 -- use a decimal point even if the fraction is zero). 3. The fdiv instruction (no operand format) does a divide operation ST1 = ST1/ST0, and then pops the stack. This means that ST1 goes into ST0, and that ST1 becomes empty. After the FDIV instruction, the result is sitting in register ST0. 4. The "fstp fres" instruction stores the register value ST0 to the memory operand fres as a 32-bit single precision number. After the store operation, the stack is popped so register ST0 is now 'empty'. 5. The procedure fprint is used to print the value in the memory location fres to the screen as a floating-point number. It uses an external procedure called ftoa that is found in the float.lib library to covert the 32-bit FP number to an ASCIIZ representation. The ftoa procedure expects CX to contain the number of digits of precision needed; DX:AX to contain the 32-bit single precision FP number, and DS:DI to point to a memory buffer where the null-terminated string is to be placed (the 'ares' buffer is used for this). After ftoa is called, the Irvine library procedure writestring is used to print the string to the console. Note that this program requires two libraries to be linked to it, so the library needs to be specified as "Irvine+float" when the linker prompts for a library name. 6. The "fmul op4" operation (single operand format) does a multiply operation ST0 = ST0*op4. Note ST0 is equal to 'op3' because of the 'fld op3' instruction that immediately preceded the fmul operation. The result of the fmul operation is then stored into fres via 'fstp fres' and printed to the console by the fprint procedure.4 7. The fsqrt instruction performs the square root operation ST0 = sqrt(ST0). The value of ST0 was set to op3 by the previous "fld op3" instruction. 8. The fsin instruction performs the sine operation ST0 = sine(ST0) (ST0 value in radians). The value of ST0 was set to op3 by the previous "fld op3" instruction. 9. The fcos instruction performs the cosine operation ST0 = cosine(ST0) (ST0 value in radians). The value of ST0 was set to op3 by the previous "fld op3" instruction. Lab Question 1: Assemble this program and execute it. A. For each of the calculations, list the numerical arguments and verify that the correct result is obtained. Do you notice any round-off error in any of the computations? B. Change the program such that the FSQRT function is passed a negative value. Single step through the program and record the 32-bit value that is passed back (wait until the result is stored in memory at location fres, then


View Full Document

MSU ECE 3724 - Floating Point Operations Instruction Timing

Documents in this Course
Timers

Timers

38 pages

TEST 4

TEST 4

9 pages

Flags

Flags

6 pages

Timers

Timers

6 pages

Timers

Timers

54 pages

TEST2

TEST2

8 pages

Load more
Download Floating Point Operations Instruction Timing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Floating Point Operations Instruction Timing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Floating Point Operations Instruction Timing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?