Mario Augusto
Mario Augusto

Reputation: 105

FPU first command execution

I ve this code, there we have a distance (D) and an angle (A), the functions must returns X, x=cosine(a)*d and Y, y=sine(a)*d

.data    
n180 word 180
d word 60
a word 10
x word 0
y word 0

.code
    fild word ptr a
    fild word ptr n180
    fdiv
    fldpi
    fmul
    fsincos
    fild word ptr d
    fmul
    fistp word ptr x
    fwait
    fxch
    fild word ptr d
    fmul
    fistp word ptr y

at first time i run the program (using a=10 and d=60) i get X=59 e Y=-32768 here X is right, i get it before use FXCH but Y is wrong. if i run the program again then i get X=59 and Y=10 now it's ok

Why first time I run I get an error with FXCH?

Upvotes: 0

Views: 87

Answers (1)

Chris Hall
Chris Hall

Reputation: 1772

For completeness...

... the no-operand FADD, FDIV, FDIVR, FMUL, FSUB and FSUBR all pop the right-hand argument ST(0) (so the left-hand argument ST(1) is replaced by the result, and then the stack is popped and the result becomes ST(0)).

... so the FXCH is not required.

... indeed, without the FXCH your sequence of operations leaves the FPU register stack in the same state as it started in (assuming it does not overflow at any point), which is generally a Good Thing.

I note that both FADD and FADDP etc. (with no operands) are just shorthand for FADDP ST(1), ST(0) (Intel ordering) and that is not the same as FADD ST(1), ST(0). I note also that the Intel manual prefers the FADDP etc. mnemonics for the no-operand form.

Upvotes: 1

Related Questions