Why does this code not work?
TIB 10 ACCEPT
TIB SP@ 1 cells - 10 cmove
In that code I tried to enter a string and store it in the terminal input buffer and later store it on the parameter stack.
But with .S I see that does not work.
Why does this code not work?
TIB 10 ACCEPT
TIB SP@ 1 cells - 10 cmove
In that code I tried to enter a string and store it in the terminal input buffer and later store it on the parameter stack.
But with .S I see that does not work.
MU!
Basically what you do here is a copy CMOVE to the area at the stack, on a Forth where SP@ is defined (it is not a standard word). So you destroy the stack. The answer is: such code is not supposed to work.
The question should not be " why doesn't this work?" but "under what circumstance has this the intended effect?"
Let us assume:
this Forth has an addressable and writable stack (if you think that's the same read up about Intel segment descriptors and the particulars of POP and MOV instructions)
SP@ points to the top of the stack at the moment is called.
the stack grows up. (if it grows down the situation is totally different)
at the start of the call the stack was empty.
The TIB and ACCEPT are red herrings and will be ignored.
Assuming a protected Forth, the last bytes are beyond the stack, and lead to a segmentation fault.
So you must readjust your thinking to low level if you want to use Forth. Think of memory as stack of letterboxes and start from there.
Consider very carefully what happens to the stack after each word. I have reproduced your code below, and annotated it with the stack depth at every point.
So when
SP@
is executed, it returns a pointer to stack element 2. The pointer is then decremented by one cell, resulting in a pointer to stack element 3 (because the stack grows downwards).cmove
then overwrites 10 bytes, i.e. 2 stack elements (guessing you're running a 64-bit forth). So stack elements 3 and 2 are changed. Finally,cmove
pops three elements from the stack, leaving only one. Which is unchanged.The parameter stack grows towards low memory
The main problem with the sample code is that the parameter stack grows towards low memory. So the starting point for the destination of the copy should be at a higher memory address (inside the existing/defined parameter stack). So instead of
it should be:
Memory allocation for the string
The next problem is that there is not enough storage for the string on the parameter stack. ACCEPT has left over one cell (four bytes on a 32-bit system), the actual number of characters. With sample input "user10181" (9 characters),
results in:
Forgetting about that extra element for the moment1, for the purpose of this elaboration, we allocate four cells on the parameter stack (the actual value, for example, 235, does not matter), 16 bytes on a 32-bit system:
The result of
TIB SP@ 1 cells + 10 cmove
is then:We see that three of the four cells (each with four bytes) have been overwritten by
cmove
(as expected).TIB is overwritten by subsequent input from the terminal
Unfortunately, our copied bytes are not as expected. Decoding the output for the three changed cells (that are in decimal), first from
to hexadecimal:
And then decoding as ASCII:
And reversing (we had the high memory first and the test platform is little endian):
That is the first ten characters of our second line,
TIB SP@ 1 cells + 10 cmove
. Thus it is clear that the terminal input buffer (TIB) is too temporary to be used in this case.The solution
The solution to the third problem is to have all the code compiled before we ask for user input. For instance, put it into a word,
inputOnStack
:The result is then:
That corresponds to "user10181" and the tenth character is "a" (most likely from the "a" in
inputOnStack
).Test platform:
sudo apt-get update; sudo apt-get install gforth
)1. A more advanced version of the code could use the actual number of characters. In any case, it should DROPped one way or the other to balance the stack if this code is be integrated into other code.