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

 Subject: Re: [stella] signed 8-bit comparisons? From: "Andrew Davie" Date: Thu, 4 Mar 2004 23:33:23 +1100
```Given two numbers A and B...

if you subtract B from A then the result is either positive, 0, or negative.

If 0, then A = B...  easy!
if negative, then B must have been bigger
if positive, then A must have been bigger

So what you're REALLY interested in is the SIGN of the result.

Now in two's complement, it's guaranteed that negative numbers have the top
bit set, and positive numbers and 0 have the top bit clear.  We can tell if
our result after "A-B" is negative simply by testing the top bit of the
result being "1".  But the 6502 automatically does this, the "N" (or
negative) flag is set/cleared after any addition/subtration and comparison.
The cmp instruction is effectively a subtract without keeping the result...
but you can quite happily use the sbc, too...

sec
lda A
sbc B
beq AequalsB
bpl AgreaterthanB
bmi AlessthanB

So there we go.  That should be fairly simple to understand, and implement
:)
Hope I got it right -- seems OK but I have a massive headache right now so

Cheers
A

----- Original Message -----
From: <KirkIsrael@xxxxxxxxxxxxx>
To: <stella@xxxxxxxxxxx>
Sent: Thursday, March 04, 2004 4:44 PM
Subject: [stella] signed 8-bit comparisons?

>
> For some reason, comparisons (and math in general) have
> always been my downfall in 6502/6507 land, and now I just
> realized I've been doing unsiged comparisons, and the signed
> comparison I need is going to be even tougher.
>
> I'm using a 6502 emulator to work this out. I think the
> follow would generally work if I wasn't dealing w/ any negative values:
> (var is the value, set to MIN or MAX if it exceeds that value)
>
> *= \$600
> var = \$0000
> MAX = 3
> MIN = -3
> LDA #-4 ; initial value
> STA var
>
> LDA #MAX
> cmp var
> BCS DoneSetToMax
> LDA #MAX
> STA var
> DoneSetToMax
>
>
> lda #MIN
> clc
> cmp var
> bcs DoneSetToMin
> LDA #MIN
> STA var
> DoneSetToMin
> BRK
>
> In case people are wondering, I'm finally (a week from
> my self-imposedish PC5 deadline) getting around to using "fractional"
> positioning for the ball vertical speed.  Initial results were
> fairly promising, with the ball vert. being the speed of the player
> who hit it, but I was getting some overflowish problem or
> something where the ball was wrapping past the floor to
> and coming out of the ceiling, which I think was partially
> tied in with it moving too fast.
>
> Thanks for any help w/ the signed comparison!
>
> -Kirk
>
> --
> KirkIsrael@xxxxxxxxxxxxx    http://kisrael.com
> "If you feel it, but it isn't right, don't do it and don't
>  believe it. We can be better than natural -- we're human."--Penn Jillette
>
>
> --------------------------------------------------------------------------
--------------------
> Archives (includes files) at http://www.biglist.com/lists/stella/archives/
> Unsub & more at http://www.biglist.com/lists/stella/
>
>

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

```