Question

I have some calculation in asm in loop, it must be before Sum will be 3, but loop work all time and never ending. Here is mu code:

int main() {

    float Sum=0;
    int i=0;
    int A=5;
    int B=180;
    int C=3;

_asm{
   finit
m1:inc i
   fldpi
   fimul A  
   fimul i
   fidiv B
   fsin
   fadd Sum 
   fstp Sum
   fild Sum
   ficom C      
   fstsw AX     
   sahf         
   jg   m1
}
}
Was it helpful?

Solution

The FPU's condition flags have completely different meanings than the condition flags in the EFLAGS register.

If Sum is greater than C, then the ficom will set the FPU condition flags to "C3=0, C2=0, C1=0". When loaded into the CPU's EFLAGS register (via. fstsw and sahf) this becomes "ZF=0, PF=0, CF=0". The jg instruction will branch if "ZF=0" and "SF=0" - it won't test the PF flag or the CF flag at all, but will test the irrelevant SF flag.

Given that you're comparing integers anyway; you should probably just do:

mov eax,[Sum]
cmp [C],eax
jg m1

The alternative is to test the flags in AX directly. For example:

ficom C
fstsw AX
test AX,0x4500    ;Are C3, C2 and C0 clear?
je m1             ; yes, Sum must be greater than C
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top