assemblyx86-16x87fpu

FBSTP stores wrong value


I am writing a program that calculates the arctan function.
The calc_value function works fine, but when I try to get the BCD value from fbstp, the instruction stores a weird value. fst on the other hand stores the right value.

segment data 
result:  resb 10
tmp_val: dd 000000000h
precs:   dq 03A83126Fh ; 0.001
arg:     dd 03F800000h ; 0.7

segment code
..start:

mov   ax, data
mov   ds, ax
finit

call  calc_value
fst   dword [tmp_val]
fbstp tword [result]

mov   ax, 4C00h
int   21h

After execution in memory:

at tmp_val: 0x3F490EDB (0.785383)
at result: 0x00000000000000000001

ps: I write this for DOS


Solution

  • The intel instruction set reference says this for fbstp:

    If the source value is a non-integral value, it is rounded to an integer value, according to rounding mode specified by the RC field of the FPU control word.

    As such, a result of 1 sounds right.