>-----Original Message----- >From: Koen van Leeuwen [mailto:koen@VANLEEUWEN.XS4ALL.NL] >Sent: 11 March 2004 22:02 >To: PICLIST@MITVMA.MIT.EDU >Subject: Re: [PIC]: coding challenge: divide by 7 > > >On Thu, 2004-03-11 at 08:48, Wouter van Ooijen wrote: >> Like you I specified a byte (8 bits) input, yet the code attempts to >> divide it by 512. And I am after an exact result, not an >> approximation. >> >> Wouter van Ooijen > >This loop version is also 11 instructions, but perhaps the loop is >shorter/faster(?) than yours, or you see an instruction which can be >erased: > >;division by 7 by Koen van Leeuwen >;number to be divided in divisor >;Trashed: w, divisor > clrc > clrf outcome ;start with clean reg > movlw b'11100000' ;7 <<5 >loop: > movwf powerof7 > subwf divisor,w ;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 > btfsc outcome,0 ;If carry was 1, subtraction succeeded. > movwf divisor ;so this should be our next divisor > rrf powerof7,w ;next bit, clear carry > btfss STATUS,C ;If we have a carry here, we >have ;shifted the 7 >out. > goto loop > ;and here is the result in outcome I wrote almost the same function before I left work last night, but didn't have time to test it so didn't post. Your's has one less instruction and it easier to understand though. If you unroll this loop, you get a good saving in execution time (executes in 40 cycles including call and return), and it's still reasonably compact (37 instructions). Regards Mike Rigby-Jones div7 clrc clrf outcome ;start with clean reg movlw b'11100000' ;7 <<5 movwf powerof7 subwf divisor,w ;check whether its too big rlf outcome,f ;shift in carry(/borrow) btfsc outcome,0 ;If carry was 1, subtraction succeeded. movwf divisor ;so this should be our next divisor rrf powerof7,w ;next factor movwf powerof7 subwf divisor,w ;check whether its too big rlf outcome,f ;shift in carry(/borrow) btfsc outcome,0 ;If carry was 1, subtraction succeeded. movwf divisor ;so this should be our next divisor rrf powerof7,w ;next factor movwf powerof7 subwf divisor,w ;check whether its too big rlf outcome,f ;shift in carry(/borrow) btfsc outcome,0 ;If carry was 1, subtraction succeeded. movwf divisor ;so this should be our next divisor rrf powerof7,w ;next factor movwf powerof7 subwf divisor,w ;check whether its too big rlf outcome,f ;shift in carry(/borrow) btfsc outcome,0 ;If carry was 1, subtraction succeeded. movwf divisor ;so this should be our next divisor rrf powerof7,w ;next factor movwf powerof7 subwf divisor,w ;check whether its too big rlf outcome,f ;shift in carry(/borrow) btfsc outcome,0 ;If carry was 1, subtraction succeeded. movwf divisor ;so this should be our next divisor rrf powerof7,w ;next factor movwf powerof7 subwf divisor,w ;check whether its too big rlf outcome,f ;shift in carry(/borrow) return ======================================================================= This e-mail is intended for the person it is addressed to only. The information contained in it may be confidential and/or protected by law. If you are not the intended recipient of this message, you must not make any use of this information, or copy or show it to any person. Please contact us immediately to tell us that you have received this e-mail, and return the original to us. Any use, forwarding, printing or copying of this message is strictly prohibited. No part of this message can be considered a request for goods or services. ======================================================================= Any questions about Bookham's E-Mail service should be directed to postmaster@bookham.com. -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads