STATUS Z bit Question

Discussion in 'Embedded Systems and Microcontrollers' started by backwards_dave, Dec 31, 2012.

  1. backwards_dave

    Thread Starter New Member

    Dec 31, 2012
    3
    0
    Hi,
    Before you enter the Interrupt Service Routine, you need to save the STATUS register since all registers get scrapped during the ISR (why is that, btw?).
    To do this, you can use:

    swapf STATUS, W ; move status to w without affecting Z bit
    movwf STATUS_TEMP ; copy to RAM (with nibbles swapped);

    Just wondering why swapf doesn't affect the Z bit, what if STATUS contained all 0's? Surely the Z bit would then go to 1 because the operation resulted in a register full of 0's?

    Why couldn't you use this:

    movf STATUS, W
    movwf STATUS_TEMP

    I don't understand how movf affects the Z bit, because the result of moving STATUS to W wouldn't be 0 unless STATUS contained all 0's right?

    Hope you can clear this up for me!
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Since you have failed to identify the device you are using, I will assume you are using a PIC microcontroller, since this is the only uC I'm aware of that fits with your description of the registers and namings.

    This is because of the way the PIC architecture was created. It's simply the way that the designer chose to do it.

    If you look at your datasheet(under the section titled, "Instruction Set Summary"), the swapf command does not affect the Z flag, even if the result is all 0s. The movf command, on the other hand can, and will, affect the Z flag in the STATUS register.

    Again, this is simply how the commands were implemented in the PIC architecture.
     
    Last edited: Dec 31, 2012
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    The interrupt doesn't have to change the STATUS or W, for example you could have:

    BSF Foo, 0
    RETFIE

    as your interrupt and nothing has been modified apart from the bit in "Foo". However as soon as you do almost anything more complex than that in the interrupt you need to start saving things. It's the instructions that you put in the interrupt that can change STATUS and W, not the interrupt itself. Also note that the bank isn't automatically switched to bank 0 when you enter the interrupt, so you should save the backups of the saved registers in memory that is available in all banks, and then to switch to Bank 0 if required for the instructions in the interrupt.
     
  4. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Even working on a single bit may need changes to the status byte, as there's no way to be sure that the correct memory bank is set when you arrive in the interrupt--unless you know that you never use more than one bank, and that's hard to achieve because some of the control registers are in various banks. Though if that byte Foo is in the top few bytes, it's accessible in any bank and you'd be able to work on individual bits, as in setting a "tick flag". But that's pretty minimal use of an interrupt.
     
  5. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    Perhaps rare, John, but eliminating context save/restore code is not unheard of. I've actually done just that on more than one project. For example, both version 2 and 3 of the ez-lcd serial LCD backpack firmware would initialize everything and then do a continuous loop in main (in bank 0) while the ISR did all the work. No context saving necessary.

    Regards, Mike

    [​IMG]
     
  6. backwards_dave

    Thread Starter New Member

    Dec 31, 2012
    3
    0
    why will movf affect the Z flag? I thought it only does it if you are moving only 0's.
     
  7. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It always affects the Z flag, it will be 1 if the number was 0 or 0 if the number was anything else. If you are checking Z bit anywhere in the main loop then the interrupt is likely to happen just before you test the Z bit in the main loop, making the result of the test incorrect.
     
  8. backwards_dave

    Thread Starter New Member

    Dec 31, 2012
    3
    0
    but if z=0 before the ISR, and then you use movf where the result isn't 0, then you wouldn't say that it affects the z bit would you, since z doesn't change?
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    If you prefer you can say that Z only changes when the result of the last zero test is different from the previous test... which is kinda obvious and ultimately pointless.

    Z contains the result of the last zero test.

    Period, end of story.
     
Loading...