Re: [stella] signed 8-bit comparisons?

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

>> 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

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

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

    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.


Archives (includes files) at
Unsub & more at

Current Thread