Search code examples
c++assemblyx86fpu

Assembly: JA and JB work incorrectly


Since my main OS is linux and have project on visual studio, I decided to use online compilers to achieve it. I found this which was suggested by many. So here is my code:

#include <iostream>

using namespace std;
int main(void) {
float a = 1;
float b = 20.2;
float res = 0;
float res1 = 0;

_asm { 

    FLD a
    FCOM b
    JA midi
    JMP modi           

    midi:
    FST res
    JMP OUT

    modi:
    FST res1
    JMP OUT


}
    OUT:
cout << "res = " << res << endl;
cout << "res1 = " << res1 << endl;
return 0;
}

My goal is simple, if a is greater that b than put a in res, otherwise in res1. But it seems like whatever I have in variable a it always jumps on midi and result is always res = whatever is in a. Hope you can help. Sorry if my questions is silly, I've just started to learn assembly. thanks :)

P.S
same thing goes with JB but the opposite. It always prints res1 = whatever is in b.


Solution

  • From this page:

    FCOM compares ST0 with the given operand, and sets the FPU flags accordingly.

    But your JA midi is testing the CPU flags.

    It goes on:

    FCOMI and FCOMIP work like the corresponding forms of FCOM and FCOMP, but write their results directly to the CPU flags register rather than the FPU status word, so they can be immediately followed by conditional jump or conditional move instructions.