Threaded code: Difference between revisions

Content deleted Content added
Leibniz (talk | contribs)
rm junk
Line 3:
 
The term '''''threaded code''''' is used in the [[Forth programming language|Forth]] and early versions of the [[B programming language|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.
Line 10 ⟶ 8:
Also, the [[instruction set]]s 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]]),and placing it in a [[subroutine]]. Then the programmer replaced every copy of that operation with much shorter "call" instruction.
Then the programmer replaced every copy of that operation with much shorter "call" instruction.
 
This process ([[refactor]]ing) 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==