subject=Re:[PIC:] coding challenge: divide by 7 source= http://www.piclist.com/piclist/2004/03/12/163317a.txt? After some thinking: --- ;Reoptimized version of division by 7, by Koen van Leeuwen ;Classic algorithm. ;Number to be divided in W ;Result: integer result in outcome, remainder in W ;Trashes: W, STATUS, outcome ;Instructions/cycles: 25 ;According to the datasheets, ;sublw is w = k - w ;addlw is w = w + k ;It is kinda odd. ;I also read that the add instructions do not add carry. Also odd. ;But if it is correct, I guess the following code should work: ;WARNING: untested code ahead! clrc clrf outcome ;start with clean reg addlw d'-224' ;-(7<<5)check whether its too big rlf outcome,f ;shift in carry(/borrow) ;(=whether outcome is negative or not) ;and clear carry since leftmost bit of ;outcome is always zero btfss outcome,0 ;If carry was 0, subtraction underflowed. addlw d'224' ;so fix it ;NOTE: this generates a carry addlw d'-112' ;-(7<<4)check whether its too big rlf outcome,f ;shift in carry(/borrow) btfss outcome,0 ;If carry was 0, subtraction underflowed. addlw d'112' ;so fix it addlw d'-56' ;-(7<<3)check whether its too big rlf outcome,f ;shift in carry(/borrow) btfss outcome,0 ;If carry was 0, subtraction underflowed. addlw d'56' ;so fix it addlw d'-28' ;-(7<<2)check whether its too big rlf outcome,f ;shift in carry(/borrow) btfss outcome,0 ;If carry was 0, subtraction underflowed. addlw d'28' ;so fix it addlw d'-14' ;-(7<<1)check whether its too big rlf outcome,f ;shift in carry(/borrow) btfss outcome,0 ;If carry was 0, subtraction underflowed. addlw d'14' ;so fix it addlw d'-7' ;-(7<<0)check whether its too big rlf outcome,f ;shift in carry(/borrow) ;(=whether outcome is negative or not) ;and clear carry since leftmost bit of ;outcome is always zero return end PIC/PICList FAQ: http://www.piclist.com -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body