Re: [cc65] Problem with evaluating if with negative numbers

From: Gábor Lénárt <lgb1lgb.hu>
Date: 2012-02-03 14:42:19
Hi,

On Fri, Feb 03, 2012 at 01:24:03PM +0000, S M wrote:
> if (bugs[n].mx < 0 ) {...
> 
> but it didn't work so I had to change it to:
> 
> if ( (bugs[n].mx >=128) && (bugs[1].mx <=255) ) {...

Even with this, "if (bugs[n].mx&128) { ..." seems to be a better work-around
for me - well at least if bugs[n].mx can't be more than 255, but if
it's a "char" it can't be a problem, I guess. 

> 
> Why the first line doesn't work if .mx is not unsigned ?

But is it signed? Did you specified "signed" in the declaration of your
variable?

What is the exact declaration of that .mx? Please provide the source line.

Afaik (from cc65 dox, command switches):

  "-j, --signed-chars

  Using this option, you can make the default characters signed. Since the
  6502 has no provisions for sign extending characters (which is needed on
  almost any load operation), this will make the code larger and slower. A
  better way is to declare characters explicitly as "signed" if needed. You
  can also use #pragma signed-chars for better control of this option."

So, I guess, if you define your variable as only "char" (without "signed" or
"unsigned") it will be "unsigned" by default with CC65 because of the reason
documentation also describes.  You can make it "signed", if you specify that
in your declaration or if you use that command switch or the mentioned
#pragma.  As far as I can imagine you defined your variable as "char mx;"
and you expected that it will be signed by default (which may be not true
#for cc65).  Try this: "signed char mx;"

If I am correct ...; if not, please fix me, somebody :)

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Fri Feb 3 14:42:34 2012

This archive was generated by hypermail 2.1.8 : 2012-02-03 14:42:37 CET