6.3 Subroutines

If the same collection (function) of instructions is executed several times in a program, writing the function every time makes the program difficult to understand. In addition, it makes the program larger since it also increases the instruction count.

To prevent this, programs should be developed by separating repeated functions from the main flow.
These separated functions are called "subroutines". (By contrast, the main program flow is called "main routine".)

Moving execution to a separated function is referred to as "calling a subroutine (subroutine call)" and subroutine call instructions (BSR and JSR instructions) are used for this purpose. They are written as follows:

BSR Subroutine name (Called with program counter relative addressing)
JSR @Subroutine name (Called with absolute address addressing)
* Subroutine name: Symbol prefixed to a subroutine

In the called subroutine, instructions are executed in ordinary order and control is returned to the source after execution is completed.
The RTS instruction is used to return execution to the source. This means that all subroutines end with the RTS instruction. This instruction is written as follows:

RTS (The RTS instruction has no operand)

After the RTS instruction is executed, processing resumes from the instruction next to the one which has called the subroutine.