While trying to find out how Forth manages the dictionary (and memory in general), I came across this page. Being familiar with C, I have no problem with the concept of pointers, and I assume I understood everything correctly. However, at the end of the page are several exercises, and here I noticed something strange.
Exercise 9.4, assuming DATE
has been defined as a VARIABLE
, asks what the difference is between
DATE .
and
' DATE .
and exercise 9.5 does the same using the user variable BASE
.
According to the supplied answers, both phrases will give the same result (also with BASE
). Trying this with Win32Forth however, gives results with a difference of 4 bytes (1 cell). Here is what I did:
here . 4494668 ok
variable x ok
x . 4494672 ok
' x . 4494668 ok
Creating another variable gives a similar result:
variable y ok
y . 4494680 ok
' y . 4494676 ok
Thus, it looks like each variable gets not just one cell (for the value), but two cells. The variable itself points to where the actual value is stored, and retrieving the contents at the execution token (using ' x ?
) gives 0040101F
for both variables.
For exercise 9.5, my results are:
base . 195F90 ok
' base . 40B418 ok
These are not even close to each other. The answer for this exercise does however mention that the results can depend on how BASE
is defined.
Returning to normal variables, my main question thus is: why are two cells reserved per variable?
Additionally:
- Since only one cell contains the actual value, what do the contents of the other cell mean?
- Is this specific to Win32Forth? What happens in other implementations?
- Is this different for run-time and compile-time variables?
- How do answers to the above questions apply to user variables (such as
BASE
)?
EDIT1: Okay, so Forth also stores a header for each variable, and using the '
gives you the address of this header. From my tests I would then conclude the header uses just one cell, which does not correspond to all the information the header should contain. Secondly, according to the exercise retrieving the address of a variable should for both cases give the same result, which appears to contradict the existence of a header altogether.
My gut feeling is that this is all very implementation-specific. If so, what happens in Win32Forth, and what should happen according to the exercise?
'
) returns the address of the code that is executed for the word that follows. This can even be different for interpreted code and for compiled code... – Rudy VelthuisDATE
itself, the code for the variable puts the data address on the stack. So there is a big difference between' DATE
andDATE
itself. – Rudy Velthuis