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 heredjnz LoopCount, #:Loop
DAT
NumberOfLoopsToRun long1000
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
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_LOOPS loop .... djnz count,#loop
Uh... I'm a little slower than Dave Hein and JasonDorie.
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.
And hence, I have out-cheeky'd Dave.
mov counter, #500
shl counter, #1 ' double the value.
mov counter,#1477 movd counter,#1477>>9
It's very readable and needs not more longs than with a long in a constant poolAndy
[COLOR="red"]mov counter,#1477[/COLOR] movd counter,#1477>>9
That doesn't even compile. Besides, you get extra runtime as opposed to using a constant pool.mov counter,#1477 & $1FF movd counter,#1477 >> 9
Andy