Re: [stella] CMP Wizard

Subject: Re: [stella] CMP Wizard
From: "Roger Williams" <mer02@xxxxxxxxxxxxx>
Date: Sun, 28 Oct 2001 07:07:28 -0800
> ;           CMP BRANCH WIZARD
> ;---------------------------------------
> ;A  =  M - use BEQ ;       N=0,*Z=1, C=1
> ;A  >  M - use BCS ;       N=0, Z=0,*C=1
> ;A  <  M - use BMI ;      *N=1, Z=0, C=0
> ;A  >= M - use BPL*;      *N=0,      C=1
> ;
> ; * No need to set carry ahead of time


.A <= M - use BCC
.A <> M - use BNE

Using BPL and BMI can be dangerous, as they only work
if the result is a 2's complement byte.  For example, if you
are using unsigned math the following test fails:

    LDA #213
    STA Temp
    LDA #3
    CMP Temp
    BMI Wherever

By your chart this should branch, since 3 < 213.  But it
will actually pass through, because the S flag is set
according to the high bit of the result (indicating a
negative 2's complement result).  The 6502 really
thinks of 213 as -43, and the result of the subtraction
as 3-(-43)=+46.

In signed math 255+1=0, $FF+$01=$00
In unsigned math, 127+1=-128, $7F+$01=$80

Addition and subtraction (including carry) work
the same whichever rollover point you assume,
but the Sign bit assumes the latter form where
no positive number is ever greater than 127.

Since Stella's horizontal and vertical resolution are
both greater than 1257 it's important to keep this
in mind.  When you are working with unsigned
values that can exceed 127 it's safer to use the
carry, which functions the same whether you assume
signed or unsigned values.

--Roger Williams

Archives (includes files) at
Unsub & more at

Current Thread