4.27. 4.27 Accessing Multidimensional Array Elements in Assembly Language

Well, you've seen the formulae for computing the address of an array element. Now it's time to see how to access elements of those arrays using assembly language.

The mov, shl, and intmul instructions make short work of the various equations that compute offsets into multidimensional arrays. Let's consider a two-dimensional array first:

static
     i:          int32;
     j:          int32;
     TwoD:          int32[ 4, 8 ];

           .
           .
           .

// To peform the operation TwoD[i,j] := 5; you'd use code like the following.
// Note that the array index computation is (i*8 + j)*4.

          mov( i, ebx );
          shl( 3, ebx );         // Multiply by eight (shl by 3 is a multiply by 8).
          add( j, ebx );
          mov( 5, TwoD[ ebx*4 ] );

Note that this code does not require ...

Get Art of Assembly Language, 1st Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.