• AVR Freaks

Hot!Copy subroutine

Author
Airborne
New Users
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2018/02/12 05:13:02
  • Location: Italy
  • Status: offline
2020/05/20 03:02:20 (permalink)
0

Copy subroutine

Hi , I'm working with PIC16F19156 (mpasm) and 64-bit variables and I would like a subroutine to copy one variable to another. For example in this case:
 BANKSEL varA
 MOVFW varA + 0
 BANKSEL varB
 MOVWF varB + 0
 BANKSEL varA
 MOVFW varA + 1
 BANKSEL varB
 MOVWF varB + 1
 BANKSEL varA
 MOVFW varA + 2
 BANKSEL varB
 MOVWF varB + 2
 BANKSEL varA
 MOVFW varA + 3
 BANKSEL varB
 MOVWF varB + 3
 BANKSEL varA
 MOVFW varA + 4
 BANKSEL varB
 MOVWF varB + 4
 BANKSEL varA
 MOVFW varA + 5
 BANKSEL varB
 MOVWF varB + 5
 BANKSEL varA
 MOVFW varA + 6
 BANKSEL varB
 MOVWF varB + 6
 BANKSEL varA
 MOVFW varA + 7
 BANKSEL varB
 MOVWF varB + 7

in the subroutine I should provide the variable label (varA and varB) with different memory banks. The result should be copying varA to varB.
Thanks for help
#1

12 Replies Related Threads

    upand_at_them
    Super Member
    • Total Posts : 590
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: Copy subroutine 2020/05/20 03:09:42 (permalink)
    0
    If you use the FSR's you can benefit from the moviw and movwi instructions.
    #2
    upand_at_them
    Super Member
    • Total Posts : 590
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: Copy subroutine 2020/05/20 03:18:46 (permalink)
    0
    movlw LOW (varA)
    movwf FSR0L
    movlw HIGH (varA)
    movwf FSR0H
    movlw LOW (varB)
    movwf FSR1L
    movlw HIGH (varB)
    movwf FSR1H
    movlw .8
    movwf counter ; variable in common area RAM (70h-7Fh)
    moviw FSR0++
    movwi FSR1++
    decfsz counter, f
    goto $-3

     
    #3
    ric
    Super Member
    • Total Posts : 28024
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Copy subroutine 2020/05/20 05:24:23 (permalink)
    5 (2)
    A bit more universal to pop it into a subroutine
            movlw LOW (varA)    ;set source address
            movwf FSR0L
            movlw HIGH (varA)
            movwf FSR0H
            movlw LOW (varB)    ;set destination address
            movwf FSR1L
            movlw HIGH (varB)
            movwf FSR1H
            movlw .8        ;set item count
            call copy_ram
    ....

    copy_ram:
            moviw   FSR0++
            movwi   FSR1++
            decfsz  WREG, f
            goto    copy_ram
            return


    and I absolutely deplore using "goto $-3" !



    post edited by ric - 2020/05/20 05:26:24

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    upand_at_them
    Super Member
    • Total Posts : 590
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: Copy subroutine 2020/05/20 06:21:56 (permalink)
    0
    ric, aren't you overwriting the item count value in W?
    #5
    1and0
    Access is Denied
    • Total Posts : 11000
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Copy subroutine 2020/05/20 08:08:31 (permalink)
    0
    upand_at_them
    ric, aren't you overwriting the item count value in W?

    Yes. I think he meant ;)
    copy_ram:
            banksel count
            movwf   count
    copy_lp moviw   FSR0++
            movwi   FSR1++
            decfsz  count
            bra     copy_lp
            return

     
    post edited by 1and0 - 2020/05/20 08:33:40
    #6
    1and0
    Access is Denied
    • Total Posts : 11000
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Copy subroutine 2020/05/20 08:11:03 (permalink)
    0
    airborne
    Hi , I'm working with PIC16F19156 (mpasm) and 64-bit variables and I would like a subroutine to copy one variable to another. For example in this case:
     
    ...
    in the subroutine I should provide the variable label (varA and varB) with different memory banks. The result should be copying varA to varB.
    Thanks for help

    If FSRx cannot be used, use direct addressing as you do and use macro to reduce error and typing:
    movff8  macro   source, target
        local n = 0
        while n < 8
            banksel source
            movf    source + n,w
            banksel target
            movwf   target + n
    n++
        endw
            endm

            movff8  varA,varB

    #7
    upand_at_them
    Super Member
    • Total Posts : 590
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: Copy subroutine 2020/05/20 08:21:25 (permalink)
    0
    Also, you can fit ten 64-bit variables into each 80 byte RAM bank of that PIC.  Why not organize things to make copying easier?
    #8
    ric
    Super Member
    • Total Posts : 28024
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Copy subroutine 2020/05/20 13:13:00 (permalink)
    0
    upand_at_them
    ric, aren't you overwriting the item count value in W?

    Oops, brain fart on my part after looking into MOVFF issues last week.
    Totally forgot the move was using W.
    If you were short on RAM, and always wanted to move 8 bytes, inlining 16 MOV instructions would be an alternative, otherwise you do need to use a loop count variable.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    Airborne
    New Users
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2018/02/12 05:13:02
    • Location: Italy
    • Status: offline
    Re: Copy subroutine 2020/05/20 23:00:40 (permalink)
    0
    Thanks every body!
    If FSRx cannot be used, use direct addressing as you do and use macro to reduce error and typing:

    I think I'll do so ....
    #10
    ric
    Super Member
    • Total Posts : 28024
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Copy subroutine 2020/05/20 23:08:08 (permalink)
    0
    Are they in the same bank?
    If yes, easy.
    If not, a mix of indirect and direct using one FSR/INDF pair will help.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    NorthGuy
    Super Member
    • Total Posts : 6230
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Copy subroutine 2020/05/21 09:42:41 (permalink)
    0
    I would try to do something to avoid banksels - putting them into the same bank, using common RAM. Or, perhaps you could put your variables at the same offsets in different banks, so that the same code would access different variables depending on the bank selected - in such case you may not need any copying.
     
    There are also intermediary variants involving FSRs - e.g. one of the variables is pointed to by FSR while the other is banksel'd.
     
    PIC assembler is designed to teach you to be creative. Don't let this opportunity pass :)
     
    #12
    Airborne
    New Users
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2018/02/12 05:13:02
    • Location: Italy
    • Status: offline
    Re: Copy subroutine 2020/05/22 00:00:45 (permalink)
    0
    Great advice! for now since I am a beginner I prefer to favor simplicity
    #13
    Jump to:
    © 2020 APG vNext Commercial Version 4.5