On Thu, 30 Sep 1999, Tony Nixon wrote:
{Quote hidden}> Date: Thu, 30 Sep 1999 10:59:28 +1000
> From: Tony Nixon <
Tony.Nixon
KILLspamENG.MONASH.EDU.AU>
> Reply-To: pic microcontroller discussion list <
.....PICLISTKILLspam
.....MITVMA.MIT.EDU>
> To:
EraseMEPICLISTspam_OUT
TakeThisOuTMITVMA.MIT.EDU
> Subject: Compares
>
> Hi all,
>
> I don't know why, but I "still" get confused when doing things like this
> in assembler.
>
> if RAMx > RAMy then
>
> I just can't seem to keep in my mind what is needed especially when a
> SUBLW instruction is involved.
>
> So, to make it easier on myself and others, I've posted a small text
> file which lists probably all the variations for basic number
> comparisons in 8 and 16 bit format. There are two sets, one give a true
> branch and the other gives a false branch.
>
> If anyones interested, the link is
>
>
http://www.picnpoke.com/projects/compares.txt
And then I wrote:
{Quote hidden}> XH:XL YH:YL
>
> movf yl,w
> subwf xl,w
> movf yh,w
> skpz
> incf yh,w
> subwf xh,w
> skpnc
> goto x_is_greater_than_or_equal_to_y
>
> Similar sequences exist for the other cases.
>
And then Tony asked about the Z and C bits after the low-order byte
subtraction. I inadvertantly deleted the message so I don't know what you
exactly asked Tony. However, I think I can answer the generic question of
doing comparisons.
First, don't worry about the size of the variables being compared (because
the result is the same for 8, 16, 24, etc. bits).
Whenever you subtract two numbers, A-B you get this result:
The carry will be set if A is greater than or equal to B. Or equivalently,
the carry will be cleared if B is less than A.
So if I want this kind of result:
if(A >=B )
foo();
Then I'd write in PIC assembly:
movf B,w
subwf A,w
skpnc
call foo
The other three cases yield:
if(A > B) foo();
movf A,w
subwf B,w
skpc
call foo
if(B > A) foo();
movf B,w
subwf A,w
skpc
call foo
if(B >= A) foo();
movf A,w
subwf B,w
skpnc
call foo
The main thing to notice is that the Z bit is not necessary for any
comparisons.
Now the 6 word subtraction:
movf AL,w
subwf BL,w
movf AH,w
skpc
incf AH,w
subwf BH,w
Will correctly propogate the carry. So for the 4 cases, A>B, A>=B, B>A,
and B>=A we only need to either subtract A from B or B from A and then we
need to only check to see if the carry bit is set or is it cleared. The
information in the z bit is not used. Furthermore, we don't need to check
the carry bit from the low order subtraction because the carry information
is automagically propogated to the high order. Is this clearer?
Scott