SuperForth for the Tatung Einstein, showing a SuperForth 1.12 session defining a word
← Back to Courses
module
21

Tables Of Numbers

Introduction

A variable holds one number. Seven temperatures for the week would need seven variables with seven names, and a loop could not get at them, because a loop has a counter, not a list of names.

An array is one name for a row of numbered cells. Give it the number and it gives you the cell.

ARRAY

Screen 3 of UTILITY has the words.

7 ARRAY TEMPS

makes seven cells called TEMPS, each holding 0. The cells are numbered from 0 to 6.

TEMPS works like a variable that wants to be told which one: put the cell number on the stack first, and TEMPS replaces it with that cell's address. After that, everything from S11 applies.

ToType
store 42 in cell 242 2 TEMPS !
fetch cell 22 TEMPS @
print cell 22 TEMPS ?
add 5 to cell 25 2 TEMPS +!

Cells numbered from nought, and a loop counter that starts at nought: they were made for each other.

The Code

NEWFILE UTILITY
3 LOAD
7 ARRAY TEMPS
: FILL-UP 7 0 DO I 10 * I TEMPS !
LOOP ;
: SHOW 7 0 DO I TEMPS ? LOOP ;
FILL-UP SHOW

Starting from

A fresh boot, with SuperForth just started.

What you should see

FILL-UP SHOW
0 10 20 30 40 50 60 ok

An array filled by one loop and printed by another

In FILL-UP, I is used twice: once to work out the value, ten times the cell number, and once to say which cell to put it in.

Stay Inside The Lines

SuperForth does not check the cell number. TEMPS has cells 0 to 6. Ask for cell 9 and it will calmly work out where a cell 9 would be, which is somewhere in the memory after the array, belonging to something else. Nothing good can come of reading from there, and storing there overwrites whatever it was.

So the limit of every loop over an array should be the same number you gave ARRAY. It is a good use for a constant.

Bytes

CARRAY makes an array of single bytes instead of full numbers. A byte holds 0 to 255, which is plenty for a character code or a small count, and takes half the room. Bytes have their own fetch and store, C@ and C!:

5 CARRAY BYTES
200 0 BYTES C!
0 BYTES C@ .

Text With A Name

The same screen has STRING:

STRING NAME "GAVIN"

NAME now leaves the address of that text. Two words turn the address into print. COUNT takes it and leaves what TYPE wants - where the letters start, and how many there are - and TYPE prints them:

NAME COUNT TYPE

The length is kept in the text's first byte, in front of the letters, which is where COUNT finds it.

Change One Thing

  • Change FILL-UP to store the squares of the cell numbers.
  • Store 99 in cell 3 by hand and run SHOW again.
  • Change both loops' limits from 7 to 5. What happens to cells 5 and 6?
  • Type NAME C@ . What is that number? Change the name and try again.

Exercises

21.1 Define TOTAL ( -- n ) to add up everything in TEMPS.

21.2 Define BIGGEST ( -- n ) to find the largest number in TEMPS.

21.3 Define GREET to print HELLO and then the name held in NAME.

21.4 Use TOTAL to print the average.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
ARRAY? or STRING?Screen 3 of UTILITY is not loaded.
A number instead of the cell's contentsYou left out the @. The array's name gives an address, like a variable's.

Summary

n ARRAY name makes n cells numbered from 0; i name gives cell i's address for @, !, ? and +!. CARRAY does the same for bytes, with C@ and C!. Nothing checks the cell number. STRING name "text" names a piece of text, and COUNT TYPE prints it.

Next

S22, Colour And The Screen - clearing the screen, colouring it, and redesigning its letters.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.