Close

DOER / MAKE, ;AND and UNDO on the Microprofessor

A project log for Exploring the Microprofessor

A journey to explore and better understand the Multitech Microprofessor machines in my retro computer collection

michael-wesselMichael Wessel 03/12/2022 at 02:260 Comments

So, my whole Forth-learning endeavor started about 3 months ago when I couldn't wrap my head around how to write a recursive version of the Factorial function with the Microprofessor Forth. I quickly found Leo Brodie's excellent forthward-thinking "THINKING FORTH" book, and learned that this can be a use-case for DOER/MAKE. However, none of the 6 printed versions from the Appendix in the book would work out-of-the box on the Microprofessor, and what was worse, I only understood 20% of it! Having a PhD in computer science with quite a bit of background in programming languages, I couldn't accept this defeat by a > 50 year old language on an almost 40 year piece of Z80 hardware. So I had to figure it out, and it really took me down the FORTH rabbithole.

I became serious about learning FORTH. Starting with Brodie's other excellent book, "Starting FORT", the meager MPF-IP Forth manual, and a lot of experiments and Trial&Error, here I am, 3 months later, with a fully functional version of DOER, MAKE, &AND, and UNDO!

I will make one more MPF-IP Forth video in which I am going to share my findings - but here is already the code and examples as an exclusive preview to the upcoming video ;-) 

Or, more readable: 

: NOTHING ;
: DOER CREATE ' NOTHING , DOES> @ >R ;
VARIABLE MARKER 
: (MAKE) R> DUP 2+ DUP 2+ SWAP @ 4 + ! @ ?DUP IF >R THEN ;  
: MAKE COMPILE (MAKE) HERE MARKER ! 0 , ; IMMEDIATE
: ;AND COMPILE EXIT HERE MARKER @ ! ; IMMEDIATE
: UNDO ' NOTHING <COMPILE> ' ! ;

If you have no idea what that means, why it looks like an Ancient Alien Language, or what it is good for, then maybe it will motivate you to learn FORTH yourself! It is very rewarding once you figure it out.

Or watch my videos if you'd like to learn more about defining and compiling words, vectored execution, and similar black magic Death Star the Dark Side of the FORTH kind of stuff!

Here is a use-case. However, direct recursive definitions (words) are only one possible application for (the vectored execution of) DOER/MAKE (but the one I was after for):

DOER FAC 
: SETUP MAKE FAC DUP 1 > IF DUP 1- FAC * THEN ; 
SETUP
3 FAC . -> 6  

 Or, even indirect recursion is possible:  

DOER ODD
DOER EVEN

: SETUPODD  MAKE ODD  DUP 2 < IF ELSE 1- EVEN THEN ; 
: SETUPEVEN MAKE EVEN DUP 2 < IF 1 SWAP - ELSE 1- ODD THEN ;

SETUPODD
SETUPEVEN 

10 EVEN . -> 1
10 ODD  . -> 0
11 EVEN . -> 0
11 ODD  . -> 1

The Brodie book gives this example (slightly adjusted by myself), which also demonstrates :AND and UNDO:

DOER JOE  
: SETUPJOE MAKE JOE ." HI JOE " ;
SETUPJOE
JOE -> HI JOE

FORGET SETUPJOE
: SETUPJOE MAKE JOE ." HELLO JOE " 
                    ;AND ." NOW! ";
SETUPJOE -> NOW!  
JOE -> HELLO JOE 
UNDO JOE
JOE ->  

Stay tuned for the video soon! 

Discussions