STATUS Z bit Question

Thread Starter

backwards_dave

Joined Dec 31, 2012
3
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!
 

tshuck

Joined Oct 18, 2012
3,534
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.

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?).
This is because of the way the PIC architecture was created. It's simply the way that the designer chose to do it.

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

Markd77

Joined Sep 7, 2009
2,806
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.
 

John P

Joined Oct 14, 2008
2,026
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.
 

MMcLaren

Joined Feb 14, 2010
861
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

 

Attachments

Markd77

Joined Sep 7, 2009
2,806
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.
 

Thread Starter

backwards_dave

Joined Dec 31, 2012
3
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.
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?
 

ErnieM

Joined Apr 24, 2011
8,377
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?
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.
 
Top