Hi,
Version 4.50
Bug in "if statement" when operands type is "unsigned long"
and result is negative, if type is "signed long" or result >= 0 all right.
if ( ulong1 - ulong2 <= 0 ) {}
The assembler code:
1. "signed long" ok
if( ulong1 - ulong2 <= 0 )
8B 45 FC mov eax,dword ptr [ulong1]
2B 45 F8 sub eax,dword ptr [ulong2]
83 F8 00 cmp eax,0
7F 14 jg address
2. "unsigned long" error
if( ulong1 - ulong2 <= 0 )
8B 45 FC mov eax,dword ptr [ulong1]
2B 45 F8 sub eax,dword ptr [ulong2]
83 F8 00 cmp eax,0
75 14 jne address
but "sub eax,dword ptr [ulong2]" not set zf flag
if result is negative.
Please test.
The example generated error:
CCFLAGS= -Tx86-coff -Ot -Gd -Ze -Zx
#include <stdio.h>
int main()
{
signed long niTst1 = 0;
signed long niTst2 = 10;
unsigned long nuiTst1 = 0;
unsigned long nuiTst2 = 10;
printf("\tTest PellesC\n");
if ( niTst1 - niTst2 <= 0 )
{
printf( "\tAll is correct.\t%i <= 0 YES!\n", niTst1 - niTst2 );
}
if ( nuiTst1 - nuiTst2 <= 0 )
{
printf( "\tAll is correct.\t%i <= 0 YES!\n", nuiTst1 - nuiTst2 );
}
printf( "\tError!\t%i <= 0 NO?!\n", nuiTst1 - nuiTst2 );
getchar();
return 0;
}
Huh?! Exactly how did you came to the conclusion that an unsigned result can be negative?! It works just fine...
Quote from: "Pelle"Huh?! Exactly how did you came to the conclusion that an unsigned result can be negative?! It works just fine...
Oops! :oops: #-o
You are right, sorry.
This is not my code, it was "size_t" type in source, but not
obviously "unsigned long", it was confused me.
I must be more attentive before write to forum, sorry :oops:
Thanks for your work.