Close

Bump to v2.2.1: adding DO, LEAVE, LOOP, and +LOOP

A project log for eForth for cheap STM8S gadgets

Turn cheap modules from AliExpress into interactive development kits!

thomasThomas 12/11/2016 at 11:370 Comments

I experimented a bit with the v2.2.0 vocabulary, and I quickly missed the DO .. LOOP structure.

Figuring out how DO .. LOOP should work took some time: after testing some Forth implementations, I decided to stick with the ANS Forth specification (STM8EF will never be ANS Forth compliant, but the standard makes testing much easier).

Here is an example for DO, LEAVE, and +LOOP:

: test DO R@ DUP . 3 = IF LEAVE THEN 2 +LOOP ; ok
10 -9 test -9 -7 -5 -3 -1 1 3 ok
10 2 test 2 4 6 8 ok

DO takes the start value for the index (e.g. -9) and the upper limit (e.g. 10) from the stack, inside the DO..LOOP structure R@ accesses the index on the return stack, the optional LEAVE cleans up the return stack and leaves the loop, +LOOP takes an increment (e.g. 2) from the stack and increments the index on the return stack. As long as the new index value is lower than the upper limit (e.g. 10) it goes back to the beginning of the loop.

The implementation does a signed compare of upper limit and index. I think that looping through negative numbers has advantages, but it also means that looping through addresses, using the start and the end addresses as parameters for DO won't just work (except when both addresses are lower than 0x8000).

The new words can be selected with the option flag "HAS_DOLOOP =1" in globalconf.inc. Except in CORE the option is enabled by default (89 bytes well spent).

I released the new STM8EF version v2.2.1 on GitHub. Binaries for the supported boards are also there.

Edit: I just checked the code size. Due to recent optimizations the co, including the new DO..LOOP words,de size of MINDEV including DO..LOOP is now 5320 bytes, down from 5332 bytes a week before. Bare-bones CORE is now just 4350 bytes.

Discussions