Your code will result in: low(NumLoops) + (high(NumLoops)-1)*256 loops The problem is that a value of 0x00 will result in an equivalent value of 256 i.e. Let NumLoops=255 will result in not 255 loops, but 255+256*256 loops since high(NumLoops) will be 0. incrementing the value of high(NumLoops) is a step in the right direction. Hope this helps, Pete Harold Hallikainen wrote: > Hello Listmembers! > > decfsz is a real nice instruction for loop counting. For example, > > movlw NumLoops > movwf LoopCounter > Loop > [do something] > decfsz LoopCounter,f > goto Loop > return > > This works GREAT for NumLoops<256 (though you can set NumLoops to 0 and get 256 loops). But, how can you initialize the loop counter if it's more than a single byte (more than 256 loops)? Here are a few problems I've found... > > Let NumLoops=256 > > movlw high(NumLoops) > movwf LoopCounterHi ; puts 1 in LoopCounterHi > movlw low(NumLoops) > movwf LoopCounterLo ; puts 0 in LoopCounterLo > Loop > [something useful] > decfsz LoopCounterLo,f ; Drops LoopCounterLo to 0xff > goto Loop ; keep looping til we hit 0 > decfsz LoopCounterHi,f ; After 256 loops, drops to 0 > goto Loop ; keep looping til we hit 0 > return ; exit > > The above code DOES properly loop 256 times. Now, let's try another... > > Let NumLoops=257 > > movlw high(NumLoops) > movwf LoopCounterHi ; puts 1 in LoopCounterHi > movlw low(NumLoops) > movwf LoopCounterLo ; puts 1 in LoopCounterLo > Loop > [something useful] > decfsz LoopCounterLo,f ; Drops LoopCounterLo to 0 > goto Loop ; skipped on first pass > decfsz LoopCounterHi,f ; drops to 0 > goto Loop ; skipped on first pass > return ; exit > > So, even though we wanted 257 loops, we got 1! > > The problem seems to be in setting the high byte of the loop counter. It SEEMS that if the low byte is zero, you can just use high() and low() to initialize the counter. But, if the low byte is non-zero, it SEEMS that we need to increment the high byte after initialization to get it to loop right. > > Of course, we could do stuff with subtracting from the loop counter (or adding -1), dealing with carries, then checking for an overall value of zero across the multiple bytes of the loop counter, but that's far more complex than cascaded decfsz instructions. The only problem with cascaded decfsz appears to be in the initialization of the counter. > > Comments???? > > Thanks! > > Harold > > FCC Rules Online at http://www.hallikainen.com > > ________________________________________________________________ > The best thing to hit the internet in years - Juno SpeedBand! > Surf the web up to FIVE TIMES FASTER! > Only $14.95/ month - visit www.juno.com to sign up today! > > -- > http://www.piclist.com#nomail Going offline? Don't AutoReply us! > email listserv@mitvma.mit.edu with SET PICList DIGEST in the body -- ____________________________________________ Peter Donahue AMTS (Code Monkey) COM DEV International, R&D P (519) 622-2300 x2439 F (519) 622-8706 -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body