- In slot 2, save the program counter to the D3 register
- In slot 3, load the program counter with the subroutine address (found in slot6, shown in GREEN)
- The subroutine is executed now. The first instruction saves the return address, that was stored in D3, in the workspace at slot 15.
- When the subroutine has finished, the program counter is loaded with the saved program counter and execution continues at slot 7 in the main program.
- Back in the main program, instruction 4 is executed, followed by instruction 5 and then a jump to the next block.
By convention, the slot to return to is always slot 7.
In most cases (but not in this example), the subroutine will also create its own "stack frame" by using a new value for the workspace pointer WP. This makes nested and recursive functions easy.
In this example, the call instruction is in slot 2 and 3. But it can of course also be placed in other slots. Note that this mechanism utilizes the possibility to put instructions in any order that you want.
ADDRESSING WITHIN 256K RETURN LOCATIONS WITH ONLY 16 BITS
The program counter has 16 bits, where bit zero is always 0 because instructions are only on even addresses. Each value in the PC addresses a sequence of 8 instructions. So the maximum number of instructions that can be addressed is 262144.
For the return address, only 16 bits have to be stored. The drawback is that within an 8-instruction block there can only be one subroutine call, because the return point is always in slot 7.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.