Search code examples
assemblycomparenasmintelfpu

Compare two numbers in Intel x86 assembly (nasm)


I have the following assembly code:

%include 'rw32.inc'

[segment .data use32]  

a dd 2.0
b dd 1.0  
[segment .code use32]

prologue                ; macro -- inicialization
  fld dword [a]
  fld dword [b]
  fcom
  jc greater
  jmp less
greater:
  fxch
less:
  call WriteDouble
  call WriteNewLine
  epilogue                ; macro -- termination

rw32.inc is utility library, which includes program initialization and termination and other functions. WriteDouble - prints out st0 on the screen WriteNewLine - just inserts newline.

and I want this to compare numbers 'a' and 'b' and print the greater one. My logic is this: get both numbers on stack. By fcom set flags. If carry flag is 1, number 'a' is greater, so it needs to be switched with 'b', so it is on top of the stack. Otherwise 'b' is greater and just print out result.

But it seems that program never jumps to label 'greater'. How can this be fixed, please? Thank you.


Solution

  • FPU math should never afect to CPU flags like carry and zero!

    So copy flags from FPU to CPU flag register after fcomp instruction and than check the carry and zero flags like:

    fld qword ptr [a]
    fcomp qword ptr [b]
    wait                 ;wait FPU
    fstsw ax             ;copy FPU flags to ax
    sahf                 ;copy ax to CPU flags
    jbe LessOrEqu        ;do less or equal
    ...