Free x90 FPU Stack (ia32)

At my university, we just met the FPU IA32 x87. But we were not told how to clear the FPU-Stack from the elements that were no longer required.

Imagine that we are doing a simple calculation, for example (5.6 * 2.4) + (3.9 * 10.3).

.data value1: .float 5.6 value2: .float 2.4 value3: .float 3.8 value4: .float 10.3 output: .string "The result is: %f\n" .text .global main main: fld value1 # Load / Push 5.6 into FPU fmul value2 # Multiply FPU top (5.6) with 2.4 fld value3 # Load / Push 3.8 into FPU fmul value4 # Multiply the top element of the FPU Stacks with 10.3 fadd %st(1) # Add the value under the top element to the top elements value .output: # Reserve memory for a float (64 Bit) subl $8, %esp # Pop the FPU top element to the program Stack fstpl (%esp) # Push the string to the stack pushl $output # Call printf function with the both parameters above call printf # Free the programs stack from the parameters for printf addl $12, %esp .exit: movl $1, %eax int $0x80 

The problem is this: After you select the top FPU that contains the result of the calculation. How to free the FPU stack from the recently remaining top element that contains the result (5.6 * 2.4).

The only way I can imagine is to free a few more program stacks and push items from the FPU stack until all unnecessary items are removed.

Is there a way to directly manipulate the top pointer?

+6
source share
4 answers

To make sure you don’t have gadgets on the stack, you need to use the FADDP and FMULP and similar instructions.

+6
source

If someone like me comes here looking for the best way to clear the stack, I found this simple solution the best:

 fstp ST(0) ; just pops top of the stack 
+5
source

emms can also be used to designate each member of the fp stack as free. This has the advantage over finite that it does not change any flags in the fp control word or state (exception mask, etc.)

+2
source

There are several instructions that can perform operations like the ones you are looking for. FDECSTP reduces the stack pointer (without doing anything else), FFREE notes that the slot is empty (without touching the stack pointer). The solution mentioned above with FADDP or FMULP is often more enjoyable.

You should consider downloading Intel architecture guides . They contain a complete set of instructions for the Intel processor family.

+1
source

Source: https://habr.com/ru/post/957780/


All Articles