Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to remove something from the FPU stack

Tags:

x86

assembly

x87

I've been having some trouble lately with FPU stack overflows. I managed to track it back to a buggy library function that pushes a garbage value onto the FPU stack every time it's called and never cleans it up.

Fortunately, this is easily reproducible and I know exactly what conditions cause it. I can drop a block of inline ASM into the routine that calls this routine to pop the top value back off the FPU stack... except I don't quite know what to write. My ASM-fu is fair to middlin', but not that strong.

So what's the simplest way to get rid of the top value on the FPU stack in x86 assembly, assuming it's garbage data and I don't care about the value?

like image 430
Mason Wheeler Avatar asked Jan 23 '11 04:01

Mason Wheeler


People also ask

What is FPU stack?

The most important registers in the FPU are a stack of eight extended floating point numbers. One of the registers is designated as the top of the stack (ST0) and all other registers are relative to it: ST1, ST2. Data is pushed on the stack with a load. Data is taken off the stack with a store.

What is Fstp Assembly?

The FSTP instruction performs the same operation as the FST instruction and then pops the register stack. To pop the register stack, the processor marks the ST(0) register as empty and increments the stack pointer (TOP) by 1. The FSTP instruction can also store values in memory in extended-real format.


1 Answers

If you know how much you need to adjust the stack by, you can use fincstp. You also want to ffree the registers that you increment over.

However, probably the simplest solution is to use one of the popping data transfer operations like fstp. Normally you would store the result into an area of memory for later use, something like:

mem_area: defs 10         ; ten bytes for 80 bits
          fstp mem_area   ; pop it

But, if you know you just want to throw away the value, you can use st(0) itself as the destination, saving the memory requirement:

fstp st(0)

See here for a detailed guide on the instructions (particularly this bit).

like image 149
paxdiablo Avatar answered Sep 17 '22 17:09

paxdiablo