I can't believe I did that... ...and yes, I did get my own little reminder message. -----Original Message----- From: pic microcontroller discussion list [mailto:PICLIST@MITVMA.MIT.EDU] On Behalf Of j newton Sent: Friday, September 29, 2000 14:52 To: PICLIST@MITVMA.MIT.EDU Subject: [PICLIST] Code challenge: fr > w or fr <= w Importance: Low Ok, I have a code challenge... On a 16C5x (ok, actually an SX), I have a value in W and a value in a register (call it fr), and I need to subtract W + 1 from the value of fr and not put the result in fr. I.e. I need to find out if fr > w or maybe fr <= w with out destroying fr. Munching W is, of course, ok. The key stumbling blocks are: A) There is no inc or dec for W (actually on the SX there is, but I don't want to use it for other reasons) B) I can't decf fr,w (on the theory that (fr - 1) - w is the same as fr - (w + 1)) because it would overwrite the value I have in w. I could use a temp register but I don't have any left, besides, that's no fun. C) The subwf fr,w instruction will subtract the value of w from fr and I could look at both the carry and zero flags to find the answer because: 1. If fr > w then the Carry flag will be set but that flag would also be set if they were equal. We must also know that the Zero flag is not set. This is a logical AND of the Carry and Zero flags. 2. If fr <= w then either the Zero flag (and the Carry flag) will be set (they were equal) or the Carry flag (and the Zero flag) will be cleared (unequal but Y < X). We must make both tests in a logic OR system. but this is a lot of hopping, skipping and jumping and I'd like to avoid that. I've found a way to write these two comparisons in just three instructions. Not using subwf fr, w. But these two cases are the only comparison out of all the other possibilities that don't end up with that same pattern of subwf fr, w followed by sc or snc and I'd like to make them more consistent. e.g. all comparisons basically come down to: ;get the values swapped around into a standard fr < w or fr >= w format SUBWF fr, w if comparison = Lt then BTFSC 3, 0 else BTFSS 3, 0 endif Which would allow me to write a smaller macro to do general purpose conditionals. I could get around A if someone has a cute way to increment or decrement W, or a cute way to skip on two flags (Zero AND Carry, Zero OR Carry) See http://www.sxlist.com/lib/flow/compcon_sx for more information. --- James Newton mailto:jamesnewton@geocities.com 1-619-652-0593 -- http://www.piclist.com hint: PICList Posts must start with ONE topic: "[PIC]:" PIC only "[EE]:" engineering "[OT]:" off topic "[AD]:" ad's -- http://www.piclist.com hint: PICList Posts must start with ONE topic: "[PIC]:" PIC only "[EE]:" engineering "[OT]:" off topic "[AD]:" ad's