The easiest way to remove something from the FPU stack

Recently, I have had problems with FPU stack overflow. I managed to track it back to the buggy library function, which pops the garbage value onto the FPU stack every time it is called and never clears it.

Fortunately, this is easy to reproduce, and I know exactly what conditions cause it. I can drop the embedded ASM block into a routine that calls this procedure to pop the top value from the FPU stack ... except that I don’t quite know what to write. My ASM-fu is fair for middlin ', but not so strong.

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

+8
assembly x86 x87
source share
4 answers

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

However, perhaps the easiest solution is to use one of the pop-up data transfer operations, such as fstp . Usually you save the result in a memory area for later use, for example:

 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) as the destination, while preserving the memory requirement:

 fstp st(0) 

See here for a detailed instruction manual (especially this bit ).

+5
source share

For Delphi / BASM, in my opinion, the easiest way to replenish the FPU stack once:

 asm fstp st(0) end; 
+13
source share

If this is just the top value you want to get rid of:

 ffree st0 
+2
source share

just pop it from the stack with any (quick) instruction that appears. instruction set 8087

If this does not work, FUCOMPP appears twice.

0
source share

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


All Articles