I wrote a Forth interpreter in Assembler (core set), but is there a standard way to define a Forth word using Assembler instructions or directly machine code?
Example
I want to define "+" (addition) using Assembler ("::" separate assembler instructions) like this
: + ASMSTART pop hl :: pop de :: add hl de :: push hl ASMEND ;
or directly giving the op codes (for Z80 in this case), which is easier:
: + OPSTART E1 D1 19 E5 OPMEND ;
where ASMSTART-ASMEND and OPSTART-OPMEND are builtin words made for this purpose.
Currently, "+" is a builtin in the interpreter code, like other arithmetic, logical and bit manipulation operations.
As ruvim says, typically CODE
and END-CODE
are used for this purpose, analogously to :
and ;
.
Also, a common way to implement an assembler in Forth is by using a separate vocabulary or wordlist named something like ASSEMBLER
, containing words corresponding to assembler mnemonics. This lends itself nicely to an RPN-syntax assembler. So for instance HL
could be defined in this vocabulary as a constant, and POP
as a word which assembles a pop instruction, using the value from the top of the stack to determine the register operand. Your definition of +
could then look something like
ASSEMBLER ALSO
CODE + ( x y -- sum )
HL POP
DE POP
DE HL ADD
HL PUSH
END-CODE