According to Instruction Set Description "xorwf myLiteral" should xor contents of "w" with "myLiteral" and if both are same then flag Z in STATUS must be set (not interested where this instruction saves the result but Z flag must be affected if both values were same)

But in my code this is not happening

movf PORTC
xorwf red_on    ;w holds 01000100 and red_on is also 01000100 Z must be one
btfsc STATUS, Z ;but here it's not one
goto Turn_Yellow_On

Full code is following:

    ; PIC16F72 Configuration Bit Settings

; Assembly source line config statements

#include "p16f72.inc"

; __config 0xFFF7

STATUS equ 0x03
TRISC equ 0x87
PORTC equ 0x07
INTCON equ 0x0b

RP0 equ 5
RP1 equ 6
Z equ 2

GIE equ 7
INTE equ 4
INTF equ 1

all_off equ 0x00
red_on equ 0x44
yellow_on equ 0x22
green_on equ 0x11 

bank0 macro
    bcf STATUS, RP0
    bcf STATUS, RP1

bank1 macro
    bsf STATUS, RP0
    bcf STATUS, RP1

extrn_int_enable macro
    bsf INTCON, GIE
    bsf INTCON, INTE

PORTC_as_out macro
    movlw 0x00
    movwf TRISC

PORTC_out macro outByte
    movlw outByte
    movwf PORTC

test_and_switch macro testWith, jumpTo
    movf PORTC
    xorwf testWith
    btfsc STATUS, Z
    goto jumpTo

    org 0x00
    goto Main

    org 0x04
    goto Switch_Next

    org 0x05

    goto Loop

    org 0x200
    bcf INTCON, INTF

    ;test_and_switch all_off, Turn_Red_On
    movf PORTC      ;I recognized the mistake I should use d = 0 but data
    ;will still be copied in w when program goes to "Turn_Red_On" routine for 
    ;the first time
    xorwf all_off
    btfsc STATUS, Z
    goto Turn_Red_On

    ;test_and_switch red_on, Turn_Yellow_On
    movf PORTC
    xorwf red_on    ;w holds 01000100 and red_on is also 01000100 Z must be one
    btfsc STATUS, Z ;but here it's not one
    goto Turn_Yellow_On

    ;test_and_switch yellow_on, Turn_Green_On
    movf PORTC
    xorwf yellow_on
    btfsc STATUS, Z
    goto Turn_Green_On

    goto Turn_All_Off

    PORTC_out red_on
    goto Return_From_Int

    PORTC_out yellow_on
    goto Return_From_Int

    PORTC_out green_on
    goto Return_From_Int

    PORTC_out all_off
    goto Return_From_Int



Your movf PORTC instruction lacks a destination.

The default is f, PORTC in this case, so you are not putting the number in the w register.

Change it to movf PORTC, w

And you should always state it explicitly to prevent problems. Running a simulation would probably have caught this, and it's worth learning how to do it.

Edit: Also, you are using xorwf (ex-or the w register with a file register), when you should be using xorlw (ex-or a literal with the w register).

xorwf takes the operand to be an address and xor's the w register with the contents of the register at that location (0x44, in the 96 bytes of general purpose registers), which is not what you want.

  • \$\begingroup\$ destination of the result is not point of interest atm. The undesired result is when w does hold 0x44 and xorwf 0x44 doesn't set Flag Z (it's astonishing to me). If you copy and simulate this code in MPLAB X and use stimulus to interrupt INT pin you'll notice on 2nd interrupt after entering Switch_Next label on about 7th line of code you'll notice the value of w would be 0x44 and "xorwf red_on" (red_on equ 0x44) still won't set Z in STATUS upon it's execution which is strange \$\endgroup\$ – Danish ALI May 2 '18 at 16:01
  • \$\begingroup\$ See edit addition above. \$\endgroup\$ – Spehro Pefhany May 2 '18 at 17:57
  • 1
    \$\begingroup\$ Thanks it was my mistake I didn't recognize xorwf was taking literal as address not as constant value now I replaced it with xorlw and my code is working. Grateful to You! \$\endgroup\$ – Danish ALI May 3 '18 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.