4 ms·
[Edit: yes the code above seems to be valid for the author's non-standard "jonesforth" implementation of Forth. ;CODE is defined in the ANS Forth standard to m
by dvdkhlng 12y ago
[Edit: yes the code above seems to be valid for the author's non-standard "jonesforth" implementation of Forth. ;CODE is defined in the ANS Forth standard to mean something very different.]
Sorry for the nit-picking, but the code you give is most likely wrong. Assembler code in Forth is enclosed in CODE...END-CODE . ;CODE is used to attach machine-code run-time semantics to words created with CREATE [1].
CODE RDTSC ( -- d )
RDTSC
EAX PUSH
EDX PUSH
END-CODE
Here RDTSC and PUSH are not compiled but executed immediately to output the corresponding machine code to the current definition (which also uses the name RDTSC albeit in a different vocabulary).
You can generate machine code by invoking the Assembler's words from Forth words (a "word" is what you call "functions" in other languages), which can be used as a simple macro facility or as a facility to dynamically generate machine code, do automatic register allocation etc.
ALSO ASSEMBLER
: my-macro RDTSC EAX PUSH EDX PUSH ;
CODE RDTSC my-macro END-CODE
BTW for those interested, this is the source of the x86 assembler, written in Forth, that ships with GNU forth [2]. The amd64 version [3] even supports SSE. When writing assembler code in Gforth, the non-standard ABI-CODE facility [4] is preferable over CODE..END-CODE, BTW.
[1] http://www.forth200x.org/documents/forth13-1.pdf http://www.forth200x.org/documents/forth13-1.pdf
[2] http://git.savannah.gnu.org/cgit/gforth.git/tree/arch/386/asm.fs http://git.savannah.gnu.org/cgit/gforth.git/tree/arch/386/as...
[3] http://git.savannah.gnu.org/cgit/gforth.git/tree/arch/amd64/asm.fs http://git.savannah.gnu.org/cgit/gforth.git/tree/arch/amd64/...
[4] http://www.complang.tuwien.ac.at/anton/euroforth/ef10/papers/ertl.pdf http://www.complang.tuwien.ac.at/anton/euroforth/ef10/papers...