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:
UPDATE_OUTPUT: 20 80 05 JSR LOAD_INPUT 20 90 05 JSR STORE_INPUT 20 A0 05 JSR LOAD_FILTER 20 A8 05 JSR FMUL 20 08 04 JSR FACC 20 10 06 JSR STORE_OUTPUT 60 RTS
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.
UPDATE_OUTPUT: E2 06 ENTER 80 05 LOAD_INPUT 90 05 STORE_INPUT A0 05 LOAD_FILTER A8 05 FMUL 08 04 FACC 10 06 STORE_OUTPUT F1 06 EXIT
(I just made this up; entirely fictional ... except for 6502 opcodes JSR and RTS ... do we need a better example?)
External links
- The Development of the C Language by Dennis M. Ritchie also puts B in the context of BCPL and C.
- Anton Ertl's explanatory page "What is Threaded Code?" describes different threading techniques and provides further references. "Charles Moore invented (indirect) threaded Code in 1970"