Close

Subroutines

A project log for Kobold K2 - RISC TTL Computer

A 16 bit RISC computer with video display, from just a few TTL and memory chips.

roelhroelh 09/15/2019 at 10:240 Comments
This is the instruction sequence for subroutines:

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. 

So there is no "near or far" madness in CALL and RETURN instructions. Return addresses are just a single 16 bit word (instead of two 16 bit words). This makes calling and jumping faster and saves memory space. 

Discussions