By Frej <email@example.com>
Here I will (try to) explain to you how to use the FPU and what it is.
What is it? The FPU is a math processor inside your computers processor. Early computers had an external FPU processor, unlike today.
What can I use it to? You can for example take the square root of a number, multiply floating point numbers, divide, etc.
What do I have to know? You gotta have a little assembly knowledge to understand what I'm talking about, there are tons of asm tutorials out there, I would recommend 'arts of assembly'.
There are 8 places to store numbers in the FPU. Just like regular asm, where you had AX, BX, now you have ST(0) - ST(7). This would be the FPUs stack (only 8 values). You can put a 80 bit value in these stacks.
Why 80 bits?
1 bit 0 : sign bit
15 bits 1 : exponent
64 bits 16 : fraction (mantissa)
The sign bit stores the sign. The exponent shows how far the fraction should be shifted left or right. And finally the fraction, is the number that will move the digits left or right depending on the exponent.
Now, to initialize the FPU in assembly we use:
Next, we'll use our stack. Just like with a regular stack, you can PUSH and POP things to it. However, in FPU you push a number by using FLD and you pop a number by using FSTP.
Lets say that the variable Dummy would have the number 6 then if I'd do FLD Dummy the stack would look like this: For each time you do a FLD the stack is pushed "down" (ST(0) becomes ST(1) and so forth)
ST(0) = 6 ST(1) = ? ST(3) = ?
Of course, if ST(0) had already original had a number, such as 2, it would be put into ST(1). And the contents of ST(1), would be moved to ST(2) and so on.
If we wanted the variable Dummy, now, to equal ST(0), we would do a FSTP Dummy1. For each time you do a FSTP the stack is pushed "up" (same as for FLD but backwards). Got it?
Now let's get to some instructions:
FADD - This instruction adds two FPU stack elements together. Let's say you wanted to add ST(0) to ST(1):
FADD ST(0), ST(1)
The result is found in ST(0).
fSQRT - This instruction finds the root of If you want to take the root of ST(0) then you would do:
It takes the root of ST(0) and the result is left in ST(0)
;Variables A DD 1 B DD 2 C DD 3 ;Code FLD A ;ST(0) <- A FLD B ;ST(0) -> ST(1), ST(0) <- B FADD ST(0), ST(1) ;ST(0) = ST(0) + ST(1) FLD C ;ST(0) -> ST(1), ST(0) <- C FMUL ;ST(0) = ST(0) * ST(1) FSTP A ;A <- ST(0)
A list of FPU instruction that might come handy: