Shop OBEX P1 Docs P2 Docs Learn Events
P2 Taqoz V2.8: Calling a Forth Word from Assembler Code — Parallax Forums

P2 Taqoz V2.8: Calling a Forth Word from Assembler Code

Christof Eb.Christof Eb. Posts: 1,195
edited 2024-06-27 08:54 in Forth

Hi,
perhaps this might be useful for others...

The idea is to be able to call a Forth Word from inside a code word. Of course you must be careful, that your register variables might get overwritten by the Forth code. While it is easy to call the Forth word, some effort must be done to enable correct return to the calling routine. - If anyone has a more elegant way to do it, feel free to post it...

484 := wPointer \ store for the Instruction word Pointer
$31 := doNext

: prtHello \ do something
   crlf
   ."    Hello"
   ."  from Forth"
   crlf

   \ return to assembler
   SUB:
      mov PTRA,wPointer ' \ return from Forth restore IP
      pop r1            ' \ discard one return level
      ret
   FORTH:
   ."  never reached "
;

' prtHello := aPrtHello  \ get start address


code cTest ( x -- x+6 ) ' \ calls a Forth word
   add a,#1
   mov wPointer,PTRA      ' \ save IP
   mov PTRA,##aPrtHello
   call #doNext
   add a,#5
   ret
end


: cT
   1 cTest . \ use the code word
;

Have Fun! Christof

Sign In or Register to comment.