| |||||||||
For multi-threaded programming, see Thread (computer science)
The term threaded code is used in the Forth and early versions of the B programming languages. It means a form of code consisting entirely of subroutine calls, written without the subroutine call instruction, and processed by an interpreter (Forth) or the CPU (B), which jumps to each successive piece of basic function code in turn.
Motivation
In early computers, memory was very expensive. So programmers spent a lot of time trying to find ways to squeeze their programs so they would fit in the memory available. Also, the instruction sets were so primitive that even simple operations like printing a character or dividing one number by another number required quite a few instructions.
Instead of writing out every step of such operations in every part of the program where it was needed (possibly using a macro), programmers saved memory by writing out every step of such operations exactly once (Wiki:ExactlyOnce), in a subroutine. Then the programmer replaced every copy of that operation with much shorter "call" instruction.
This process (refactoring) is still commonly used today by programmers and wiki editors, although today we do it for different reasons.
Many subroutines were squeezed so much that they became little more than a list of subroutines:
Is there any way to make this 19 byte subroutine any smaller ?
Do you see that "20" byte that is repeated over and over ? The indirect threaded version of this routine eliminates that, saving a few bytes.
(I just made this up; entirely fictional ... except for 6502 opcodes JSR and RTS ... do we need a better example?)