How to test a BYTE in Assembler?

Discussion in 'Embedded Systems and Microcontrollers' started by ProgKees, Nov 11, 2009.

  1. ProgKees

    Thread Starter New Member

    Nov 10, 2009
    2
    0
    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
     
  2. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    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
     
  3. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

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

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    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):

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

    My .02
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
    Last edited: Nov 11, 2009
  7. ProgKees

    Thread Starter New Member

    Nov 10, 2009
    2
    0
    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.
     
Loading...