LearnBuild toward a chatbotMoving and copying

Moving and copying

Aim. At the end of this lesson you can use a number in a sum and still have it afterwards.

Where the lessons are going

The sum uses each number more than once, so a program has to read a number without losing it. This lesson adds that: moving a number, copying it, and feeding several cells in one pass.

Move a number

Try it: the left cell holds 4. Press Step until the program has ended, and watch where the 4 goes:

Figure 1. Moving a number. Watch the left cell empty as the right cell fills. (interactive)

Copy a number

To keep a number, a program pours it into two cells at once and then pours one of those back. Choose the number to copy and watch the third cell: press Play, or drag Step back and forth:

The helper in the third cell filled up and emptied again, and the number ended in two places. The first loop poured the number into the copy and the helper at once, and emptied the original. The second loop poured the helper back into the original. Such a helper is called a scratch cell: empty before, empty after. The chat program is full of them. The slider stops at 9; the real program copies numbers up to 4,294,967,295 the same way.

Try it: this is the same program with the number 3. Press Step until the program has ended, and keep an eye on the third cell:

Figure 2. Copying a number. Watch the third cell, the helper: it fills and empties again. (interactive)

Fill two cells in one go

Try it: this is the copy program of step 2. Change it so that cell 0, cell 1 and cell 2 all hold the 5 at the end. Cell 3 is your scratch cell and must end empty:

Figure 3. The copy program, for you to change. All of cells 0, 1 and 2 must end at 5, and the scratch cell at 0. (interactive)

What you can do now

Read a number and keep it, and feed several cells in one pass. The 90 the sum starts with can now be used and still be there for the next product.

The first real arithmetic is 90 × 100, in a language that has no "times". How is that done?

What comes back later

The pass of step 3 that feeds several cells is the building block almost everything in the chat program is made of. Step 3 of lesson 3 shows why it is cheap.

Scratch cells have a price: they take room on the tape, next to the data. The rounding of step 5 of lesson 5 needs ten of them.