# Byte Shift Code Help

#### Suraj143

##### Active Member
I have 24 bytes & I need to shift bytes from D1 to D24, but the "New Data Byte" must feed into the 1st byte D1.
Below code I have, Can it be compact more than this?

Code:
        movlw    .24
movwf    Temp
movlw    D1        ;D1,D2,....D24
movwf    FSR
movf       New_Data,W
movwf    INDF
;
S_Right
xorwf    INDF,W
xorwf    INDF,F
xorwf    INDF,W
incf       FSR,F
decfsz  Temp,F
goto      S_Right
return

#### Pommie

##### Well-Known Member
Put the new data in after the shuffle. So load, inc, store loop.

Mike.

#### Pommie

##### Well-Known Member
Try'
Code:
        movlw    .24-1
movwf    Temp
movlw    D1        ;D1,D2,....D24
movwf    FSR
S_Right movf    INDF,w
incf    FSR,f
movwf    INDF
decfsz  Temp,F
goto      S_Right
movf    New_Data,W
movwf    D1
return
Mike.

#### Suraj143

##### Active Member
Try'
Code:
        movlw    .24-1
movwf    Temp
movlw    D1        ;D1,D2,....D24
movwf    FSR
S_Right movf    INDF,w
incf    FSR,f
movwf    INDF
decfsz  Temp,F
goto      S_Right
movf    New_Data,W
movwf    D1
return
Mike.
Excellent. Thank you very much.

#### Beau Schwabe

##### Member
I think this might be more of what you are looking for... the entire data array is treated as a FIFO shifting from D1 to D24 with new data appearing at D1. D24 is lost at each Array shift

Code:
        movlw   d'24'-1
movwf   Counter
movlw   D23
movwf   FSR
S_Right:
movf    INDF,W
incf    FSR,F
movwf   INDF
movlw   d'2'
subwf   FSR,F
decfsz  Counter,F
goto    S_Right
movf    New_Data,W
movwf   D1
return

#### Pommie

##### Well-Known Member
Agree with Beau, though not it's less efficient than the original xor code. Note to self, don't post untested code after a night down the pub.

Mike.

#### Beau Schwabe

##### Member
Yes, I had to look at the original code closer to understand the xor's .... I remarked one line to get it to run correctly, but looking at the original code it looks like it might have been remarked already, but just a typo. Anyway, the original code looks about as tight as you can get it with that processor. You "might" be able to shave off a little more code if you arrange the data array from D24 to D1 instead of D1 to D24 and coordinate the Temp variable with the actual FSR position decrementing rather than incrementing and starting with D24, but I did not entertain that exercise.

Code:
        movlw    d'24'
movwf    Temp
movlw    D1        ;D1,D2,....D24
movwf    FSR
movf     New_Data,W
; movwf    INDF  ;<-- Remark this line from original code
S_Right
xorwf    INDF,W
xorwf    INDF,F
xorwf    INDF,W
incf     FSR,F
decfsz   Temp,F
goto     S_Right
return

#### Pommie

##### Well-Known Member
The only way I can see to make that quicker is to arrange for D24 to be at the end of a bank so the incf FSR can be changed to incfsz and not require the counter. Reversing the order and placing at the beginning of a bank would be better as the common (valuable) ram is at the end of each bank - alas, not possible with that chip. Alternatively, is program space allows, just straight line it - 4 instructions times 24 is only 96 program locations. Further savings could be made by just using load store 24 times resulting in only 48 program locations being used.

Mike.

#### Beau Schwabe

##### Member
Another way to do it would be to use an index pointer to the data. The data doesn't actually shift within D1 to D24, but the pointer does as far as how and when the data is retrieved and written.

#### Suraj143

##### Active Member
The only way I can see to make that quicker is to arrange for D24 to be at the end of a bank so the incf FSR can be changed to incfsz and not require the counter. Reversing the order and placing at the beginning of a bank would be better as the common (valuable) ram is at the end of each bank - alas, not possible with that chip. Alternatively, is program space allows, just straight line it - 4 instructions times 24 is only 96 program locations. Further savings could be made by just using load store 24 times resulting in only 48 program locations being used.

Mike.
Wonderfull suggestion.That is very clever thinking.

#### Beau Schwabe

##### Member
Brut force no loops ... This is quicker but takes up more code space

Code:
S_Right:
movf    D23,W
movwf    D24
movf    D22,W
movwf    D23
movf    D21,W
movwf    D22
movf    D20,W
movwf    D21
movf    D19,W
movwf    D20
movf    D18,W
movwf    D19
movf    D17,W
movwf    D18
movf    D16,W
movwf    D17
movf    D15,W
movwf    D16
movf    D14,W
movwf    D15
movf    D13,W
movwf    D14
movf    D12,W
movwf    D13
movf    D11,W
movwf    D12
movf    D10,W
movwf    D11
movf    D9,W
movwf    D10
movf    D8,W
movwf    D9
movf    D7,W
movwf    D8
movf    D6,W
movwf    D7
movf    D5,W
movwf    D6
movf    D4,W
movwf    D5
movf    D3,W
movwf    D4
movf    D2,W
movwf    D3
movf    D1,W
movwf    D2
movf    New_Data,W
movwf    D1
return