Regulus Berdin wrote: > > Hi John/Scott, > > Here is my version, somewhat similar to Scott's but the correction and > the end is more simpler: > > (untested) > cnt16bit: > clrf kz > clrf lo > clrf hi > > movlw 2 ;preset 2 counts > loop btfsc PORTB,7 > goto quit1 ;1 > > addwf lo,f ;inc lo cnt > btfsc PORTB,7 > goto quit2 ;2 > > rlf kz,w ;get carry > btfsc PORTB,7 > goto quit3 ;3-2 > > addwf hi,f ;hi incremented depends on carry > btfsc PORTB,7 > goto quit4 ;4-2 > > skpc ;check if already finished > btfsc PORTB,7 > goto quit5 ;5-2 > > movlw 7 ;add 7 uncounted tests > btfsc PORTB,7 > goto loop6 ;6-2 > > btfss PORTB,7 > goto loop > ;7-2 > > quit7 incf kz,f ;add uncounted tests > quit6 incf kz,f > quit5 incf kz,f > quit4 incf kz,f > quit3 addwf hi,f ;inc hi byte first > ; bec. not incr in the loop > > quit1 incf kz,w > addwf lo,f > skpnc > incf hi,f > return > > quit2 addwf hi,f > return And then corrected: > Sorry, I made a mistake. This should be: > > incf kz,w > adj addwf lo,f > quit2 skpnc > incf hi,f > return > > quit1 addlw -1 ;1 test less > goto adj This still will not work because the value of W is either 0,1 or 7 when the quit 3 label is encountered. I think a slightly better approach - that still keeps the spirit of this idea - is to not try to use the contents of W once the loop has been terminated. Also, another small problem is that the overflow condition is not checked for outside of the loop. If I may: cnt16bit: > clrf kz > clrf lo > clrf hi > > movlw 7 ;preset 7 counts > loop btfsc PORTB,7 > goto quit1 ;1 > > addwf lo,f ;inc lo cnt > btfsc PORTB,7 > goto quit2 ;2 > > rlf kz,w ;get carry > btfsc PORTB,7 > goto quit3 ;3-2 > > addwf hi,f ;hi incremented depends on carry > btfsc PORTB,7 > goto quit4 ;4-2 > > skpc ;check if already finished > btfsc PORTB,7 > goto quit5 ;5-2 > > movlw 7 ;add 7 uncounted tests > btfsc PORTB,7 > goto loop6 ;6-2 > > btfss PORTB,7 > goto loop > ;7-2 > > quit7 incf kz,f ;add uncounted tests > quit6 incf kz,f quit5 skpnc goto overflow movlw -7 + 4 ; addwf lo,f skpc decf hi,f goto quit1 quit4 incf kz,f subwf hi,f ;counter-act the addwf quit3 incf kz,f quit2 ;subtract the 7 added in above - the incf's ;will keep track of how much needs to be added ;back. ignore the C, since the loop didn't get ;a chance to handle it. movlw -7 + 1 ;+1 in lieu of incf kz,f addwf lo,f quit1 incf kz,w addlw CONSTANT ;take into account ;initial latency > addwf lo,f > skpnc > incf hi,f clrf kz > return overflow movlw 0xff movwf lo movwf hi clrf kz return I think that does it, but there's bound to be one or two cases I missed. Scott