Ver 3.0 Beta - Signed Compares

Beta Testing discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
Skyline
Posts: 267
Joined: 10 Jan 2006 09:35

Ver 3.0 Beta - Signed Compares

#1 Post by Skyline » 28 Apr 2010 06:56

Hi,

Got a bit of difficulty with signed integer compares.

Code: Select all

Program Compares

dim i1,idelta as integer        'signed 16bit integers

main:
    integer1 = -22
    idelta = 1
while 1
    if integer1 > 2000 then     'if (-22 > 2000) then ... this should be a false condition, but interpreted  as true condition
       idelta  = -1             'this statement should be skipped on false condition, but it is executed
    end if
    integer1 = imteger1 + idelta
wend
end.
Code generated seems incorrect:

Code: Select all

;a.mbas,10 :: 		if integer1 > 2000 then
0x021A	0x804021  	MOV	_integer1, W1
0x021C	0x207D00  	MOV	#2000, W0
0x021E	0xE10800  	CP	W1, W0
0x0220	0x360002  	BRA LEU	L__main7          'should be BRA LE ?
L__main10:
;a.mbas,11 :: 		idelta  = -1
0x0222	0x2FFFF0  	MOV	#65535, W0
0x0224	0x884000  	MOV	W0, _idelta
L__main7:
;a.mbas,13 :: 		integer1 = integer1 + idelta
Most situations code is generated ok.

Code: Select all

if integer1 > integer2   'code ok
if integer1 > 200        'code ok
if integer1 > 2000       'code not ok as above

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: Ver 3.0 Beta - Signed Compares

#2 Post by slavisa.zlatanovic » 29 Apr 2010 11:50

Hi!

This is a quick workaround. Just apply the integer() cast operator.

Code: Select all

if integer1 > integer(2000) then ...
I've informed our developers of this issue.

Best regards
Best regards
Slavisa

User avatar
srdjan
mikroElektronika team
Posts: 1552
Joined: 28 Dec 2005 12:47
Location: Serbia

Re: Ver 3.0 Beta - Signed Compares

#3 Post by srdjan » 29 Apr 2010 15:49

Hi,
We'll fix this. Thank you for reporting.
As a workaround you can use:

Code: Select all

if integer1 > integer(2000) then
However, you should take care when mixing signed and unsigned types.
Literal constant 2000 is interpreted as a word (unsigned) constant.
When mixing singed and unsigned operands, in order to calculate correctly, expression evaluation must be done
by extending both of these operands into next signed type larger than both operand types.
In this case it must be done on a longint level, leading to a greater code.
By implicitly casting constant into int type, expression will be evaluated on integer level (both operands are singned).

This leads us to the following conclusion, when comparing singed operand to a longword operand,
there is no larger type comprising both of them. The expression evaluation will lead to an error if longword operand exceeds longint range ;)

That is why standard pascal suggests strict type compatibility.

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 Beta Testing”