To "sublw" and discard if zero or negative.

Status
Not open for further replies.

RobertD

New Member
Hi folks, I need to subtract two registers. They both vary up and down, but I'm only interested when one is up above the other, how do I set up this...?

I'm thinking of something like this:

subwf ADDR,W ;sub ADDR from W put result in W
btf STATUS,DC ; to see if it's negative
btf STATUS,Z ;to see if it's zero
goto next
 
Hi Robert,

May I suggest that the MPLAB Simulator is perfect for testing out those tricky binary math codes? It's definately worth learning early on Sir.

I can't tell you off the top of my head how to do it because I always have to simulate it myself (grin).

Have fun, Mike
 
The subtract instructions on a pic work in a rather non intuative way. Subwf does not subtract the file from W but the other way around (I see you know this already). Same with sublw and so the mnemonic is even wrong and should be Subtract W from Literal.

Anyway, with this in mind, if W contains (random number) 48 and ADDR contains 47 then 47-48 is calculated. This results in -1 and so the Carry flag is set (not the Digit Carry as you have above). You also want to include the case where both are the same and so you need an additional check.

Something like,
Code:
	subwf	ADDR,W
	btfsc	STATUS,Z	;were they equal
	goto	WasLTE		;yes
	btfsc	STATUS,C	;was it less than
	goto	WasLTE		;yes
	goto	next

WasLTE	;will only get to here is ADDR was less than or equal to W

Mike.
 
Indeed thanks, you have every right to laugh at my lack of programming skills. You are forgiven...
 
If you think I was laughing at you then I apologize for giving you the wrong impression.

Binary math is my weak point and I've spent many many hours using the Simulator to get the simplest bit of code working. It's a great tool.

Mike
 
It's OK I don't mind if you did, it was a joke anyway.

I have a good grasp of logic, and math, but do lack the practical experience, and the patience I have to admit. I find programming tedious and one of those things that will fall by the way side when we design programs to write programs so that writing a program can be done by anyone with an idea. I also know that a lot of these routines have been done already and if I knew how to use a more advanced tool like a linker, I could just pull the routines and link them, and then I would not have to go through the bean counting.

I used the debugger to step through the program and follow the progress, but there are still a lot of things in there I have yet to discover. One of these being the linker.
 
It sounds like you're wanting to check comparisons?, greater than, less than, etc.

If you check the PICList there's an entire section on doing this, with both 8 and 16 bit examples.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…