assemblyx86floating-pointx87

How to compare 2 float in assembly


i´, completely novice in assembly. I want to compare two variables (float) and jump correctly to the right function; Its a Pong game Its written in FASM

 ;The right pad
  P0x dd  0.9  ;
  P0y dd  0.05 ;
  P1x dd  0.95 ;
  P1y dd  -0.25 ;

 ;The left pad
  P0x2 dd -0.9
  P0y2 dd  0.05
  P1x2 dd -0.95
  P1y2 dd - 0.25

B0x GLfloat  0.01 ; Its the ball coordinate
...
BvelX GLfloat 0.02 ;Its the velocity that the ball move in x
...

I want that: if the ball position is the same or more then the Pad position, then invert the velocity. What i do:

;right
 fld [B0x]
 fld [P0x]
 fcomip st1
 jge .changexEsq

 ;left
 fld [B0x]
 fld [P0x2]
 fcomip st1
 jle .changexDir

 ;Up
 fld [B0y]
 fld [TelaComecoY]
 fcomip st1
 jge .changeyBaixo

 ;Down
 fld [B0y]
 fld  [TelaFimY]
 fcomip st1
 jge .changeyBaixo

  ....
.changexEsq:
mov edi,-0.02
mov [BvelX],edi
jmp .main
    ret

.changexDir:
mov edi, 0.02
mov [BvelX],edi
jmp .main
    ret

.changeyBaixo:
mov edi,-0.02
mov [BvelY],edi
jmp .main
    ret

.changeyCima:
mov edi,-0.02
mov [BvelY],edi
jmp .main
    ret

But the comparation is doing nothing ! How could i compare those variables and jump correctly ?


Solution

  • FCOM and company (FCOMP, FCOMPP, FICOM, FICOMP) put results in the floating point status word, not in the main CPU flags register. You can use fstsw to store the floating point status word somewhere you can get at it and act on its contents (e.g., to store to AX, you use FSTSW AX).

    Note that this is somewhat roundabout. In some cases, you may want to consider treating the numbers as if they were integers -- IEEE 754 was carefully designed so that integer comparisons for ordering mostly yield correct results for floating point numbers.