## Re: [stella] signed 8-bit comparisons?

 Subject: Re: [stella] signed 8-bit comparisons? From: Bill Heineman Date: Thu, 04 Mar 2004 21:54:28 -0800
```on 3/4/04 1:28 PM, KirkIsrael@xxxxxxxxxxxxx at KirkIsrael@xxxxxxxxxxxxx
wrote:

>> Hi there,
>>
>>> I think what you want is:
>>>     LDA    var
>>>     CMP    #MAX
>>>     BMI    ltMAX
>>>     LDA    #MAX
>>> ltMAX
>>>     CMP    #MIN
>>>     BPL    gtMIN
>>>     LDA    #MIN
>>> gtMIN
>>>     STA    var

Or you can cheat...

Since you're comparing a signed number to a constant... Do this...

LDA Value
EOR #\$80
CMP #CONSTANT+\$80
BCC LESS
BCS GREATEREQUAL

By adding 0x80 to the number, you convert 0-0x7F to 0x80-0xFF and 0x80-0xFF
become 0x00-0x7F. Now, this turns your signed range into an UNSIGNED range.
By the performing the UNSIGNED CMP instruction, you can use BCC and BCS with
100% accuracy.

You see, there is a problem with overflow for signed numbers in 6502. To get
around it, there is the overflow flag. The proper way to do signed compares
was...

LDA Value
CMP #\$80
BVC NoOverflow
BMI GreaterEqual
BPL Less

NoOverflow
BPL GreaterEqual
BMI Less

That code was a joke.

Now, if you can guarantee that there is no overflow (Both values are like
sign and the subtraction won't cross the \$80 boundary, then you can ditch
the BVC (Normal case) and just use BPL for Greaterequ and BMI for less (Most
people do this anyways.

I use the EOR #\$80 trick for many years and it's the fastest way to compare
a value to a constant with no fancy stuff.

Burger

----------------------------------------------------------------------------------------------
Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://www.biglist.com/lists/stella/

```