.. vim: ft=rst showbreak=--ยป\ noexpandtab fileencoding=utf-8 nomodified wrap textwidth=0 foldmethod=marker foldmarker={{{,}}} foldcolumn=4 ruler showcmd lcs=tab\:|- list tabstop=8 noexpandtab nosmarttab softtabstop=0 shiftwidth=0 linebreak :date: 2026.01.15 21:00:15 :_modified: 1970.01.01 00:00:00 :tags: MHF-002 :authors: Gilhad :summary: FORTH_25.01.10_Intro :title: FORTH_25.01.10_Intro :nice_title: |logo| %title% |logo| %HEADER% |MemxFORTHChipandColorfulStack.png| FORTH_25.01.10_Intro -------------------------------------------------------------------------------- **FORTH** is interesting language, especially for embeded systems. See `wiki `__, get book `Starting FORTH `__, or find any other resources. It is used in critical places, see `Forth in Space Applications `__. **FORTH** is a stack-oriented programming language and interactive integrated development environment designed by Charles H. "Chuck" Moore and first used by other programmers in 1970. **FORTH** uses stack and `Reverse Polish notation `__ (**RPN**) instead of infix notation, so it does not need brackets for evaluation equations and functions can have any number of input parameters as well as any number of output parameters. Infix: **( 1 + 2 ) * ( 3 + 4 ) * 22** - try to explain in details, how this should be computed Postfix: **1 2 + 3 4 + * 2 SQUARE *** - take 1 and 2, add it, take 3 and 4, add it, multiply the results, take 2, square it then multiply the results Maybe you will use postfix to explain infix notation :) The **1**, **2**, **3** and **4** are numbers, they are simply put onto stack. The **+**, ***** and **SQUARE** are all words (how FORTH calls functions and subroutines) and each take 2 parameters from stack and return 1 result. (Word **C@** (character at) get address from stack, read charecter from there and put it on stack (~ **PEEK**). Word **C!** (character store) read address from stack, then read character from stack and write it to the address (~ **POKE**). Words **AND**, **OR** and **NOT** are binary **&**, binary **|** and binary **negation**. ) New words are usually defined by **colon :** followed by **new_name** then words to make the function and finished by **semicolon ;** For example, on **ATmega** based **Arduino** (like **UNO**, **Nano**, **Mega**) the system **LED** is attached to port **B**, pin **7**, which Arduino calls **D13**. So we can define some usefull words like this: **: SetupLED DDRB C@ bit_7 OR DDRB C! ;** \ read direction flags of port B, set 7. bit on and write back, ~ **pinMode(SYSTEM_LED,OUTPUT);** **: LED_ON PORTB C@ bit_7 OR PORTB C! ;** \ read port B, set bit 7 on and write back ~ **digitalWrite(SYSTEM_LED,HIGH);** **: LED_OFF PORTB C@ bit_7 NOT AND PORTB C! ;** \ read port B, set bit 7 off and write back ~ **digitalWrite(SYSTEM_LED,LOW);** **: LED_CHANGE bit_7 PINB C! ;** \ write bit 7 to PINB so the output just change value ~ **digitalWrite(SYSTEM_LED, ! digitalRed(SYSTEM_LED));** The usual routine with **INO** files is, that you - make change in **INO** file - save it - compile it - upload it (Arduino restarts) - get to the point, where change should manifest - observe it - see what else need change - start from begin When **FORTH** is installed, you have **interactive shell** so it goes different way: - open Serial connection to Arduinouse some wordssee what need changewrite new word or edit old onerepeat - use some words - see what need change - write new word or edit old one - repeat In both ways you have to write new commands, but with FORTH installed you do not need to compile, upload (which restarts Arduino) and then again set Arduino to required state. You just use the new word immediately, without reseting Arduino at all. When debugging some circuits and modules (maybe on breadboard), where you need set some pin high in middle of long test, it is really convenient to just do so, without all the recompiling and restarting everything again and again. I found it really convenient when I needed to test some connections on breadboard, where some wire was just loose, or misplaced. Also I wrote some more complicated tests on spot and reused them as many times as was convenient. When some words are use more often, they may be saved in file on PC/NB and then simply copy-pasted at begin of every session, or if you serial program allows it (like **picocom**, which I use and recomend) simply send as file over the serial. And they may be added to the FORH vocabulary permanently, but it need FORTH recompilation and reupload, so it is better to do it with already debugged and fully tested words. .. |MemxFORTHChipandColorfulStack.png| image:: MemxFORTHChipandColorfulStack.png :width: 250 :align: top :target: MemxFORTHChipandColorfulStack.png