Electronic – What’s the most effective way to save/restore a PIC status bit

assemblypic

I want to save/restore the PIC's carry bit. I'm using the 16F628A.

SAVE_CARRY
    btfss STATUS, 0
    goto CARRY_OFF 

CARRY_ON
    bsf carry, 0
    return

CARRY_OFF
   bcf carry, 0
   return

RESTORE_CARRY
   btfss carry, 0
   goto RESTORE_CARRY_OFF

RESTORE_CARRY_ON
   bsf STATUS, 0
   return

RESTORE_CARRY_OFF
   bcf STATUS, 0
   return

There must be a better way. Is there?

Best Answer

One way that you can do:

SAVE_CARRY:
    bsf    carry, 0
    btfss  status, c
    bcf    carry, 0
    return

RESTORE_CARRY
    bsf    status, c
    btfss  carry, 0
    bcf    status, c
    return

If you need to save the registers "W" and "Status", before execute an interrupt code, there is another way:

Save_Context:
    movwf w_temp
    swapf status,w
    movwf status_temp
    ;Your interrupt code here. 

Restore_Context:
    swapf status_temp,w
    movwf status
    swapf w_temp,f
    swapf w_temp,w
    retfie