Okay so, I'm having this part of my program that needs a value to be checked to continue.

basically, here is what it is doing:

WAIT_INT   ; wait for interrupt
     BTFSS TO_LOOP_OR_NOT    ; will be set after interrupt runs
     ; interrupt assigns a value to W-reg
     ; When it exits, it will be compared here below
     SUBLW 01H
     BTFSS STATUS, Z            ; checks if equal
     ; rest of code

I am stuck in this part and I don't know if the value W-reg is saved after being assigned in an interrupt.

  • 1
    \$\begingroup\$ A specific sequence of instructions (described in the datasheet) is required to save and restore W and the STATUS register. If you don't do this they will retain the values they were given in the ISR. You could even just change the Z flag and wait for that in your main code. \$\endgroup\$ Dec 18, 2020 at 17:37
  • 1
    \$\begingroup\$ Since you very rarely know where in your code an interrupt occurs, your interrupt service routine should never corrupt W-reg. Your interrupt servicing code should alter no status bits, nor alter W-reg. Sometimes, a very simple interrupt routine can set a bit in a variable (set a flag) without affecting W-reg or status...with bsf flag,0 for example. \$\endgroup\$
    – glen_geek
    Dec 18, 2020 at 19:34

1 Answer 1


The answer is in the datasheet in section 2.4.


When an interrupt occurs the PCLAT and PCLATH are stored on the stack. It doesn't day anything about WREG being stored. If you assign a value to WREG in an interrupt it the value will persist when the interrupt returns.

The PIC16F84A doesn't have a notion of a stack in memory. It only has a hardware return address stack for interrupts and function calls. In fact if you look in Table 7-2 (PIC16CXXX INSTRUCTION SET) there are no push and pop instructions. You can however use the INDF register for the purposes of implementing a stack in memory.

I would point out though, that you are waiting in a loop for an interrupt to occur. You could just as easily test the interrupt flags directly without actually going into an interrupt, and you will probably achieve the same result.

  • \$\begingroup\$ Thank you, how would I approach storing the value of WREG so that I could access it again in main? \$\endgroup\$ Dec 18, 2020 at 17:22
  • \$\begingroup\$ If you assign the value to WREG in the interrupt, then you don't need to do anything special for your main code loop to see the new value in WREG. Its just there when the main code resumes. \$\endgroup\$
    – user4574
    Dec 18, 2020 at 17:25
  • \$\begingroup\$ Ohhh okay, I see. Thank you so much! \$\endgroup\$ Dec 18, 2020 at 17:29

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.