Search code examples
forth

Return stack operations generate "invalid memory address" in Gforth 0.7


I'm learning Forth here, and I've got onto return stack operations.

So using the console on Ubuntu 11.04 x64 I am trying to get the TOS onto the return stack but this happens:

1 2 3 4 5 ok
>r 
:36: Invalid memory address
>R>>><<<
Backtrace:

What am I doing wrong here?


Solution

  • >r is itself a word and needs to return to the interpreter. When >r is executed as in the question it adds a new return address, an invalid one.

    Instead use >r inside a (new) word. Note that the items added to the return stack must be removed before that word ends - the return stack must be in the same state as when the word started executing.

    Loops are actually an example of an application of the return stack inside words (and thus your own use of the return stack must also be balanced within loops just as it must be balanced within a word).