Forth, anyone?

Any stories, collections or recollections of the earliest days of computers
or related projects
Post Reply
Martin
Posts: 744
Joined: 16 Feb 2018, 14:11
Location: Warwickshire

Forth, anyone?

Post by Martin »

I've spent most of my programming time, the last couple of months, writing a Forth. By its nature, Forth is a compiler, interpreter, and operating system - it's very compact and runs code much faster than most interpreted languages, such as BASIC or Python. My whole Forth fits in about 8K. It's an interesting way of working on an Arduino-style board because it's interactive - there's no edit, compile, upload, test, repeat cycle - you just type Forth commands and programs into the serial terminal, and they are executed immediately. Particularly nice way to test out I/O chips, timers and such, because you can just 'peek' and 'poke' their registers in real time, to see what happens.

I chose one of the TI MSP430 chips to write the initial version. You can buy one of TI's LaunchPad boards - superficially similar to an Arduino - but it's a better choice for developing a Forth, initially, because it has:
  • 16-bit architecture
  • Ferromagnetic RAM - which is similar to Flash, but faster and easier to write, and won't wear out in a lifetime, even if you write to it a million times per second.
  • Von Neumann architecture - so the processor can run code stored in RAM, as well as in the Ferro RAM (not that I ended up using that feature). The architecture allows a program to easily modify parts of its own code - or add new code. This is not easily possible with the Atmel chips used in the smaller Arduinos - a program can only modify its own Flash memory if it has a bootloader set to do that - and the default way Arduino chips are configured have the bootloader set so that the program code can't access it, except immediately following a reset.
The particular board I chose was the MSP-EXP430FR2433. The chip has 15 kB of Ferro RAM and 2 kB of RAM. It runs at 16MHz. So very similar to an Arduino, except that it's 16-bit rather than 8-bit.

Image

I wrote the whole thing in Assembler. Forth is peculiar in that once you've written the fundamentals, it becomes possible to write other parts in Forth itself. You're still using the assembler, but with a good macro assembler you can use macros to write Forth words in a sort of pseudo-Forth.

I used Alfred Arnold's excellent AS - which is free and open source. It targets a huge number of different microprocessors, including the Atmel ones used in Arduinos, 8080 and Z80, 6502, and some PIC chips. It's much faster to use than TI's own bloated CCS, assembling my whole Forth source code in a fraction of a second.

I may port it to Arduino, or to the Raspberry Pi Pico (ARM) next. It's been an interesting journey.

Anyone else interested in Forth, or ever played with it and maybe hated it?
Pchristy
Posts: 413
Joined: 16 Feb 2018, 13:57
Location: South Devon, UK

Re: Forth, anyone?

Post by Pchristy »

I do vaguely recall Forth. I think there was a Sinclair ZX80 clone that used it as an operating system, but it never caught on. I have a vague recollection about it using reverse Polish notation for mathematical equations, which was extremely counter-intuitive, IIRC. Or I might be confusing the reverse Polish bit with another of Sinclair's early calculators....!

It was all a long time ago!

:lol:
User avatar
Phil_G
Posts: 597
Joined: 15 Feb 2018, 23:32
Contact:

Re: Forth, anyone?

Post by Phil_G »

That was the Jupiter Ace, Pete. I have an unsoldered Jupiter Ace PCB right here, its significantly bigger than the ZX80/81 :D
Being compact, Forth has quite a following within retro computing circles and has spawned many even smaller, stack-based languages like S2
The FB members of 'Minimialist Computing' would be very excited to hear about it Martin.
I have dabbled, but it never really appealed to me to take it further. I just like assembler.
I found other peoples Forth code very difficult to follow!
Good work though!

Heres the minimalist computing link Martin, they'll love it, there's a strong Forth following on there, plus mint etc
https://www.facebook.com/groups/minimal ... ONOLOGICAL

Cheers
Phil
Martin
Posts: 744
Joined: 16 Feb 2018, 14:11
Location: Warwickshire

Re: Forth, anyone?

Post by Martin »

Yes, for maths it does use reverse polish - same as some of the classic HP calculators. It means you don't need an equals at the end, and you don't need to use parentheses to do the operations in the right order.

Say you want to calculate (3 + 4) x (5 + 6) and print the answer. In BASIC or C you'd do

BASIC: PRINT (3 + 4) * (5 + 6)
C: printf("%d\n", (3 + 4) * (5 + 6));

In Forth the numbers go onto a stack, and then the operators, like + pull the necessary numbers off the stack, do the operation and push the answer back to the stack. The stack is always last-in, first-out. You can picture it like one of those stacks of plates they have at restaurants - you push an extra plate on top of the stack, or pop a plate off the top of the stack.

Forth: 3 4 + 5 6 + * .

in Forth the . (pronounced 'dot') prints out the answer.

It takes a bit of getting used to, but it can be surprisingly elegant.

For operations where the order of the inputs matter, like minus, or divide, there are stack words that manipulate the top few items on the stack. SWAP, swaps the top two items, DROP discards the top item, OVER makes a copy of the top-but-one item on the stack and places it on the top... and so on.

When you're writing a Forth program, you combine existing words and calculations to create new words that become part of the language. If you're careful with your choice of words, the top level of your program can read surprisingly like English. You might create words like RED, GREEN, TOGGLE, SECOND, DELAY, and so on - and then the top level of a BLINK program would be:

: BLINK
BEGIN
RED LED TOGGLE
1 SECOND DELAY
BUTTON_PRESS? UNTIL
;
the colon and semi-colon are Forth's way of marking the beginning and end of new word you're defining - in this case BLINK. BEGIN and UNTIL are standard parts of the language, and the other words you'd have to define before you use them to create BLINK.
Last edited by Martin on 21 Nov 2022, 19:55, edited 2 times in total.
Martin
Posts: 744
Joined: 16 Feb 2018, 14:11
Location: Warwickshire

Re: Forth, anyone?

Post by Martin »

Phil_G wrote: 21 Nov 2022, 19:17 I just like assembler.
Writing your own Forth in Assembler - even if you don't intend to actually use that Forth! - is a very fun assembler project.
I recommend jonesforth as a good tutorial - it's the actual assembler source code for a Forth (for x86, but that doesn't matter) interspersed with lots of explanation and ASCII diagrams about the inner workings of Forth.
Pchristy
Posts: 413
Joined: 16 Feb 2018, 13:57
Location: South Devon, UK

Re: Forth, anyone?

Post by Pchristy »

Jupiter Ace! Yes, that's the one! Thanks for reminding me, Phil!

I never saw one in the flesh, but in the pictures it looked just like a ZX80, though you couldn't tell the size!

I was taught computing initially on an IBM mainframe running Fortran IV. Very unexciting! You wrote your program out long-hand on special sheets of paper, dropped it in the "In" tray in the computer room, and a week later got back reams of paper (there goes the old rain forest!) with a red ink comment on the end saying "Terminated - stuck in loop"!

Things improved a lot when we got our hands on a DEC PDP-8 running Focal. Focal took up 4K of ferrite ring memory, and ours had a 4K extension on it, allowing 4 teletypes to run simultaneously - each having access to 1K of RAM!

Some years later I built a UK 101 kit which came with Microsoft Basic, which seemed quite good until the BBC "B" came along and showed how Basic *should* be written! Never had much time for Microsoft stuff since! :lol:

All my computers now run Slackware Linux, except for an old Apple Mac, and since Apple no longer support it, I'm thinking of putting Slackware on that, too!

--
Pete
Martin
Posts: 744
Joined: 16 Feb 2018, 14:11
Location: Warwickshire

Re: Forth, anyone?

Post by Martin »

The Jupiter Ace is very collectable now, and they go for silly money on ebay.

Here's one: https://www.ebay.co.uk/itm/204145268889
Pchristy
Posts: 413
Joined: 16 Feb 2018, 13:57
Location: South Devon, UK

Re: Forth, anyone?

Post by Pchristy »

Good grief! Back in the day, they couldn't give them away!
Post Reply