I've tried the following code after reading the docs:
create buff 128 allot
buff 128 stdin read-line throw
I was hoping that this would get me a char for each successive address of buff, but I'm getting this weird number in there:
buff @ ok
. 3689349013085184353 ok
What am I missing here?
buff
put the address of your buff
variable on the (data) stack. The memory by that address contained the input received from stdin
, something like this:
Address Value
------- -----
N+0 0x61
N+1 0x61
N+2 0x61
N+3 0x61
N+4 0x61
N+5 0x33
N+6 0x33
N+7 0x33
... ...
The @
word transformed the address left by buff
into an integer value by that address. But since you've (apparently) got a 64-bit gforth
version, @
returned a 64-bit, i.e. 8-byte, value starting at the given address, i.e. 0x3333336161616161
, which is 3689349013085184353
in decimal. The .
word just showed you that number.
If you want to fetch a particular byte, use c@
instead of @
:
buff c@ .
That'll give you the code of the first character in the buffer (0x61
or 97
). If you want to get the second item, increment the address before executing c@
, like this:
buff 1+ c@ .
Similarly, this will get you the sixth character's code:
buff 5 + c@ .