# Doubt in comparison routine

#### massanetabr

Joined Jun 3, 2019
44
Doubt in comparison routine

Guys, I'm being beaten up to do the following asm routine for pic 16F877.

I have A and B, both range from 0 to 100 and I would like to do the following routine

If A = B, ok don't do anything in C

If A+5 > B, increase C

If A-5 < B, decrease C

#### click_here

Joined Sep 22, 2020
548
Sure, show us the code that you are not getting to work

#### massanetabr

Joined Jun 3, 2019
44
I only want to increase or decrease C if the difference between A and B is 5 for one side or for the other side.

A = 50
...
B = 44, increment C
B = 45, increment C
B = 46, do nothing
...
B = 56, decrease C
B = 55, decrease C
B = 54, do nothing

#### cmartinez

Joined Jan 17, 2007
8,182
Have you tried to write the code yet? If so, it would be real helpful for us if you posted it so we could take a look

#### click_here

Joined Sep 22, 2020
548
The basic way of doing it is to subtract the number that you are comparing with and check the status register.

#### atferrari

Joined Jan 6, 2004
4,752
I always work out the flow diagram and once satisfied with it, I write the code. This last becomes a simple clerical task.

#### Robin66

Joined Jan 5, 2016
275
The pseudo code is:

if(A+5 > B)
C++;
else if(A-5 < B)
C--;
end

This is enough for you to attempt to write the asm

#### AlbertHall

Joined Jun 4, 2014
12,331
if(A+5 > B)
C++;
else if(A-5 < B)
C--;
Using 'else if(B + 5 > A)' will avoid the overflow if A is less than 5.

#### click_here

Joined Sep 22, 2020
548
I think that the problem that the op was having is that there is no < or > in the PIC assembly language.

To work out if it's < or > the user needs to subtract one number from the other and see if the zero/carry bit is set in the status register

#### cmartinez

Joined Jan 17, 2007
8,182
The OP has probably already seen through that and solved the problem. It would be real nice if he were to report back and post his solution out of simple courtesy, though.