dsPIC30F Family Reference Manual
DS70049C-page 2-34 © 2004 Microchip Technology Inc.
2.9.2.5 DO Loop Restrictions
DO loops have the following restrictions imposed:
• choice of last instruction in the loop
• the loop length (offset from the first instruction)
• reading of the DOEND register
All DO loops must contain at least 2 instructions because the loop termination tests are
performed in the penultimate instruction. REPEAT should be used for single instruction loops.
The special function register, DOEND, cannot be read by user software in the instruction that
immediately follows either a DO instruction, or a file register write operation to the DOEND SFR.
The instruction that is executed two instructions before the last instruction in a DO loop should not
modify any of the following:
• CPU priority level governed by the IPL (SR<7:5>) bits
• Peripheral Interrupt Enable bits governed by the IEC0, IEC1 and IEC2 registers
• Peripheral Interrupt Priority bits governed by the IPC0 through IPC11 registers
If the restrictions above are not followed, the DO loop may execute incorrectly.
2.9.2.5.1 Last Instruction Restrictions
There are restrictions on the last instruction executed in a DO loop. The last instruction in a DO
loop should not be:
1. Flow control instruction (for e.g., any branch, compare and skip, GOTO, CALL, RCALL,
TRAP).
2. RETURN, RETFIE and RETLW will work correctly as the last instruction of a DO loop, but
the user must be responsible for returning into the loop to complete it.
3. Another REPEAT or DO instruction.
4. Target instruction within a REPEAT loop. This restriction implies that the penultimate
instruction also cannot be a REPEAT.
5. Any instruction that occupies two words in program space.
6. DISI instruction
2.9.2.5.2 Loop Length Restrictions
Loop length is defined as the signed offset of the last instruction from the first instruction in the DO
loop. The loop length when added to the address of the first instruction in the loop forms the
address of the last instruction of the loop.There are some loop length values that should be
avoided.
1. Loop Length = -2
Execution will start at the first instruction in the loop (i.e., at [PC]) and will continue until the
loop end address (in this case [PC – 4]) is pre-fetched. As this is the first word of the DO
instruction, it will execute the DO instruction again, re-initializing the DCOUNT and
pre-fetching [PC]. This will continue forever as long as the loop end address [PC – 4] is
pre-fetched. This value of n has the potential of creating an infinite loop (subject to a
Watchdog Timer Reset).
end_loop: DO #33, end_loop ;DO is a two-word instruction
NOP ;2nd word of DO executes as a NOP
ADD W2,W3,W4 ;First instruction in DO loop([PC])