How to test a BYTE in Assembler?

Thread Starter

ProgKees

Joined Nov 10, 2009
2
Hi there,

I am new in assembler. With the help of the K8048 kit of Velleman and the examples they deliver with it, I am slowly getting the hang of it.
One thing I can't figure out, and that is how to test a complete byte.
With the BTFSC and BTFSS commands it can be done, but to check the whole byte you must perform this test 8 times.
There must be an easier way (I think and hope)
Does anyone know?

Thnx, Kees
 

RiJoRI

Joined Aug 15, 2007
536
The only way I see to do a comparison is to do an exclusive-or (XORLW), and test the Z-flag. In XOR, if the bits are the same, the result is zero; if they are different, the result is 1.

1001 1100 XOR 1001 1100 = 0000 0000 <- Z flag set
1111 1101 XOR 1001 1100 = 0110 0001 < Z flag clear

HTH,
--Rich
 

BMorse

Joined Sep 26, 2009
2,675
The only way I see to do a comparison is to do an exclusive-or (XORLW), and test the Z-flag. In XOR, if the bits are the same, the result is zero; if they are different, the result is 1.

1001 1100 XOR 1001 1100 = 0000 0000 <- Z flag set
1111 1101 XOR 1001 1100 = 0110 0001 < Z flag clear

HTH,
--Rich

1001 1100 XOR 1001 1100 = 0000 0000 <- Z flag CLEAR
1111 1101 XOR 1001 1100 = 0110 0001 < Z flag SET
 

t06afre

Joined May 11, 2009
5,934
We are talking about Microchip PIC 16F series I hope. Perform a SUBLW, subtract W from literal. Or a SUBWF subtract W from f. Use the status register to check the result. In assembler the status register is an important helper. In C or basic the use of this register is hidden, but it is still important.
 

BMorse

Joined Sep 26, 2009
2,675
We are talking about Microchip PIC 16F series I hope. Perform a SUBLW, subtract W from literal. Or a SUBWF subtract W from f. Use the status register to check the result. In assembler the status register is an important helper. In C or basic the use of this register is hidden, but it is still important.

You have to know both numbers in order to use SUBLW, (i.e only useful if you know what number it is you are checking for):

Rich (BB code):
INCF      TESTNUM,F       ;Increment a count
MOVLW  d'100'               ;move literal to W
SUBWF   TESTNUM,W      ;Is Testnum more than W??
BTFSS    STATUS,Z         ;If testnum > W then Z = 1 else Z = 0
goto      Over_100
goto      Under_100
To test to see if 2 numbers/literals are similar it is best to use the XORLW:
Rich (BB code):
MOVLW   b'1111000'
MOVWF   TESTNUM
MOVLW   b'11110011'
XORLW    TESTNUM,W
BTFSS     STATUS,Z      ;Z will equal 1 if different, 0 if the same
GOTO      DIFF_BITS
GOTO      SAME_BITS

My .02
 

t06afre

Joined May 11, 2009
5,934
To check if something is equal I agree that XOR is the best thing. But the OP did not say how he wanted to compare.
Anyway I post a page from a Microchip 16F data sheet. Emphasize on how the status register behaves for a SUB or XOR instruction.
Edit: If any doubt. A SUBLW, or a SUBWF will affect the status register in the same manner. I also see now that the OP meant to compare if equal
 

Attachments

Last edited:

Thread Starter

ProgKees

Joined Nov 10, 2009
2
Thanks a lot guys (and girls?? probably not!)
I see that I have to pay more attention to the Statusbyte and specially the Z flag.
I will try both suggestions (XOR and SUBLW) and see each one does.
 
Top