Introduction to floating point operations

zhaozj2021-02-16  35

Introduction to floating point operations

For those who are accustomed to C, those of those who are convenient for C., understand the floor floating point operations seems to have no significance, now how many people are concerned about the so-called underlying? Right of AFOS? For the floating point operations, the floating point operation is a very important part of the program, because we may face some slightly complicated operations, if you are Die-Hard's ASM support, you don't want to use C to solve the problem, you can definitely Imagine the pain in ASM with integration to seek Sin (2.3) pain, in fact, the microcomputer has long prepared a solution: That is a floating point operation. But now about floating point operations less, I believe many people and I still don't master this powerful technology. Well, let's learn to learn.

(1) THIS PART Mainly Froe Bill's Article Before this, let's take a few terms: fpu-> floating point unit, floating point arithmetic part BCD-> binary Coded Decimal compressed twenty-entered number, use 4 Points to represent numbers 0 ~ 9, one byte represents two decimal numbers, such as 01111001 Represents 89 Scientific Counting: This is a scientific ~~~~ Specific meaning in the middle school or elementary school mathematics text D :)

Floating point operations use three different data: 1) integer, divided into words, short integer, long integer 2) real number (SINGLE REAL) and double Double Real 3) The compressed twenty-input number (BCD) is below the number of bits (BITS) and can be expressed.

Type Length Range ----------------------------------------------- Word Integer 16 Bit -32768 To 32768 Short Integer 32 Bit -2.14E9 to 2.14E9 Long Integer 64 Bit -9.22E18 TO 9.22E18 Single Real 32 Bit 1.18E-38 TO 3.40E38 Double Real 64 Bit 2.23E-308 TO 1.79E308 Extended Real 80 Bit 3.37E-1932 To 1.18E4932 Packed BCD 80 bit -1e18 to 1e18

Double precision and expansion accuracy indicate that the scope is much larger than the general application!

1) Integer, stored in the form of complement, the positive complement is its own, the negative completion of the completion of its absolute value is changed, and the following is an example of actual storage: 0024 VAR1 DW 24 FFFE VAR2 DW -2 000004d2 var3 DD 1234 ffffff85 var 4 DD-123 0000000000002694VAR5 DQ 9876 fffffffffffffebfvar6 DQ -321

2) BCD number in the FPU is exactly the width of the floating point register in the FPU, in its format as follows: BIT 79_____71_______________________________________0 symbol - 18 twenty into the number -------- Look below Example: 00000000000000012345 VAR1 DT 12345 80000000000000000100 VAR2 DT -1003) Floating point, this complex point, there are three formats

Single precision: _31_30 ________ 23_22___________0 Symbol Index Active

Double precision: _63_62 __________ 52_51_________________0 Symbol Index Active

Extended-precision numbers: the number of effective exponent sign _79_78____________64_63___________________0 example: C377999A var1 dd -247.6 40000000 var2 dd 2.0 486F4200 var3 real4 2.45e 5 4059100000000000 var4 dq 100.25 3F543BF727136A40 var5 real8 0.00123

C377999A VAR1 DD-247.6 40000000 VAR2 DD 2.0 486F4200 VAR3 Real4 2.45E 5 40591000000000 VAR4 DQ 100.25 3F543BF727136A40 VAR5 Real8 0.001235 400487F34D6A161E4F76 VAR6 Real10 33.9876

DD and Real4 can define single-precision floating point numbers in ASM, 4 BYTES DQ and Real 8 can define double precision floating point numbers in ASM, 8 BYTES DT and Real10 can define extended precision floating point in ASM, 10 Bytes (2) floating-point components

The FPU is divided into two parts: the control unit and the arithmetic unit, the control unit is mainly for the CPU, and the calculation unit is responsible for the specific calculation. The FPU is floating-point component includes 8 universal registers, 5 error pointer registers and three controls. register.

1) 8 universal registers each 80 bit, form a register stack, all of which are saved in the register stack, where data is all 80-bit extension accuracy format, even BCD, integer, single precision, and double precision, etc. Be automatically converted to an 80-bit extension accuracy format when loading registers, pay attention to st (0), then ST (1) ... ST (i), ST (i) is As related to the top of the stack.

And the stack is very similar, but the width is 80bit, the image is as follows: ______________________________________________________________________________________________________________ | | ST (i) | | ___________________________________________________________

Status Register -> SW _M_____D________10___9____8___7_________5_________________________0__ | B | C3 | TOP | C2 | C1 | C0 | ES | | PE | UE | OE | ZE | DE | IE | | ____ | ____ | ____ | ____ | ____ | ____ | ____ | ____ | ____ | ____ | ____ | ____ | ____ | ____ |

B: The floating point part is busy c0-c3 indicates the result of the floating point operation. Different instructions have different meanings TOP indicator top, usually any of the following positions (PE, UE, OE, ZE, DE, or IE) below 0 ES. Set the PE Precision Fault UE Number When it is too small to indicate that the overflow OE can not represent that the number too large overflow ZE is in addition to 0 fault DE indicates that there is at least one operator unregified IE invalid error, indicating the stack overflow or underflow, invalid Operation number, etc.

Control Register: _15____________10___9____8___7_________5______________________0__ | | IC | RC | PC | | PM | UM | OM | ZM | DM | IM | | ____ | ____ | ____ | ___ | __ | _ | __ | __ | ____ | ____ | ____ | ____ | ____ | ____ | ____ |

IC infinity control, pair 486, has been invalid RC rounding control 00 = DPRK closestly or even rounded 01 = Packed negative infinity direction round 10 = DPRED infinity direction round 11 = super 0 direction cut off PC accuracy control 00 = single Accuracy 01 = Reserved 10 = Double Precision 11 = Extended Accuracy PM ~ IM Shield Status Register Low 5 Indicated Errors. To 1 shield.

Mark Register: Each bit represents a state of a corresponding stack register, the specific meaning is as follows: 15___________________________________0 | Tag7 | ............................ ....... | TAG1 | | _____ | ________________________________________________ | meaning: 00 = effective 01 = zero 10 = invalid or infinity 11 = empty

(3) Floating point instruction system and mASM floating point program design

In fact, the most important and more difficult to find information has been described in (1) and (2) section, below is for integrity considerations, if you are the first time you contact floating point instructions, see the summary below. Different One aspect of this article not involved is about floating point treatment abnormalities, because involving more content such as protection mode and interruption, task switching, and SEH, I believe that it will only be more confusing, and I don't seem to be unable. Take these questions completely clear, usually we almost don't need to know. Let us first see the main content. About floating point programming is a big topic, I am just an outline, briefly, MASM32V7 (/ V6) Methods, because 486 The CPU built into a floating point component so it can be used directly in the program. The following is a small example:

__Masmstd eQU 1 .386p .Model flat, stdcall option casemap: none; case sensitive include c: / hd/hd.h include c: /hd/mac.h

;; -------------- .data Num1 DQ 12345 NUM2 DQ 98765 RES DD 0 .DATA? BUF DB 200 DUP (?)

;; ----------------------------------------- .code __start: finit; initialization Floating-point components Fild Num1; load Num1 Fild Num2; load Num2 Fmul; Perform multiplication fist res; Storage Invoke WSPrintf, AddR BUF, CText ("THE RESULT IS:% LD"), RES INVOKE STDOUT, ADDR BUF; display, Note that the console is displayed, compiling / subsystem: console invoke stdin, addr buf, 20 invoke exitprocess, 0 end __start

How do you want to use an instruction, you have to see the operations you have to do and the algorithm to be executed. Note that in the FPU internal register is always represented by the expansion accuracy, the integer operation is finally stored In this way, the correct result can be done automatically by the FPU.

Floating point instruction system is divided into five categories: data transfer class, arithmetic computing class, transcendence class, comparison class, environment and system control class. I don't want to list all functions and usage because this will be a waste of labor. I typedly use pinyin !: D) The specific reference is met to see the article, and I can't help you.

1) Data transfer class, mainly including this type of instruction is mainly from memory to floating point register stack data, the general destination address is always stack top ST (0), using the debugger you can clear this. Note P The end of the end is the outlet after the previous operation is completed, that is, the original ST (1) content is now the content of ST (0). Notes this, you can easily design flexible and changed programs. into: FLD Push real onto stack FILD Convert two's complement integer to real and push FBLD Convert BCD to real and push to stack storage: FST store floating-point number from stack FSTP Convert top of stack to integer FIST FISTP Convert top of stack to integer FBSTP Store BCD To Integer And Pop Stique Exchange: FXCH Exchange Top Two Stack Elements constant loading: FLD1 load constant 1.0 FLDZ load constant 0.0 FLDPI load constant pi (= 3.1415926 .... Precision is sufficient, rest assured) FLDL2E loading Constant log (2) E FLDL2T load constant log (2) 10 FLDLG2 load constant log (10) 2 FLDLN2 load constant log (e) 2 I don't want to list all floating point instructions, because no If necessary! Many information have these instruction formats, floating point instructions are started with f, and LD represents LOAD, ILD represents an integer's LOAD, BL is a 20-entered load, so that it is easy, many instruction functions It can be seen according to the instruction.

2) arithmetic class addition: FADD / FADDP Add / add and pop FIADD Integer add subtraction: FSUB / FSUBP Subtract / subtract and pop FSUBR / FSUBRP Subtract / subtract and pop with reversed operands FISUB Integer subtract FISUBR Integer subtract / subtract with reversed operands multiplication: FMUL / FMULP multiply / multiply and pop FIMUL Integer multiply division: FDIV / FDIVP divide / divide and pop FIDIV Integer divide FDIVR / FDIVRP divide / divide and pop with reversed operands FIDIVR integer divide with reversed operands other: FABS Calculate absolute value FCHS Change sign FRNDINT Round to integer FSQRT Calculate square root FSCALE Scale top of stack by power of 2 FXTRACT Separate exponent and mantissa FPREM Calculate partial remainder FPREM1 Calculate partial remainder in IEEE format ST if the latter instruction is not an operand with its default operand (0) and ST (1 )

3) beyond the trigonometric function based FSIN Calculate sine FCOS Calculate cosine FSINCOS Calculate quick sine and cosine FPTAN Calculate partial tangent FPATAN Calculate partial arctangent Log class FYL2X Calculate y times log base 2 of x FYL2XP1 Calculate y times log base 2 of (x 1 ) F2XM1 Calculate (2 ^ x) -1

4) comparing the class FCOM Compare FCOMP Compare and pop FICOM Integer compare FTST Integer compare and pop FUCOM Unordered compare FUCOMP Unordered compare and pop FXAM Set condition code bits for value at top of stack FSTSW Store status word

According to the result setting, C0 to C3, the C0 to C3 is not specifically described above, and C1 is used to determine the overflow or underflow, and C0 corresponds to the CF inside EFLAGS, and the effect is basically consistent, and C2 corresponds to PF. C3 is equivalent to ZF, you may see why fstsw ax sahf jz label is stored as the following instructions, because the status word is stored in EFLAGS, C0 is exactly in the CF bit, C3 is just placed in the ZF bit .5) Environment And System Control FLDCW Load Control Word Fstcw Store Control Word Fstsw Store Status Word Fldenv Load Environment Block Fstenv Store Environment Block Fsave Save Coprocessor State FrStor Restore Coprocessor State

FINIT Initialize coprocessor FCLEX Clear exception flags FINCSTP Increment stack pointer FDECSTP Decrement stack pointer FFREE Mark element as free FNOP No operation FWAIT Wait until floating-point instruction complete

I really don't want to say, because these instructions have a detailed description in the fphelp.hlp file below the Masm32v7 Help directory, of course, there are still many other instruction formats, I am listed is for complete Sex. There is still a more difficult problem is the display of floating point, and Windows has no ready-made function call. Wsprintf can only display integers, but there are many library support, such as floating point development packs on the LYB homepage, of course, you are finishing These things can also be written.

With regard to the debugging of floating point procedures, it is recommended to use Softice because TRW does not support the display of floating point stacks. Now there is an FPU plugin on the Internet. It can be partially solved, but it is not easy to use. Everything looks at your own choice.

转载请注明原文地址:https://www.9cbs.com/read-27907.html

New Post(0)