I was following an example, trying to explain to myself what it does:
: place \ ptr len ptr2
2dup \ ptr len ptr2 len ptr2
>r >r \ ptr len ptr2
char+ \ ptr len (ptr2 + 1)
swap \ ptr (ptr2 + 1) len
chars \ ptr (ptr2 + 1) (len * char)
cmove \ --
\ from to how-many
r> r> \ ptr2 len
c! ; \ len = ptr2 ???
\ s" Hello! " name place
It all made sense, until the last instruction... where did I go wrong?
EDIT:
I've added some tracing:
: place \ ptr len ptr2 |
2dup cr .s \ ptr len ptr2 len ptr2 | <5> 16490736 5 2126333248 5 2126333248
>r >r cr .s \ ptr len ptr2 | <3> 16490736 5 2126333248
char+ cr .s \ ptr len (ptr2 + 1) | <3> 16490736 5 2126333249
swap cr .s \ ptr (ptr2 + 1) len | <3> 16490736 2126333249 5
chars cr .s \ ptr (ptr2 + 1) (len * char) | <3> 16490736 2126333249 5
cmove cr .s \ -- | <0>
\ from to how-many |
r> r> cr .s \ ptr2 len | <2> 5 2126333248 ok
c! ; \ ptr2 = len ??? |
\ s" Hello! " name place
I think the first part of Will Hartung's answer is correct.
The string representation that is being used is as he described, namely a character count and then the actual string.
So in your example c!
is storing the length of the string in the the first cell of the memory that starts with ptr2
.
So if you wanted to retrieve your string you would only need to know the address, you could then fetch from that address to get the length n, and the fetch n chars starting at the address + 1.