i'm wandering if is really correct the following way to save W and status:
Code:
movwf w_save
movf STATUS,w
movwf status_save
this method is used often in many examples on the net.
By the way, reading well the movf instruction in the datasheet, i would be more sure doing
Code:
movwf w_save
movf STATUS,0
movwf status_save
Is one of the 2 methods more correct ?
Only putting 0 as 'd' i would be really sure STATUS is moved into w ? Really i've difficult to understand the datasheet in this point.
You wrote it for pic16, right? If you are worrying about changing STATUS you can do swapf STATUS,w instead of movf. You will get your STATUS in W with nibbles reversed. You will need to use swapf again when restoring the STATUS.
You are correct that putting 0 in d will put the result into w.
Often, the include file will define f as 1 and w as 0, so you can just write:-
movf status, w
so both examples are equivalent.
However, when saving the status register, you should use the swap command, because the mov command will affect the status register, so you should use:-
movwf w_save
swapf STATUS,w
movwf status_save
swapf w_save, f
after the interupt, you should use:-
swapf status_save, w
movwf status
swapf w_save, f
retfie
This code is often used because it saves the status without actually changing it.
(A movf instruction affects the status zero flag but a swapf does not)
Code:
swapf STATUS,W ; swap status bits and put into w without affecting the status
clrf STATUS ; clear the status reg (effectively selects bank 0)
movwf status_temp ; save the previous status to another register
In answering your original question, I think it is easier to understand if you always use "W" rather than "0" to mean the W reg.
And "F" rather than "1" for the File register.
This code is often used because it saves the status without actually changing it.
(A movf instruction affects the status zero flag but a swapf does not)
When one is saving context of the STATUS register, one can use either MOVF or SWAP. In fact code examples by Microchip use both methods. The MOVF construct is more easily understood by beginners.
The STATUS flags don't get changed until after the completion of the instruction.
However, the only correct way to restore W is to use the SWAP construct as other commands would change the STATUS bit.
Be sure to use the Common Shared address for your _temp variables. The location for the common bank storage will depend on your device. The location is usually mapped at the bottom of the Bank and under data sheet section Memory Organization. The common address is mapped to all banks just like INTCON and FSR.
If you use the code posted by picasm, the temporary variable for W must be in the common area but the one for status can be anywhere.
Code:
swapf STATUS,W ; swap status bits and put into w without affecting the status
clrf STATUS ; clear the status reg (effectively selects bank 0)
movwf status_temp ; save the previous status to another register
Doing it this way save 1 common location and ensures you are in a known bank at the start of the ISR.
this is what i've done, and it seems also to be the fixing to the bug of my previous post on 16F877/ eeprom 24LC256 bug.
Code:
; now we can save W and STATUS without knowing in what bank we are
movwf w_save ; save the w reg. (common area mandatory)
swapf w_save,f ; to retrieve it later we will swapf, we must swap it now
swapf STATUS,w ; swap status bits and put into w without affecting the status
movwf status_save ; save the status (common area mandatory)
; exiting
swapf status_save, w
movwf STATUS
swapf w_save, w
retfie
Hope my assumptions are correct now, and also my bug seems fixed!