# Doubt in comparison routine

#### aless2056

##### Junior Member level 2
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

So how do you compare two numbers ? Methods ?

Clue, subtract or add, test sign bit, or test accumulator......

A + 5 > B, so subtract B from this inequality

A + 5 - B > 0, what operations do you have to do here ?

Hint also keep track of what the carry and overflow bits are doing when doing subtract/adds

Print out instruction set, shortform, and go over it several times while watching Star trek,
it will start firing neurons......familiarity breeds understanding.

Regards, Dana.

#### KlausST

##### Super Moderator
Staff member
Hi,

Most "compare" act like a "subtract" .... thus be sure to use the correct variable type and size.

But there is an issue:
(I alwas recommend to use a paper and a pencil to test your function. I doubt you did this)
You say"
If A = B, ok don't do anything in C

If A+5 > B, increase C

If A-5 < B, decrease C
Lets say A = 3, B = 3;
Compare 1) Then A = B is TRUE --> do nothing
But
Compare 2) A + 5 > B --> 3 + 5 > 3 --> 8 > 3 is TRUE, too.
And
Compare 3) A - 5 < B --> 3 - 5 < 3 --> -2 < 3 is TRUE, too.
(Mind the negative value of '-2' here. A "signed compare" will be TRUE, while an "unsigned compare" will treat -2 as 254 and thus will be FALSE)

So all three compares are TRUE at the same time.
Now it depends on how you process the three compares (if one compare was "true", do you process the following compares or not?)

and how you define "do nothing" ( is "increase C" followed by a "decrease C" a "do nothing" or rather a "do both"? Just focussed on the value of "C" it does not matter, but it matters in processing time and if there anything else is done - also have in mind that maybe half a year later you add some code... )

Generally you never do a compare that - when TRUE - does nothing....you don't need to tell a microcontroller "to do nothing".
You rather do a compare that when "true" does something .... (and it automatically does nothing when FALSE)

Klaus

Your program flow looks something like -

As you can see routine leaves C undisturbed if it does not meet the two inequality tests,
so you could alter the above literal flow by eliminating the A = B test, or leave it in for

Note the return block implies return the value of C on the stack. Thats if you write the
routine as a subroutine rather than inline code.

Regards, Dana.

Last edited:

#### KlausST

##### Super Moderator
Staff member
Hi,

There are many possible program flows..
so I´m not sure whether the folowing "comparisons" are made unconditionally or only are processed when the upper is FALSE / NO (ELSE-path)
***

regarding the shown flow chart I have two things that could be improved:
* I´d use the same "symbol" for a "compare". Usually a diamond symbol. (the firts decision is shown as octogon)
* the last decision needs a path "No"

Klaus

Hi,

There are many possible program flows..
so I´m not sure whether the folowing "comparisons" are made unconditionally or only are processed when the upper is FALSE / NO (ELSE-path)
***

regarding the shown flow chart I have two things that could be improved:
* I´d use the same "symbol" for a "compare". Usually a diamond symbol. (the firts decision is shown as octogon)
* the last decision needs a path "No"

Klaus
Actually I have seen many diagrams where the "NO" is implied. If its not "YES" then it must be "NO".
Depends on how ones mind works and method learned.

Flow depends also on desired performance. Dont do the inequality tests if you dont have to. Waste
of MIPS..... order depends on most likely outcome, do those tests first. Or if you have real time issues

Regards, Dana.

#### betwixt

##### Super Moderator
Staff member
The question concerns assembly commands so 'high level' math isn't relevant.

To compare two values for equality:
Put one value in W with the (movlw or movf ,w) instruction,
Put the other value in a register (it probably is already!)
Use 'subwf' to subtract the value in W from the value in the register.
If the Zero flag is then set in the STATUS register, the values are the same.

To check If A+5 > B, increase C
Put 'A' in the W register
Use 'addlw 5' to add literal value 5 to it, you now have A+5 in W
Put B in a register
Use 'subwf' to subtract B from W
If the Carry flag in STATUS is set, B was larger than A+5 so use 'incf ,f' to add 1 to C

I'll leave you to work out the other part but basically follow the same method as above. Beware of values that would give negative results!

Brian.

#### KlausST

##### Super Moderator
Staff member
Hi,

This is what I had in mind:
(without texts)

Klaus

I would add the texts and which path is "NO" and which is "YES", otherwise
great looking flow chart. Note any line can be a "YES" or "NO", presentation
and simplification determine which fits the flow better.

In both charts there is additional consideration, if interrupts modifiy A or B
there is possibility of a double modification to C in your chart.

Regards, Dana.

#### KlausST

##### Super Moderator
Staff member
Hi,

update:

It´s simply MS Word.. not the best tool to do flowcharts.