Search code examples
assemblyx86x87

assembler how does the jump if above works


We are comparing two values 1 and pi

then i use 'jump if above' or jump if greater:

we have:

1 - pi < 0 

so we don't jump.

it only occurs when condition :

x - value > 0 is true, right ? and how does the overflow flag work in jg ?

fcomp ; st0:1 , st1:pi

 fstsw ax   
 sahf 

ja nie_odejmuj ;

Do we jump ?

using FPU


Solution

  • ja needs CF==0 and ZF==0. It doesn't check OF. In x87 flags this means C0==0 and C3==0. It's exactly condition that ST(0) > ST(1) for the comparing using FCOM, FCOMP, etc.

    You shouldn't be confused with formal condition difference (as ja is for comparing unsigned numbers) because x87 condition flags differ and are passed here using special transport. It could be even possible to see absolutely mismatching conditions, but Intel tried to provide at least far similarity between conditions and checking instructions.

    OTOH, jg, yes, checks very other conditions with bits that aren't passed using fstsw+sahf. So it isn't applicable here even if is named the same as needed condition.

    This is legacy issue, as well as tons of other x86 strange solutions. (30+ years of developing without redesign from scratch doesn't give a consistent result. OTOH, all they are well documented so you should just follow the known recipes.)

    UPDATE: btw, you can alternatively skip sahf and check the flags directly:

    fstsw ax
    test ax, 4100h
    jz nie_odejmuj
    

    It is longer in code but expresses the method more clearly.