-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 source= http://www.piclist.com/piclist/2004/03/10/154044a.txt? Wouter van Ooijen says: >Someone asked me for a fast yet not extremely large code snipped to divide >a one-byte value in memory by 7 (on a 14-bit core). I think I have a >straight-line (no loop) code that uses 33 instructions (2 less if the >source can be destroyed). Looping version would be 12 instructions, but of >course somewhat longer execution. Can anyone do it faster, preferrably >with fewer instructions? I know a table lookup will be faster, but I think >the 256+ size would be classified as 'extremely large'. I have coded a tiny program that can be examined in MPLab. The two routines produce exact results, both quotient and remainder. The Divide_Short routine is correct for any positive dividend and counting number divisor, and is 6 or 8 instructions. The Divide_Faster routine is also correct for any positive and counting numbers. It requires multiply support into a table of powers, but that can be done at assembly time with the fixed divisor problem. It is up to about 20 instructions. I have not calculated the product of (bytes X cycles) for the two routines. Is it defined whether (bytes X cycles) or some other metric is to be minimized? Perhaps there is a maximum execution time he has to meet? ;********************************************************** ; Divide a memory location by 7 ;********************************************************** #include ; processor specific variable definitions ;********************************************************** cblock 0x20 quotient remainder divisor endc ;********************************************************** Test: movlw D'7' movwf divisor movlw D'142' movwf remainder call Divide_Short call Divide_Setup movlw D'142' movwf remainder call Divide_Faster goto $ ;********************************************************** ; The dividend is in W, ; the divisor is in RAM ;********************************************************** Divide_Short: clrf quotient movf divisor, w loop: subwf remainder, F btfss STATUS, C goto neg incf quotient, f goto loop neg: addwf remainder, F return ;********************************************************** cblock pwr0 pwr1 pwr2 pwr3 pwr4 pwr5 pwr6 pwr7 endc Divide_Setup: movlw D'1' movwf pwr0 movlw D'7' movwf pwr1 movlw D'7'*D'7' movwf pwr2 return ;********************************************************** Divide_Faster: movlw pwr2 movwf FSR ; clrf quotient loop3: movf INDF, w loop2: subwf remainder, F btfss STATUS, C goto neg2 decf FSR, f movf INDF, w addwf quotient, f incf FSR, f movf INDF, w goto loop2 neg2: addwf remainder, F decf FSR, F movf FSR, w sublw pwr0 btfss STATUS, Z goto loop3 return END - --- Aubrey McIntosh http://www.piclist.com/member/AM-vima-Y84 PIC/PICList FAQ: http://www.piclist.com -----BEGIN PGP SIGNATURE----- Version: PGPfreeware 7.0.3 for non-commercial use iQA/AwUBQFAdegKlSw8yssF7EQIRnQCg9BPB575ic1qjKZVHqkMOYRzF5CQAoIde NiqQVm4QXdwkl2OWe3hhmYVE =XfTi -----END PGP SIGNATURE----- -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu