Level 1, lesson 19

# 1.19 Storing individual characters

Storing individual characters works just the same. Keep that array of characters in mind. When we want to fetch a variable we write:

         my_var @

When we want to store a value in a variable we write:

         5 my_var !

Fetching only requires the address of the variable. Storing requires both the address of the variable and the value we want to store. On top of the stack is the address of the variable, below that is value we want to store. Keep that in mind, this is very important.

Let's say we have this program:

         32 string one                   \ define string one
         " Hans" one copy                \ initialize string one
         drop                            \ drop the address

Now we want to change "Hans" to "Hand". If we want to find out what the 4th character of string "one" is we write:

         32 string one                   \ define string one
         " Hans" one copy                \ initialize string one
         3 chars + c@                    \ get the fourth character

Remember, we start counting from zero! If we want to store the character "d" in the fourth character, we have to use a new word, and (yes, you guessed it right!) it is called 'C!':

         32 string one                   \ define string one
         " Hans" one copy                \ initialize string one
         3 chars +                       \ address of the fourth char
         char d                          \ we want to store 'd'
         swap                            \ get the order right
         c!                              \ now store 'd'

If we throw the character "d" on the stack before we calculate the address, we can even remove the 'SWAP':

         32 string one                   \ define string one
         char d                          \ we want to store 'd'
         " Hans" one copy                \ initialize string one
         3 chars +                       \ address of the fourth char
         c!                              \ now store 'd'

We will present the very same programs, but now with stack-effect- diagrams in order to explain how this works. We will call the index 'i', the character we want to store 'c' and the address of the string 'a'. By convention, stack-effect-diagrams are enclosed by parenthesis.

If you create complex programs this technique can help you to understand more clearly how your program actually works. It might even save you a lot of debugging. This is the first version:

         32 string one                   ( --)
         " Hans" one copy                ( a)
         3 chars                         ( a i)
         +                               ( a+i)
         char d                          ( a+i c)
         swap                            ( c a+i)
         c!                              ( --)

Now the second, optimized version:

         32 string one                   ( --)
         char d                          ( c)
         " Hans" one copy                ( c a)
         3 chars                         ( c a i)
         +                               ( c a+i)
         c!                              ( --)