Conditional Branches, Calls, and Returns
5-12
The conditional branch instructions are BCND (branch conditionally) and
BANZ (branch if currently selected auxiliary register is not equal to 0). The
BANZ instruction is useful for implementing loops.
5.4.4
Conditional Calls
The conditional call (CC) instruction is executed only when the specified condi-
tion or conditions are met (see Table 5–3 on page 5-10). This allows your pro-
gram to choose among multiple subroutines based on the data being pro-
cessed. If all the conditions are met, the PC is loaded with the second word
of the call instruction, which contains the starting address of the subroutine.
Before branching to the subroutine, the processor stores the address of the
instruction following the call instruction—the return address—to the stack. The
function must end with a return instruction, which will take the return address
off the stack and force the processor to resume execution of the calling pro-
gram.
By the time the conditions of the conditional call instruction have been tested,
the two instruction words following the call instruction have already been
fetched in the pipeline. If all the conditions are met, these two instruction words
are flushed from the pipeline so that they are not executed, and then execution
continues at the beginning of the called function. If the conditions are
not met,
the two instructions are executed instead of the call. Because there is a wait
cycle for conditions to become stable, the conditional call takes one more cycle
than the unconditional one.
5.4.5
Conditional Returns
Returns are used in conjunction with calls and interrupts. A call or interrupt
stores a return address to the stack and then transfers program control to a
new location in program memory. The called subroutine or the interrupt service
routine concludes with a return instruction, which pops the return address off
the top of the stack and into the program counter (PC).
The conditional return instruction (RETC) is executed only when one or more
conditions are met (see Table 5–3 on page 5-10). By using the RETC instruc-
tion, you can give a subroutine or interrupt service routine more than one pos-
sible return path. The path chosen then depends on the data being processed.
In addition, you can use a conditional return to avoid conditionally branching
to/around the return instruction at the end of the subroutine or interrupt service
routine.
If all the conditions are met for execution of the RETC instruction, the proces-
sor loads the return address from the stack to the PC and resumes execution
of the calling or interrupted program.