-1

Hey guys i have my finals coming up ,currently studying assembly and i'm struggling with one thing that i cant really find an answer too. Lets say i have 2 Double Values x1=-0.5 ,x2=1.0 i want to compare x1 and x2, but sadly one of them is signed (x1) and the other is unsigned, I want to use a Condition ,lets say the #C equivalent of

                       "While(x1<=x2)"

so iam using the following code:

_find_delta2 PROC NEAR
    PUSH BP
    MOV BP,SP
    MOV AX,0
    L1:
    MOV AX,0
    FLD CNT
    FMUL QWORD PTR [BP+22];ST0=I*H
    FADD QWORD PTR [BP+6]; STO0=I*H+X1
    FST TMP ; TMP=I*H+X1
    FCOMP QWORD PTR [BP+14] ; ST0 COMPRAE WITH X2
    FSTSW AX
    SAHF
    JNL DONE

Sadly,in the first Loop it appears that the condition is met despite x1=-0.5, x2=1.0 How could i solve this issue? Thanks!

Soske
  • 541
  • 1
  • 4
  • 10
  • I have tagged CPU architectures. Please correct the tags if they are wrong. Also, please make a [mcve] as your code snippet cannot be reproduced as is. At least add code that sets up the x87 stack and `BP+14` with the desired values. – fuz Jul 08 '20 at 14:14
  • Hey fuz, i have added the segment of the code, i was also wanted a general explanation on how i can Compare an unsigned and signed double numbers – Soske Jul 08 '20 at 14:22
  • I think you should use `jae`. While `jge` is for signed integer comparison, [`sahf` doesn't set](https://ulukai.org/ecm/insref.htm#insSAHF) the Overflow Flag which is used by `jge`. For integer comparison (`cmp`) `jae` means unsigned but FPU comparison always is signed. – ecm Jul 08 '20 at 14:22
  • Hey ecm, FSTSW AX -> SAHF sets the AX flags same as the numeric flags, is there a diffrent command i can use to help with this issue? – Soske Jul 08 '20 at 14:24
  • 1
    What are "AX flags"? `fstsw ax` followed by `sahf` is correct, you just need `jae` (or `jnb`) after. – ecm Jul 08 '20 at 14:30
  • 7
    they look like they are floating point numbers yes? which means they are both always signed. – old_timer Jul 08 '20 at 14:45
  • @Soska Please also provide the definition of `CNT`. – fuz Jul 08 '20 at 14:48
  • Hey guys, CNT is a counter which goes from 0 until x1 is biger or equal to x2, i have provided a picture of the code also the stack is [BP+4]=FUNCTION POINTER, [BP+6] IS X1, [BP+14] IS X2 , [BP+22] IS N, [BP+30] IS EPS, This are all Double values . LINK: https://imgur.com/a/bsqOBmo – Soske Jul 08 '20 at 15:01
  • 1
    @Soske Please do not post pictures of code. Post your code as text instead. And again: I'm more interested in a [mcve] than your full code. Most of your logic is irrelevant for your question, but the values of the numbers you compare are important! That said, it's like the signed/unsigned comparison issue user ecm already mentioned. – fuz Jul 08 '20 at 16:46

1 Answers1

2

...but sadly one of them is signed (x1) and the other is unsigned

There's really no unsigned to consider when dealing with floating point numbers


While(x1<=x2)

After loading ST(0) with x1 you compare x1 with x2 using:

FCOMP QWORD PTR [BP+14]

If ST(0) is less than SRC, the C0 flag is 1
If ST(0) is equal to SRC, the C3 flag is 1

After executing:

FSTSW AX
SAHF

the C0 flag will have been copied to the CF carry flag and
the C3 flag will have been copied to the ZF zero flag.

From this you see that your <= condition ("less or equal") is TRUE if either or both these flags are set. Use the jbe instruction ("jump on below or equal").

    fcomp qword ptr [bp+14]
    fstsw ax
    sahf
    jbe TRUE
FALSE:
    ... x1 > x2
TRUE:
    ... x1 <= x2
Sep Roland
  • 33,889
  • 7
  • 43
  • 76