CS-220, Sec. 90, Week 8-b
R. Eckert

Two-Dimensional Arrays in 80x86 Assembly Language

Assume we have an array of values--for example a table of student
grades. In this table each row contains the grades earned by a given
student. Assume there are N grades in all. Each row of the table
corresponds to a given student. The table might look as follows (here
N=4):

       Grade_# -->  0    1    2    3
Stu_#
 0                  xx   xx   xx   xx
 1                  xx   xx   xx   xx
 2                  xx   xx   xx   xx
 3                  xx   xx   xx   xx
 4                  xx   xx   xx   xx
 etc.

If each grade is a byte, we would normally store this table as follows:

stu_grade   db   20 dup(?)  ; here there are only 5 students

Assume the grades are stored in "row-major" order. In other words, if
we look at memory, we would have the following. Here the first digit of
each entry indicates the student_number and the second digit the
grade_number (e.g., 2,1 means student 2, grade 1.)

                offset
stugrade   0,0     0
           0,1     1
           0,2     2
           0,3     3
           1,0     4
           1,1     5
           1,2     6
           1,3     7
           2,0     8
           2,1     9
           2,2     10
           2,3     11
           3,0     12
           3,1     13
           3,2     14
           3,3     15
           4,0     16
           4,1     17
           4,2     18
           4,3     19

Now, assume we want to get a specific grade (say grade g) for a
specific student (say student s) To do that we need to compute the
offset. For example if we wanted grade 1 of student 2, we see that the
offset is 9. This is easily obtained by multiplying the student_number
by the total number of columns in the matrix (here 4) and adding the
grade_number--in other words:

offset = s*N + g

This is an ideal situation for using indexed/based addressing on an
Intel processor. We could get the desired grade into AL by doing the
following:

      BX = N*s
      SI = g

mov   AL,stugrade[BX][SI]

Recall the way that this kind of addressing works is that the offset is
determined by adding the offset of the variable stugrade to the
contents of BX and then adding that to the contents of SI.