Page 2 of 2
Re:
Posted: Thu Dec 31, 2020 3:25 am
by JimBoyd
Inline assembly with ITC or DTC isn't common because, unlike STC, you don't gain much. Using a headerless code definition followed by an ordinary colon definition is often more flexible anyway. Even with something like FIG-Forth, which wasn't designed to use headerless words, it's easy to write a defining word that defines a headerless primitive. Many would argue, however, that inline assembly is a sign that you need to factor your definition.
What about inline high level Forth? Here is an example from Fleet Forth, an ITC Forth. I made (ABORT") , the word compiled by ABORT" , a primitive that has inline high level Forth.
If there is an error, the speed penalty of the high level Forth in (ABORT") is not a problem since we're aborting anyway.
If the top of stack is zero, (ABORT") increments IP past the inline string and jumps to POP, getting it's job done fast.
Since (ABORT") starts as a CODE word and stays at low level if there is no error, it greatly reduces the speed penalty of error checking.
Re: Mixing assembly into a colon definition
Posted: Thu May 18, 2023 12:20 am
by JimBoyd
A minor update. The source for my Forth's # used to be this:
Code: Select all
HEX
: # ( D1 -- D2 )
BASE @ UD/MOD ROT
INLINE
0 ,X LDA, 0A # CMP, CS IF,
6 # ADC,
THEN,
30 # ADC, 0 ,X STA,
END-INLINE
HOLD ;
It is now this:
Code: Select all
: # ( D1 -- D2 )
BASE @ UD/MOD ROT
>ASSEM
0 ,X LDA #10 # CMP
CS IF 6 # ADC THEN
#48 # ADC 0 ,X STA
LABEL HOLD.BODY
SEC
' HLD >BODY C@ # LDY
DEX DEX
UP )Y LDA 1 # SBC UP )Y STA
0 ,X STA INY
UP )Y LDA 0 # SBC UP )Y STA
1 ,X STA
LABEL C!.BODY
2 ,X LDA 0 X) STA
POPTWO JMP END-CODE
HOLD and C! have no bodies. Their code fields point into the low level portion of # and SIGN is now a primitive.
Code: Select all
CODE HOLD ( C -- )
HOLD.BODY LATEST NAME> !
END-CODE
CODE C! ( C ADR -- )
C!.BODY LATEST NAME> !
END-CODE
CODE SIGN ( N -- )
1 ,X ASL ASCII - # LDA
0 ,X STA
' HOLD @ CS BRAN
POP JMP END-CODE
Although # is much larger, less memory is used for these words than if # , HOLD and SIGN were high level. A savings of a few bytes and a speed up from mixing low level code in a high level word.