开发者

x87 FPOP and FCOM instructions - how do these work?

I've been tasked with writing a simple application in mixed C/ASM that has to use math co开发者_Go百科processor.

There's the function cylinder(float x, float y, float z) that returns 1 if the given point is within the cylinder (the cylinder has a base at x=0,y=0, radius = 5 and height = 10), and 0 if it isn't.

So, looks simple. Check whether z is within <0,10>, and then check if x^2 + y^2 < 25.

But my knowledge about x87 is nil.

There's everything I've written.

_cylinder PROC

push ebp
mov ebp, esp
sub esp,8 ; I can't use .data in the application, so I reserve some space on the stack for numbers 10 and 25
mov [esp],10
mov [esp+4],25

finit
fldz
fld [ebp+8]

    ;here i get stuck 

add esp, 8
pop ebp
_cylinder ENDP

So I got stuck. So, I try to find what instructions could I use in the application. And there I get stuck, because every tutorial/instruction list i find on the net is written so badly I can barely understand anything.

The question is, what happens when I pop something from the math coprocessor? Where can I find the popped value? How does it converts from 80-bit value to 32 bit one (if it does, of course) Another question is, how does the FCOM (FCOMP for pop variant) works? It compares what to what ( st0 to st1 or st1 to st0?), and where can I see whether the value is smaller/equal/bigger?

Thanks for any help!


Floating point comparisons are kind of a pain. You can do the comparison on the FPU, but before you can do anything based on that, you have to transfer the floating point status word to the CPU, test for the flags you care about, and then react based on that.

Just for example, your initial comparison that z>=0.0 would look something like this:

fldz
fcomp z
fnstsw ax
test ah, 041h; I *think* I've got the right flags there...
jp good
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜