As part of a compiler project I have to write GNU assembler code for x86 to compare floating point values. I have tried to find resources on how to do this online and from what I understand it works like this:
Assuming the two values I want to compare are the only values on the floating point stack, then the fcomi instruction will compare the values and set the CPU-flags so that the je, jne, jl, … instructions can be used.
I’m asking because this only works sometimes. For example:
.section .data
msg: .ascii "Hallo\n\0"
f1: .float 10.0
f2: .float 9.0
.globl main
.type main, @function
main:
flds f1
flds f2
fcomi
jg leb
pushl $msg
call printf
addl $4, %esp
leb:
pushl $0
call exit
will not print “Hallo” even though I think it should, and if you switch f1 and f2 it still won’t which is a logical contradiction. je and jne however seem to work fine.
What am I doing wrong?
PS: does the fcomip pop only one value or does it pop both?
TL:DR: Use above / below conditions (like for unsigned integer) to test the result of compares.
For various historical reasons (mapping from FP status word to FLAGS via
fcom/fstsw/sahfwhichfcomi(new in PPro) matches), FP compares set CF, not OF / SF. See also http://www.ray.masmcode.com/tutorial/fpuchap7.htmModern SSE/SSE2 scalar compares into FLAGS follow this as well, with [u]
comiss/sd. (Unlike SIMD compares, which have a predicate as part of the instruction, as an immediate, since they only produce a single all-zeros / all-ones result for each element, not a set of FLAGS.)This is all coming from Volume 2 of Intel 64 and IA-32 Architectures Software Developer’s Manuals.
FCOMIsets only some of the flags thatCMPdoes. Your code has%st(0) == 9and%st(1) == 10. (Since it’s a stack they’re loaded onto), referring to the table on page 3-348 in Volume 2A you can see that this is the case "ST0 < ST(i)", so it will clear ZF and PF and set CF. Meanwhile on pg. 3-544 Vol. 2A you can read thatJGmeans "Jump short if greater (ZF=0 and SF=OF)". In other words it’s testing the sign, overflow and zero flags, butFCOMIdoesn’t set sign or overflow!Depending on which conditions you wish to jump, you should look at the possible comparison results and decide when you want to jump.
I’ve made this small table to make it easier to figure out:
In other words the condition codes match those for using unsigned comparisons. The same goes if you’re using
FMOVcc.If either (or both) operand to
fcomiis NaN, it setsZF=1 PF=1 CF=1. (FP compares have 4 possible results:>,<,==, or unordered). If you care what your code does with NaNs, you may need an extrajporjnp. But not always: for example,jais only true if CF=0 and ZF=0, so it will be not-taken in the unordered case. If you want the unordered case to take the same execution path as below or equal, thenjais all you need.Here you should use
JAif you want it to print (ie.if (!(f2 > f1)) { puts("hello"); }) andJBEif you don’t (corresponds toif (!(f2 <= f1)) { puts("hello"); }). (Note this might be a little confusing due to the fact that we only print if we don’t jump).Regarding your second question: by default
fcomidoesn’t pop anything. You want its close cousinfcomipwhich pops%st0. You should always clear the fpu register stack after usage, so all in all your program ends up like this assuming you want the message printed: