1

I'm working on a MASM32 project, using the x87 instruction set. I want to compare two floating point numbers and print out a message telling the result of the comparison. My program is:

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
include c:\masm32\include\masm32rt.inc
.data
    _a DD ?
    _b DD ?
    FP_11_ DD 11.
    FP_10_ DD 10.
.code
    start:
        MOV ebx, FP_10_
        MOV _a, ebx
        FLD _a
        FCOM FP_11_
        fstsw ax
        sahf
        JGE label5
        print "less", 13, 10, 0
        JMP label6
        label5:
        print "greater", 13, 10, 0
        label6:
        jmp end_program

        end_program:
            invoke ExitProcess, 0
    end start

The expected result is "less" but i'm getting "greater". So, my question is: how can I compare correctly two FP numbers by <=, >=, <, >, == and <> using the x87 instructions?.

Tomás Juárez
  • 1,517
  • 3
  • 21
  • 51
  • 4
    `JGE` is a signed condition that uses the `OF` flag which is specifically not set by `sahf`. You should use unsigned condition, e.g. `JAE`. Note this doesn't affect the comparison since that has already been performed by `fcom`. – Jester Nov 03 '17 at 19:22
  • http://www.ray.masmcode.com/tutorial/fpuchap7.htm has a tutorial on x87 comparison, using the old `fcom` or the more efficient and simpler `fcomi`. See also other links in https://stackoverflow.com/tags/x86/info. – Peter Cordes Nov 03 '17 at 20:40

0 Answers0