mov LoopCount, #16
:Loop
' do something useful here
djnz LoopCount, #:Loop
djnz = Decrement <variable> and jump if not zero.
For any loop count greater than 511 you need to generate the value another way, as an immediate can't hold a value that large. Most people just create a variable in the DAT section and set it to the value needed, then copy it out, like this:
mov LoopCount, NumberOfLoopsToRun
:Loop
' do something useful here
djnz LoopCount, #:Loop
DAT
NumberOfLoopsToRun long 1000
mov count,#(NUM_LOOP>>27)
shl count,#9
or count,#(NUM_LOOPS>>18)
shl count,#9
or count,#(NUM_LOOP>>9)
shl count,#9
or count,#NUM_LOOP
loop ....
sub count,#1 wz
if_nz jmp #loop
For clarity, each of the preceding instructions takes a long value in cog memory, so doing it this way works, but it's very wasteful compared to the single MOV instruction and DAT statement version, which is 2 longs total compared to Dave's 7.
The djnz instruction is also one long, compared to the sub + conditional jmp of Dave's method. I'm quite sure Dave knows all this and is just being cheeky, but I thought it was worth pointing out because someone just starting out isn't likely to know the difference.
Comments
Try it, it's fun!
Jim
djnz = Decrement <variable> and jump if not zero.
For any loop count greater than 511 you need to generate the value another way, as an immediate can't hold a value that large. Most people just create a variable in the DAT section and set it to the value needed, then copy it out, like this:
Uh... I'm a little slower than Dave Hein and JasonDorie.
For clarity, each of the preceding instructions takes a long value in cog memory, so doing it this way works, but it's very wasteful compared to the single MOV instruction and DAT statement version, which is 2 longs total compared to Dave's 7.
The djnz instruction is also one long, compared to the sub + conditional jmp of Dave's method. I'm quite sure Dave knows all this and is just being cheeky, but I thought it was worth pointing out because someone just starting out isn't likely to know the difference.
And hence, I have out-cheeky'd Dave.
mov counter, #500
shl counter, #1 ' double the value.
Andy
Andy