  ### Author Topic: How to read a register if it is zero?  (Read 5194 times)

0 Members and 1 Guest are viewing this topic.

#### _pike

• Full Member
•     • • Posts: 182 ##### How to read a register if it is zero?
« on: March 16, 2011, 00:30:14 AM »
Hello!.I would like to ask how can i understand that a register has been changed and is is not zero?

example:
Code: [Select]
`temp1 equ 0x21h          ;temp1 = '00000000'movlw b'00100010'movwf temp1                 ;temp1 = '00100010'`
Now how can i compare it, and if all bits are 0 then goto 1 and if any bit has been changed goto 2

Thanks a lot Panagiotis
« Last Edit: March 16, 2011, 09:28:11 AM by _pike »

#### kam

• Hero Member
•     • • Posts: 1849 ##### Re: How to read a register if it is zero?
« Reply #1 on: March 17, 2011, 09:47:39 AM »
First, you need to have another register with the "normal value". I usual use the same name of the register, with the extension _old. Example:

Code: [Select]
`temp1 equ 0x21h          ;temp1 = '00000000'temp1_Old equ 0x22h          ;temp1 = '00000000'movlw b'00100010'movwf temp1                 ;temp1 = '00100010'`
Here is where temp1 is changed, and you test it to see if is zero or changed:

Code: [Select]
`movf temp1,w                 ;Load temp1 to W. This will also set ZERO flag if temp1 is zerobtfsc zero                        ;Test ZERO flag (located in status register)goto 1                              ; If ZERO is set (all bits are 0)then goto 1                                         ;ELSEsubwf temp1_Old,w     ;Subtract the content of temp1 (loaded on W) to the content of temp1_Oldbtfss zero                       ; Results (stored in W) are tested.goto 2                             ;If temp1 NOT EQUAL to temp1_Old, the subtraction will NOT be zero. So GOTO 2 `

This is how you test 2 numbers. Subtraction is the method to see if they are changed. If are same, the ZERO flag is set after the subtraction (2-2=0). If not same, then the ZERO flag is 0.

« Last Edit: March 17, 2011, 09:49:57 AM by kam »

#### kam

• Hero Member
•     • • Posts: 1849 ##### Re: How to read a register if it is zero?
« Reply #2 on: March 17, 2011, 10:07:48 AM »
How to check if only the 3rd bit is change...

This is a technique that you will certainly need to use one day. Suppose that you have again temp1 and temp1_old, and you want to test if the 3rd bit of temp1 is changed. This is what you do:

movf temp1,w
xorwf temp1_old,w

The XOR instruction has this ability to identify ONLY the changed bits. If both bits are 0 or 1, then the XOR function returns 0. But if bits are different (1-0 or 0-1), XOR returns 1. Here is an example:

temp1=00001100
temp1_old=10001000
The XORWF will return

00001100
10001000 XOR
-----------------------
10000100

Now, you can simply check the 3d bit of the XOR result"

Code: [Select]
`movf temp1,wxorwf temp1_old,wmovwf temp2            ;another temporary register that you store the XOR resultbtfsc temp2,3goto Bit_3_Is_Changed`
And here is another way that has the same result but without using this lousy temp2 register:

Code: [Select]
`movf temp1,wxorwf temp1_old,wandlw '00001000'btfss zerogoto Bit_3_Is_Changed`
This technique uses the AND logic function, which can isolate a single bit from a number. Check this:

00111110
00001000 AND
-----------------------
00001000

and this:
00110110
00001000 AND
-----------------------
00000000

The result of the AND logic function is affected ONLY by the 3rd bit. So, if the result is 0, this means that bit 3 was zero...

#### _pike

•     •  