+ ASSEMBLER CODE ----------------------------------------------------------------------
+
+ This is just the outline of a simple assembler, allowing you to write FORTH primitives
+ in assembly language.
+
+ Assembly primitives begin ': NAME' in the normal way, but are ended with ;CODE. ;CODE
+ updates the header so that the codeword isn't DOCOL, but points instead to the assembled
+ code (in the DFA part of the word).
+
+ We provide a convenience macro NEXT (you guessed the rest).
+
+ The rest consists of some immediate words which expand into machine code appended to the
+ definition of the word. Only a very tiny part of the i386 assembly space is covered, just
+ enough to write a few assembler primitives below.
+)
+
+: ;CODE IMMEDIATE
+ ALIGN ( machine code is assembled in bytes so isn't necessarily aligned at the end )
+ LATEST @ DUP
+ HIDDEN ( unhide the word )
+ DUP >DFA SWAP >CFA ! ( change the codeword to point to the data area )
+ [COMPILE] [ ( go back to immediate mode )
+;
+
+HEX
+
+( Equivalent to the NEXT macro )
+: NEXT IMMEDIATE AD C, FF C, 20 C, ;
+
+( The i386 registers )
+: EAX IMMEDIATE 0 ;
+: ECX IMMEDIATE 1 ;
+: EDX IMMEDIATE 2 ;
+: EBX IMMEDIATE 3 ;
+: ESP IMMEDIATE 4 ;
+: EBP IMMEDIATE 5 ;
+: ESI IMMEDIATE 6 ;
+: EDI IMMEDIATE 7 ;
+
+( i386 stack instructions )
+: PUSH IMMEDIATE 50 + C, ;
+: POP IMMEDIATE 58 + C, ;
+
+( RDTSC instruction )
+: RDTSC IMMEDIATE 0F C, 31 C, ;
+
+DECIMAL
+
+(
+ RDTSC is an assembler primitive which reads the Pentium timestamp counter (a very fine-
+ grained counter which counts processor clock cycles). Because the TSC is 64 bits wide
+ we have to push it onto the stack in two slots.
+)
+: RDTSC ( -- lsb msb )
+ RDTSC ( writes the result in %edx:%eax )
+ EAX PUSH ( push lsb )
+ EDX PUSH ( push msb )
+ NEXT
+;CODE
+
+(