Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Threaded code
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Direct threading=== Addresses in the thread are the addresses of machine language. This form is simple, but may have overheads because the thread consists only of machine addresses, so all further parameters must be loaded indirectly from memory. Some Forth systems produce direct-threaded code. On many machines direct-threading is faster than subroutine threading (see reference below). An example of a [[stack machine]] might execute the sequence "push A, push B, add". That might be translated to the following thread and routines, where <code>ip</code> is initialized to the address labeled <code>thread</code> (i.e., the address where <code>&pushA</code> is stored). <syntaxhighlight lang="c"> #define PUSH(x) (*sp++ = (x)) #define POP() (*--sp) start: ip = &thread // ip points to &pushA (which points to the first instruction of pushA) jump *ip++ // send control to first instruction of pushA and advance ip to &pushB thread: &pushA &pushB &add ... pushA: PUSH(A) jump *ip++ // send control where ip says to (i.e. to pushB) and advance ip pushB: PUSH(B) jump *ip++ add: result = POP() + POP() PUSH(result) jump *ip++ </syntaxhighlight> Alternatively, operands may be included in the thread. This can remove some indirection needed above, but makes the thread larger: <syntaxhighlight lang="c"> #define PUSH(x) (*sp++ = (x)) #define POP() (*--sp) start: ip = &thread jump *ip++ thread: &push &A // address where A is stored, not literal A &push &B &add ... push: variable_address = *ip++ // must move ip past operand address, since it is not a subroutine address PUSH(*variable_address) // Read value from variable and push on stack jump *ip++ add: result = POP() + POP() PUSH(result) jump *ip++ </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)