On Sun, 7 Sep 1997 15:39:15 -0300 Octavio Nogueira writes: >Hi all, > >I'm using floating-point arithmetic and the variables are 4 byte long. >I need a routine to move the value from one variable to another. E.g. > >Var1: Ax,A2,A1,A0 >Var2: Bx,B2,B1,B0 >Var3: Cx,B2,B1,B0 > >I need a routine to pass one variable to another, example Var1 to Var2 >or >Var3 to Var1, etc in any combination. > >I did one routine but it takes 38 ROM spaces and 3 RAM, does anyone >have a better idea? >P.S. The bytes in each variable are sequencial. > There are a lot of different ways to do this, trading off speed, code space, temporary variables, stack space, and flexibility. The fastest way to copy variables would be a simple load and store: move12 movfw Var1 movwf Var2 movfw Var1+1 movwf Var2+1 ... etc return This takes 9 locations per move. If you need to be able to move from anywhere to anywhere, 6 different versions of the routine would be needed for a total of 54 program locations. If more than 3 variables were used the number of routines would grow exponentially. So the direct method isn't very good. Using indirect addressing allows one routine to be called with pointers to the data to be moved, so a small amount of code space can serve many purposes. Here's a general purpose move routine which isn't optimized at all but it should work. ; Move a block of (count) bytes from RAM starting at (src) to RAM starting ; at (dest). Uses (tmp), W and FSR. move movfw src movwf FSR ;Point at a source byte movfw INDF ;Get the byte. movwf tmp ;Store in temp. movfw dest ;Point at a destination location. movwf FSR movfw tmp ;Get the data byte movwf INDF ;Store it. incf src,f ;Pointers to next byte incf dest,f decfsz count,f ;More to do? goto move ;Yes, do another byte. return This is 13 locations and 4 RAM. This program works on 14-bit PICs. The 17C4x's dual FSRs make operations like this a lot simpler. In order to use it it is also necessary to set up src, dest, and count before calling. Several support routines could be added to simplify calling. First, put this code over "move" to make it easy to store the destination pointer and set count to 4 every time: move4d movwf dest move4 movlw 4 movwf count To simplify setting the source and destination pointers write little routines of the form: move2var1 movwf src ;Store source movlw var1 ;Pointer for var1 goto move4d move2var2 movwf src movlw var2 ;* See note below. goto move4d ... [and a similar one for var3] Note carefully that the instruction in these is a movlw varx, not movfw. We want the *address* of the first byte in the variable in the W register, not the *data* there. Confusing these two concepts leads to a common form of mistake. With these routines in place (a total of about 25 instruction words for the entire suite), for the main program to copy var1 into var3, use this code: movlw var1 ;Move data from var1 call move2var3 ;into var3. -Mike