Searching \ for 'SWAPF instruction' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=swapf+instruction
Search entire site for: 'SWAPF instruction'.

Truncated match.
PICList Thread
'SWAPF instruction'
1997\07\14@092830 by Ben Roothooft

flavicon
picon face
Ok,

I've got this question over which I have
been breaking my head far too long. Maybe
somebody can get me out of my misery...

What's the meaning of the SWAPF instructions,
performed on W and STATUS register in the
saving and restoring code, proposed by
Microchip in the PIC datasheets ???

Thanx-a-lot !

Ben R.

1997\07\14@093703 by David BALDWIN

flavicon
face
hi,

       Nice to find someone that's doing PIC development in Belgium! What are
you working on? For the Swapf, I don't know, but I am using it, and it
works, but I don't understand why we need to do this...

--

 _____________
 \           /               David BALDWIN
  \ ALCATEL /               Design engineer
   \TELECOM/
    \     /         SdM (Societe de Microelectronique)
     \   /
      \ /      B.P. 4205            Phone : +32 (0)71 442932
       V       B-6000 Charleroi     Fax   : +32 (0)71 442905
               (Belgium)            spam_OUTbaldwinTakeThisOuTspametca.alcatel.be

1997\07\14@100600 by Clyde Smith-Stubbs

flavicon
face
On Mon, Jul 14, 1997 at 03:27:44PM +0200, Ben Roothooft wrote:
> What's the meaning of the SWAPF instructions,
> performed on W and STATUS register in the
> saving and restoring code, proposed by

The SWAPF instruction is used because the register restore sequence
at the end of the interrupt must first restore STATUS, by copying
it from the saved location, via W, back to STATUS, then reload W.

But if a MOVF  mem,w was used, this would modify the status register!
The MOVF instruction modifies the Z flag.

So instead the SWAPF instruction is used, because it does not modify
any status register bits. However, because it does swap the nibbles of
the byte, it must be used twice, e.g.

       swapf   mem             ; this swaps in the mem location
       swapf   mem,w           ; this swaps, and puts the result in w

After this, W has the original contents of mem, but STATUS is unaffected.

Cheers, Clyde

--
Clyde Smith-Stubbs  |HI-TECH Software,      |Email: .....clydeKILLspamspam@spam@htsoft.com
Ph:  +61 7 3354 2411|P.O. Box 103, Alderley,|WWW:  http://www.htsoft.com/
Fax: +61 7 3354 2422|QLD, 4051, AUSTRALIA.  |PGP: finger clydespamKILLspamhtsoft.com
---------------------------------------------------------------------------
ANSI C for the PIC! Now shipping! See http://www.htsoft.com for more info.

1997\07\14@101243 by Mike Henning

flavicon
face
> I've got this question over which I have
> been breaking my head far too long. Maybe
> somebody can get me out of my misery...
>
> What's the meaning of the SWAPF instructions,
> performed on W and STATUS register in the
> saving and restoring code, proposed by
> Microchip in the PIC datasheets ???
>
This is just a neat little trick they use to save the state of a file
register without affecting the status bits, such as Z, C...etc

If you used a movf instruction it would affect the Z bit.

Example:
swapf status,0
This swaps the nibbles in status and stores the result in W

movwf temp
temp now contains the status with the nibbles swapped

swapf temp,0
swaps the nibbles back to thier original state and stores the result
back into W.

movwf status
Status is effectively restored now

I hope this helps......
Mike henning
.....mikehKILLspamspam.....smart.net

1997\07\14@102315 by Mark Langezaal
flavicon
face
Hi,
The reason for using SWAPF in the save and restore code is that SWAPF is the only instruction that does not affect any status flags while loading W. If you use MOVW to retrieve and (re-)store the status register, the Z flag is affected BEFORE the original status register contents could be saved.

Hopefully this explanation is clear enough. If you wish I can elaborate on the subject.

Mark Langezaal
EraseMEmlangezaalspam_OUTspamTakeThisOuTklm.nl


The code as proposed by Microchip:
PUSH:
  MOVWF W_TEMP ; Copy W to TEMP register,
  SWAPF STATUS, W ; Swap status to be saved into W
  MOVWF STATUS_TEMP ; Save status to STATUS_TEMP register

POP:
 SWAPF STATUS_TEMP, W ; Swap nibbles in STATUS_TEMP register
 ; and place result into W
 MOVWF STATUS ; Move W into STATUS register
 SWAPF W_TEMP, F ; Swap nibbles in W_TEMP and place result in W_TEMP
 SWAPF W_TEMP, W ; Swap nibbles in W_TEMP and place result into W

1997\07\14@102809 by Mike Keitz

picon face
On Mon, 14 Jul 1997 15:33:00 +0200 David BALDWIN
<baldwinspamspam_OUTETCA.ALCATEL.BE> writes:
>hi,
>
>        Nice to find someone that's doing PIC development in Belgium!
>What are
>you working on? For the Swapf, I don't know, but I am using it, and it
>works, but I don't understand why we need to do this...


The SWAPF instruction swaps the high and low 4 bits of a byte.  If a
memeory location contains 11100001, SWAPF  memloc,W will return 00011110
in W.  This is useful for packed BCD operations.

But why in an ISR?  The reason is that SWAPF doesn't affect the Z status
bit but MOVFW (or MOVF file,w) does.  The obvious way to save and restore
W and STATUS during an ISR would be:

int
       movwf   savew           ;Save interrupted program's W
       movfw   STATUS          ;and STATUS
       movwf   saves
[Interrupt service code here, may freely modify W and STATUS]
       movfw   saves           ;Get stored status back
       movwf   STATUS
       movfw   savew           ;Wrong.  Will affect Z.
       retfie

Unlike some processors, in a PIC the Z flag does not always correspond to
whether the W register contains zero.  The ISR above will force it to,
which may cause logical errors in the main program.

Since SWAPF doesn't affect Z, it is the less damaging way to recall from
memory to W.  Relpacing the last movfw with:
       swapf   savew,f         ;Put bits in proper order to
swap again
       swapf   savew,w         ;Restore to W without
affecting Z
will restore W and STATUS exactly as they were when the program was
interrupted.

1997\07\14@105207 by Ian Cameron

picon face
> I've got this question over which I have
> been breaking my head far too long. Maybe
> somebody can get me out of my misery...
>
> What's the meaning of the SWAPF instructions,
> performed on W and STATUS register in the
> saving and restoring code, proposed by
> Microchip in the PIC datasheets ???

I think it's because SWAPF does not affect any status bits when it's
used, so at the end of an interrupt routine, the critical part is in
restoring W and STATUS exactly as they were before you went into the
interrupt routine.

SWAPF  S_TEMP,W
MOVWF  STATUS          ; Restore STATUS
SWAPF  W_TEMP,F
SWAPF  W_TEMP,W        ; Restore W

The first and second lines restore STATUS with the original byte
order, then the third and forth restore W without affecting STATUS.
On the other hand, if you were to use MOVF, it could potentially
affect STATUS,Z.

You can find out what affects instructions have on the STATUS
register from the PIC16CXX data sheets.  I'm looking at the 16C6X
data sheets, page 132, table 14-2.

Hope this helps.

Cheers, Ian.

1997\07\14@105223 by David BALDWIN

flavicon
face
Clyde, is there any documentation for the beta3 of your soft available. It is
hard to evaluate a soft without any doc :(

1997\07\14@115330 by lilel

flavicon
face
> What's the meaning of the SWAPF instructions,
> performed on W and STATUS register in the
> saving and restoring code, proposed by
> Microchip in the PIC datasheets ???


I use SWAPF in part of my POP routine for saving registers at the
beginning of an interrupt.  SWAPF flips the two nibbles in a byte
changing, say, 11110000 to 00001111.  But it is also useful
because it doesn't affect any status bits.   In the POP routine
below, MOVF or MOVWF would work but would affect status bits.

Other than that, the code SWAPF's twice, which restores things to
their original state.


PUSH    MACRO
  MOVWF        W_TEMP  ; SAVE W REGISTER
  SWAPF        STATUS, W       ; RECALL STATUS REGISTER
  MOVWF        STATUS_TEMP     ; NOTE THAT SWAPF DOES NOT
   ; AFFECT THE Z BIT!
  SWITCHBANK 0
  ENDM

POP     MACRO
  SWAPF        STATUS_TEMP, W
  MOVWF        STATUS          ; RESTORE STATUS REGISTER AND BANK BITS
  SWAPF        W_TEMP, F
  SWAPF   W_TEMP, W    ; RESTORE W
  ENDM

1997\07\14@115451 by John Payson

picon face
> The SWAPF instruction is used because the register restore sequence
> at the end of the interrupt must first restore STATUS, by copying
> it from the saved location, via W, back to STATUS, then reload W.
>
> But if a MOVF  mem,w was used, this would modify the status register!
> The MOVF instruction modifies the Z flag.

Saving/restoring STATUS with MOVF/MOVWF would not pose a problem; the MOVF
STATUS,W in the context-save code would trash Z, but not until after the old
status value was stored in W.  Similarly, the MOVF OLDSTAT,W would trash Z,
but the following MOVWF STATUS would restore it to the saved value.

The problem is with restoring W.  While MOVWF OLDW works fine at the start of
the ISR, MOVF OLDW,W would trash Z.  The best workarounds cost one extra cycle
versus what would be possible if MOVF didn't affect flags:

Either:
       swapf   OLDW,f
       swapf   OLDW,w

or

       rrf     OLDW,f
       rlf     OLDW,w

or

       rlf     OLDW,f
       rrf     OLDW,w

Too bad there isn't a version of "MOVF" that doesn't set flags.  The opcode
space of the 16Cxx parts would even have room for it.  Oh well...

1997\07\14@180204 by Mike Keitz

picon face
On Mon, 14 Jul 1997 10:32:05 -0500 John Payson <@spam@supercatKILLspamspamMCS.NET> writes:
[...]
>Too bad there isn't a version of "MOVF" that doesn't set flags.  The
>opcode
>space of the 16Cxx parts would even have room for it.  Oh well...
>
That wouldn't be my first choice for expanding the instruction set.  For
that I'd like to have two instructions to move any bit directly in and
out of the Carry bit.  This would speed up serial I/O and general
bit-shuffling.  Also nice would be a way to complement a bit, which the
17CXX has.

For going in and out of ISRs it would be better to expand RETFIE to take
a parameter which is a RAM adress to recall to STATUS (without affecting
flags of course).  Then the ISR would be:

int
       movwf   savedw
       movfw   STATUS
       movwf   saveds
[interrupt service code]
       movfw   savedw
       retfie  saveds
This takes 3 cycles off of the old method, or two off of the method that
could be used if a no-status MOVFW were available.  It is also compatible
with old code by assembling RETFIE STATUS which would merely copy STATUS
onto itself during the return, doing nothing.  Since the RETLW
instruction has 2 don't care bits in it, several new specialized returns
could be defined.

There is still the need to trash W in order to save STATUS at the start
of the ISR.  If the hardware copied STATUS into a dedicated shadow
location during the interrupt, then 2 cycles could be saved there.  Since
nearly all interupt-driven PIC code is not nested (facilitated by the
automatic disable), having only a single dedicated shadow location
wouldn't be much of a problem.  If the ISR could be written to not change
W (but still damage STATUS), then this feature and the new return and
load instruction would cut interrupt overhead to none, or 2 cycles and
one RAM location if W needs to be saved.

1997\07\15@042735 by Tim Forcer

flavicon
face
Just a very minor point on using SWAPF to push/pull without affecting STATUS.

*IF* (a VERY big IF) the push/pull is the boundary of the sole interrupt
service routine, AND the main code doesn't affect any STATUS bits, then you
can leave out STATUS from the push/pull, and push/pull W with plain MOVWF
and MOVF.  Result (for '84) can look like something I did recently:

       ORG     4               ; Interrupt vector
ISR
; Interrupt service routine
; explanation and description deleted............

       MOVWF   STAKW           ; Stack W BUT NOT S (not used in main routine)
;        SWAPF   STATUS, W       ; Pre-swap STATUS for storage
;        MOVWF   STAKS           ; Note GIE cleared by responding to interrupt

::::::::::::::::::

ENDINT
       BCF     INTCON, T0IF    ; Clear Timer interrupt that got us here
; STATUS not recovered, as not saved, as not used in main routine
;        SWAPF   STAKS, W        ; Recover (and unswap) status register
;        MOVWF   STATUS
;        SWAPF   STAKW, F        ; Preswap W
;        SWAPF   STAKW, W        ; And recover it
       MOVF    STAKW, W        ; Affects STATUS, but don't care
       RETFIE                  ; Also re-enables interupts by setting GIE

The specific application was a very simple one where everything depended on
timer ticks and the main routine was a simple loop.

Remembering the discussion a few weeks back about comments, you will note
that I've included several reminders to myself about what the code does,
and that the conventional push/pull of STATUS and W using SWAPF is not
deleted, merely commented out in case some future revision means the main
loop has to use STATUS.  No doubt some will think I've over-commented, but
I don't do very much PIC programming, and find it very easy to forget why
my old code looks the way it does.

You could argue, with much justification, that the push/pull should always
ensure that STATUS is returned in the same condition it was before the
routine - there were no overriding requirements of timing or space which
meant saving a few instructions actually mattered.  In this case it did
mean that the number of cycles lost in the prescaler (reset when the timer
was reloaded) was reduced by two, and I can envisage situations where that
might be useful.

Tim Forcer

Tim Forcer               KILLspamtmfKILLspamspamecs.soton.ac.uk
Department of Electronics & Computer Science
The University of Southampton, UK

The University is not responsible for my opinions

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...