On Mon, 12 Jul 1999 16:10:36 +0100 peterc writes: >Can anyone improve on the following for copying a variable number of >bytes from one area of memory to another. Both destination and source >locations are variable. The best optimization is to place the data strategically in RAM so a less general-purpose routine can be used. If you can arrange it so the address in FSR can be changed between the source and destination by setting and clearing bits, then it isn't necessary to use a temporary location to save the data. To do that the address of the source and destination should be seperated by a power of two bytes. The routine originally posted can be optimized a little by realizing the two bytes to be accessed are always a fixed distance away in RAM. So you could add and subtract a constant offset to FSR to change between source and destination. This way the address can be kept in FSR. ; dest = address of first destination byte ; offset = distance between source and dest (source - dest) ; count = number of bytes to move decf dest,w ;Correct for incrementing later. movwf FSR ;Set FSR to start move_loop incf offset,w ;Offset + 1 to W addwf FSR,f ;point to next source byte movfw INDF movwf temp movfw offset subwf FSR,f ;Point back at destination byte movfw temp ;Get data back movwf INDF ;Store. decfsz count,f ;Do another? goto move_loop ;Yes. This takes 11 cycles per loop to move a byte, the original one takes 12 (if you use a decfsz for the count). So no big savings. I think it is possible to use a swap trick to not need a temporary byte, but it would take more cycles. The routine is easiest to understand if the source data is at a higher address than the destination data. I think it will work properly if it is not though. Hopefully the PIC18 will have dual FSRs to make this sort of stuff easy. ___________________________________________________________________ Get the Internet just the way you want it. Free software, free e-mail, and free Internet access for a month! Try Juno Web: http://dl.www.juno.com/dynoget/tagj.