12

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?

1
  • For the record, x87 is obsolete. Modern code will normally use the XMM registers even for scalar FP math, with instructions like addss (scalar single-precision) or addsd (scalar double). Look at compiler-generated code for any FP function for examples. Aug 17, 2018 at 13:40

4 Answers 4

13

For Delphi/BASM, in my view the simplest way to pop the FPU stack once is:

asm
 fstp st(0)
end;
1
  • 5
    This is actually the correct answer. Most x87 implementations are optimized fot this case and will not even perform the transfer, just pop the value off the stack.
    – IneQuation
    Jan 14, 2012 at 22:49
10

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).

2
  • 1
    Note that ' fstp st(0) ' requires one clock. It doesn't get any better than that. Apr 27, 2018 at 15:44
  • @AlbertvanderHorst: Superscalar CPUs can easily do better than that. fstp/fst with a register destination actually has one per 0.5c throughput (i.e. 2 per clock) on Haswell / Skylake. agner.org/optimize. Register renaming takes care of the dependency through the top-of-stack pointer. Aug 17, 2018 at 13:43
4

If st0 is the only x87 register in use, you can empty it with:

ffree st0

But this is different from a normal pop if there are multiple stack registers in use, because it doesn't adjust the top-of-stack pointer (TOP field in the x87 status word).

See the registers chapter of the Simply FPU x87 tutorial.

st1 would still be st1 after freeing st0 instead of popping, so this is normally not what you want and has no significant advantage over fstp st0.

9
  • This doesn't work. st0 is tagged as empty, but that doesn't affect anything except the fpu tag word. Apr 27, 2018 at 15:42
  • Not sure what else you would want affected, as it it the definition of what's in the register. Apr 30, 2018 at 22:22
  • 1
    Of course you want the stack pointer changed too. I've experimentally verified (by small code words un my Forth, ciforth) that your solution doesn't work as intended. See the answer given by PhiS which is correct. May 9, 2018 at 12:29
  • Why would I want the sp changed? The stack is empty, it doesn't matter where you start filling it. May 10, 2018 at 1:33
  • 1
    @JensBjörnhager: your update was technically correct, but still easy to misinterpret for people that didn't know enough about x87 to even think of the only-1-register-in-use condition being relevant / important. I basically rewrote the answer other than the code block to something I think is worth an upvote. Aug 17, 2018 at 13:37
2

just pop it off the stack with any (fast) instruction that pops. 8087 instruction set

if that doesn't work, FUCOMPP pops twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.