Unit 6.6 -- Array Example 6
PROG
Copies Array AX to Array B
Then will Copy Array AX to Array C
PED
To learn how to use equates, "EQU", to get the length and
end address of an initialized array.
To learn how to use these to reduce the number of registers
in a do loop.
CONCEPTS
Given an initialized array of integers (let's call this
array B), we can determine its length and end as follows:
B EQU *
DC's for each number (no label required)
BLENGTH EQU (*-B)/4
BEND EQU *
One can then determine if we should leave a loop, by writing
C regA,=A(BEND)
BE loopend
where "regA" steps through the array
or by writing
C regB,=A(BLENGTH)
BE loopend
where "regB" is a counter of elements processed so far
SF
One can use items appearing in the label field of EQU, DC or
instruction formats in the "=A(label)" statement.
17.html
Unit 6.6
Array Example 6
Very often in Assembler Programs, we desire to create a
block of constant data. Sometimes, these blocks or data
constitute various sorts of "control blocks" such as the
"Data Control BLock" in the IBM Operating System that
defines the block length, name, record length, etc. for a
data set. We need to have the length somewhere so we can
copy it.
In our examples, we will define a block of data at "AX" that
contains several integers. In order to copy, sum, or
otherwise process such a block of data, we need to know it's
length in some manner. We could have a separate DC defining
that length. That is, we can have ten items of data defined
as DC's followed by another variable which we call LENGTH.
We would write, LENGTH DC F'10'. If the maintenance
programmer added an item to list, he would have to change
the DC to "DC F'11'" If one were removed, instead, then the
"DC F'10'" would be changed to "DC F'9'"
Very often, the maintenance programmer would forget this,
creating problems.
To get around this, we can define the length with Equates,
as follows:
A EQU *
DC F'3'
DC F'5'
DC F'9'
ALENGTH EQU (*-A)/4
AEND EQU *
In this case, the ALENGTH will have the value 3. "*"
represents the address where the next item will be put. A
represents the ADDRESS of A. Thus, "(*-A)" as an expression
stands for the number of bytes in the block of storage.
Finally, when we divide by four, this gives the number of
integers. (There are four bytes per integer.)
AEND simply has the address of the first byte of storage
after A begins. When we write a loop to go through A, we
don't have to keep track of how many elements there are in A
and the address of the next element in A. We can simply
keep track of the address only. We stop when that address
equals AEND.
There are thus two possible templates to be used for going
through an array:
LA regA,xx
LOOPn EQU *
C regA,=A(xxEND)
BE LOOP1E
process 0(0,regA)
A regA,4
B LOOPn
LOOPnE EQU *
where the arrray xx is defined as:
xx EQU *
DC F'nn' (definition of first item in xx)
DC f'nn' (definition of second item in xx)
.
.
.
DC F'nn' (definition of last item in xx)
xxEND EQU *
The other one is:
LA regA,xx
SR regB,regB
LOOPn EQU *
C regB,=A(xxLENGTH)
BE LOOP1E
process 0(0,regA)
A regA,4
A regB,F1
B LOOPn
LOOPnE EQU *
where the arrray xx is defined as:
xx EQU *
DC F'nn' definition of first item in xx)
DC f'nn' (definition of second item in xx)
.
.
.
DC F'nn' (definition of last item in xx)
xxEND EQU *
xxLENGTH EQU (*-XX)/4
Very often, we go through several arrays at the same time.
In other words, we process A[I] at the same time we process
B[I], then we process A[I+1] and B[I+1], all the way up to
A[N] and B[N]. An obvious example of this is copying one
array to another.
In our example, we copy the array AX to B and then copy AX
to C. Note that the first copy is done using template one
above. The second copy is done using template two above.
In this case we have two registers. In the example,
register one points to a position in AX and register two
points to a position in B. The first loop from lines 11 to
line 21 is an example of the first template. We stop when
register one reaches AXEND.
In the second loop, which uses template 2, from 22 to 34,
register 1 (regA) points to a position in AX. Register 2 is
used to hold an address in C. Register four (regB) contains
the count of the number of elements moved from AX to C.
Note at the bottom, we have "AX EQU *" defining the
beginning of the array. Then we have four DC's, putting one
in the first element, two in the second element, three int
he third element and finally four in the last element.
AXLENGTH is the number of elements in AX. Note in the ADDR2
column of line 45, we have a "0004" which is the number of
elements created by the above-described DC's. AXEND is the
address of the last element of A. Note that it is 6C, the
address of the "DUMMY" that immediately follows the numbers
for AX.
(I put the DUMMY there so I can verify that I am copying all
four elements and only four elements into B and C
respectively.)
Note that we have a new syntax feature, the A-type constant
written =A(label). If "label" is on a DC or some other type
of address in memory, this is converted by the ASSEMBLER
into a reference to a memory location containing the address
of "label."
If label is set "EQU" to a numeric constant, like (*-A)/4 or
4, then =A(label) is a reference to a memory lcoation
containing that constant.
A 1,=A(BLAH)
.....
BLAH EQU 4
is equivalent to writing:
A 1,F4
...
F4 DC F'4'
Conversely,
L 1,=A(B)
..................
B DC 10f'0'
is equivalent to:
LA B