Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

What is the best way to determine if a register is a specific value?

Status
Not open for further replies.

Steve311

Member
Hi All, I'm using a PIC16F684 in assembly language. What is the most efficient way to determine if a register has a specific value? Would I have to test each bit individually?

Or is there some unique way of subtraction with evaluating the carry flag?

For example, How would I determine if a register reads exactly 0x06?

Thanks all!
 
Subtract 0x06 from the register and then check the Zero flag.
 
Last edited:
You can "and" with the 1's complement (comf) of the number you want and see if it returns a zero (Z). That is the same as subtracting, but might be easier to visualize.

John
 
Last edited:
Usually something like:
Code:
movlw 0x06
subwf value,w
btfsc STATUS,Z
goto ValueIs6

Bean
 
You can also test for greater and lower,
Code:
		movlw	6
		subwf	Var,w
		btfsc	STATUS,Z
		goto	IsEqualTo6
		btfsc	STATUS,C
		goto	IsGreaterThan6
; if it gets to here it is less than 6

Mike.
 
I have used XOR as well, but don't understand why 'and' won't work. 0x6 anded with 0x9 gives zero. I just ran it in simulation too, and Z was set appropriately.

John

hi John,

The bit patterns for 0x06 and 0x09 are 0000,0110 and 0000,1001 so AND'ing these bit patterns will give 0000,0000

Is this what you are asking.?
BTW the 'comma's are just for easy reading
 
I was too target fixated. As I fully woke up and ran the simulation more, the correctness of diver300's comment became obvious. Hence my edit of post#7.

I realized that 0x6 and 0x9 gave zero and affect the zero bit, which was what led to the error in post #3.

John

Eric: Maybe to avoid confusion, you should restore post#7, and I will add the edit below it? Otherwise, your quote of a now absent post could raise questions. Whatever you think is least confusing is fine with me.
 
Last edited:
You can also test for greater and lower,

If I may supplement Mike's (Pommie's) fine example... there are also many different ways to test for a range of values;

Code:
;
;  40 ≤ ddram ≤ 55 ?
;
        movf    ddram,W         ; wreg = ddram = 0..127           |B0
        addlw   -56             ; ddram ≥ 56 (C=1)?               |B0
        movf    ddram,W         ; wreg = ddram = 0..127           |B0
        skpc                    ; yes, skip (lcd), else           |B0
        sublw   39              ; ddram ≤ 39 (C=1)?               |B0
        skpnc                   ; led addr? yes, skip, else       |B0
        bra     notinrange      ; branch (not in range)           |B0
inrange

;
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top