Author Topic: Doubt in comparison routine  (Read 236 times)

0 Members and 1 Guest are viewing this topic.

Offline aless2056

  • Contributor
  • Posts: 23
  • Country: br
Doubt in comparison routine
« on: May 06, 2021, 01:49:06 am »
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
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 1557
  • Country: us
Re: Doubt in comparison routine
« Reply #1 on: May 06, 2021, 02:24:41 am »
Quote
If A = B, ok don't do anything in C

If A+5 > B, increase C

If A-5 < B, decrease C

Are these cases supposed to be mutually exclusive because if, for instance, B = 10, A = 9 then what are you supposed to do with C?


« Last Edit: May 06, 2021, 02:30:00 am by ledtester »
 

Offline DrG

  • Frequent Contributor
  • **
  • Posts: 922
  • Country: us
Re: Doubt in comparison routine
« Reply #2 on: May 06, 2021, 02:34:42 am »
Quote
If A = B, ok don't do anything in C

If A+5 > B, increase C

If A-5 < B, decrease C

Note these cases are not mutually exclusive because if, for instance, B = 10, A = 9 then what are you supposed to do with C?

I'm reading it such the solution in C would be something like:

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

I see that he wants asm code but I am too lazy to do someone else's homework, at least not until I see what they have tried first - actually whether it is homework or not.
- Invest in science - it pays big dividends. -
 

Offline aless2056

  • Contributor
  • Posts: 23
  • Country: br
Re: Doubt in comparison routine
« Reply #3 on: May 06, 2021, 02:38:16 am »
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
 

Offline DrG

  • Frequent Contributor
  • **
  • Posts: 922
  • Country: us
Re: Doubt in comparison routine
« Reply #4 on: May 06, 2021, 02:51:46 am »
So what code do you have so far?
- Invest in science - it pays big dividends. -
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 1557
  • Country: us
Re: Doubt in comparison routine
« Reply #5 on: May 06, 2021, 02:56:04 am »
So it seems you want:

- if B <= A-5, increment C
- if B >= A+5, decrement C
- otherwise do nothing

Here are some general ideas:

- using the SUBWF you can subtract two values, e.g. SUBWF f,d which computes W - f.
- if the Z bit is set then the result of the subtraction was 0
- if the high bit of the result is set then the result was negative, else it was positive
- if the carry bit is set then W <= f, otherwise W > f (as 8-bit unsigned numbers)

See page 220 of http://ww1.microchip.com/downloads/en/DeviceDoc/40001262F.pdf
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf