0

the relational operator "JNL" does not work together with "COMISD" when used with floating point numbers, it only works with integers when used with "CMP" operator. See C code below using COMISD with "JNL".

CODIGO EM C:


float total = 7.3;
float media = 2.2;
main() {
    if(!total < media){
        write(total);
    }
}

CODIGO EM ASM X64:

extern printf
extern scanf
section .data

fmt_f: db "%.2f", 0

total: dq 7.30
media: dq 2.20

section .text
global main

main:
mov qword [total], 0 ; (!total)
movsd xmm0, [total]
movsd xmm1, [media]
comisd xmm0, xmm1
jnl label0
sub  rsp, 8 
movsd xmm0, qword [total]
mov rdi,fmt_f
mov eax, 1
call printf
add  rsp, 8
label0:

mov rax,0
ret

If you change the operator "JNL" to "JNG", in the two cases where "if(!total < average)" and "if(total < average)" are true and printf is executed, which is not true. Could you help me understand what happens?

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
GreatField
  • 27
  • 4
  • The floating-point compare functions set the carry flag, not the sign flag, so you use the conditional jump instructions that would correspond to an *unsigned* compare. Thus the instruction you want instead of `JNL` is `JNB`. – Nate Eldredge Apr 20 '23 at 03:35
  • Though in this case I think you want `JB`, since you want to take the jump if `total < media` is true, in order to skip over the printf. (Also I think you meant to write `if(!(total < media))` in your C code, since `!` has higher precedence than `<`.) – Nate Eldredge Apr 20 '23 at 03:38
  • Thanks, now I understand why the comparison was failing. Thanks for reminding me about the precedence of "!" relative to "<". – GreatField Apr 20 '23 at 03:57

0 Answers0