Unit 6.9 -- 2-D Arrays, row by row, all elements used

PROG

See the following program's comment lines.

PED

to  learn  how  to  do two-dimensional arrays  using  doubly
nested loops processing row by row.

CONCEPTS

A:array[k..m,x..n)] of integer;
FOR i:=k to m
  FOR j:=x to n
   process A(i,j)

gets converted to

LA regA,A
FOR LOOP 1 stuff
  FOR LOOP 2 stuff
    process the stuff at memory location 0(0,regA)
    add 4 to regA
  END OF FOR LOOP 2 stuff
END OF FOR LOOP 1 stuff

SF

none

20.html
     
Unit 6.9 -- 2-D arrays, row-by-row, all elements used

Processing of two-dimensional arrays usually involves  loops
going row by row or column by column.

We  are  going to proceed to the format for using  "register
tricks" for these cases right away.

When  we  have a two-dimensional array in memory, sometimes,
say  dimensioned 1 to m and 1 to n, we know we are going  to
use all m*n elements.  In other words, we know precisely how
big the array is going to be.

In  other  cases,  we don't know how big the two-dimensional
array  will be.  So we pick some dimensions that we know  or
hope  are  going  to be bigger than the dimensions  actually
used.   Thus,  only  the  upper-left  hand   corner  of  the
two-dimensional array declared is used.

It  turns  out  that  it  is somewhat  easier,  particularly
conceptually, to deal with row by row cases when we use  the
entire two-dimensional array allocated.  That is, we fill up
the entire declared row for each element.

This  is illustrated in the template on page 141.  You  will
note  the array is dimensioned from k to m and x to n.   The
first  variable, i, goes from k to m.  The second  variable,
j, goes from x to n.  In other words, the start and stop for
each   for  loop  are  precisely  the  beginning  and   last
subscripts for each dimension.

Thus,  as we go row by row, the address of the last  element
of  row  i, is only four less than the first element of  row
i+1.  Thus, the normal increment of the address in the array
by  four, is sufficient to make the array pointer go to  the
next  element to process at the end of a row as well  as  in
the middle of a row.

As you can see in the template, we simply start "regA" at A.
Processing  of  A[i,j] is done in the inner  loop,  followed
immediately, by incrementing regA by four.

Our sample program takes a 3 by 3 array, with each dimension
declared  1  to 3.  Our program inserts the value  i+j  into
each element, A[i,j], of the array.

You will note that the first subscript of A in the dimension
is  from 1 to 3.  The second subscript of A in the dimension
is  from 1 to 3.  And the inner loop goes from 1 to 3.  This
means we can use the template of this unit, rather than  the
more complicated template in the next unit.

In  this example, "regA" is 3.  We start by initializing  it
with  the  address  of A at line 17.  Lines  18  through  21
represent "FOR LOOP1 STUFF."

Lines  22 to 25 represent "FOR LOOP 2 stuff"  Note  that  we
keep I and J in registers 1 and 2, respectively, rather than
loading and storing them each time.

Lines 26 to 28 correspond to the commandment to "process the
stuff at memory location of 0(0,regA)"  In this case, we add
I,  in register one, to J, in register 2, leaving the sum in
register  4.  It is then deposited at "0(0,3)" where  "regA"
is 3.

The  next  commandment in our template is add four to  regA;
this  is done by "A 3,F4"  Lines 30 to 32 represent the "END
OF  FOR LOOP2 STUFF."  Lines 33 to 35 represent the "END  OF
FOR LOOP1 STUFF."