Unit 6.5 -- 1-D arrays and register tricks
See SILVER 5.2 specifically
PROG
Set A(i) i = 1 to 4
Then sum A(i), i = 1 to 4
PED
To see a short cut (register trick) for use with do loops
and arrays
CONCEPTS
A:array[1..n] of integer;
FOR I=1 to N DO
process A(i)
can be coded
LA regA,A
FOR LOOP stuff
process memory location 0(0,regA)
add 4 to regA
end FOR LOOP stuff
16.html
Unit 6.5
1-D arrays and register tricks
We observed in going through Unit Nineteen by hand that the
elements of A[I] that were loaded in the first loop were at
addresses:
90 94 98 9c
A[1] A[2] A[3] A[4]
We also observed in the second loop, that we summed the
contents of:
90 94 98 9c
A[1] A[2] A[3] A[4]
This indicates that we do not have to go through the five-
statement template of compute the value, Address of A + I*4,
each time through the loop.
At initialization, we should simply load into a register the
value of the first address inspected. Then, for each time
through the loop, we would increment that register by four.
Thus any loop with var going from 1 to n and processing
array[var] within the loop can be directly translated to
Assembly. This will be much shorter than the "brute-force"
method illustrated by Unit 19.
we translate the normal "For Loop" stuff. This includes the
incrementing of "var" the check of "var" against the value
of n and finally, the branching.
At the beginning of the loop, we initialize the value of
regA to the first element of the array to be processed
(read, stored, added to something). If the array begins at
element one, we simply do:
LA regA,array
Otherwise, if we had:
A:array[m..n1] of integer;
FOR var:=start to N
....
(Of course n1 would have to be greater than or equal to N)
LA reg,array+(start-m)*4
We translate the statements to be found inside the FOR loop.
Where array[var] appears within the FOR loop, we simply
write 0(0,regA).
at the end of the loop, we would have
A regA,F4
This will cause regA to point to the next sequential value
in the array.
Let us apply these techniques to our example
In line 16, we initialize I to 1 (part of our normal FOR
LOOP Stuff)
In line 17, we initialize register two to A. We are going to
use register two to contain the address within A that we are
processing. In other words, regA in our template will be
register two. Note that since we have decided to start A at
one, we have register two starting at the same value as the
array begins at.
Lines 18 through 20 are part of our normal translation of
the while.
In line 21, we process location 0(0,2) storing register one
there.
We increment register two by four. This is our regA in our
template. The increment of register one in line 23, is part
of the normal FOR LOOP stuff.
Finally, we have the end of this loop at line 24 and 25.
The second loop is translated similarly.
Sum is zeroed in register four. Note that we keep the sum
in register four. Register 1 is set to one at line 27 and
28. Line 27 reinitalizes "regA." Lines 29 through 31 are
our normal "FOR" loop stuff.
Line 32 does the processing of the value at "regA." In this
case, that processing involves the addition of the element
to register four. The increment of regA is in line 33.
Lines 34 through 36 are part of the normal "FOR LOOP" stuff.