FCOM floating point comparison fails

前端 未结 1 1890
别跟我提以往
别跟我提以往 2021-01-25 02:38

I am just getting started with 32-bit assembly and I\'m quite confused. I have the following code:

.586
.MODEL FLAT

.STACK 4096

.DATA

.CODE
main PROC

finit
f         


        
相关标签:
1条回答
  • 2021-01-25 03:28

    Change JL to JB, since you can only do unsigned comparisons with the FPU flags.

    The reason is that 8087 has only 2 equivalent status bits at the same positions as 8086. Those are CF and ZF. When doing a signed comparison, the processor uses OF state from any preceding operation and the 8087 Busy State as the sign bit.

     8087:   [Busy] [ EQ ] [ Top of Stack Ptr ] [UND] [SOF] [ LT ]
                      C3                         C2     C1    C0    <-- C3..C0
     8086:   [Sign] [Zero] [ 0  ] [ AF ] [  0 ] [PF ] [ 1 ] [  C ]
    

    FCOMx Sets the Control bits C3,C2,C0 according to the conditions

     C3 = EQ == equal
     C2 = Undefined == Set if ST or Mem is undefined
     C1 = Marks either Underflow or Overflow of FP Stack (If Overflow Exception == TRUE)
     C0 = True, if ST(i) < ST(1)/Mem
    

    OTOH, the branch codes are implemented as

        JL:   SF != OF
        JB:   CF
        JBE:  CF | ZF
        JA:   !CF && !ZF
    

    Thus: Behaviourally C3/EQ == Zero and C0/LT == Carry

    References: Art of Assembly, FLAGS register, Conditional Jumps

    0 讨论(0)
提交回复
热议问题