4.27 Accessing Multidimensional Array Elements in Assembly Language


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 the use of a two-register addressing mode on the 80x86. Although an addressing mode like TwoD[ebx][esi] looks like it should be a natural for accessing two-dimensional arrays, that isn't the purpose of this addressing mode.

Now consider a second example that uses a three-dimensional array:

 static      i:      int32;      j:      int32;      k:      int32;      ThreeD:      int32[ 3, 4, 5 ];           .           .           . // To peform the operation ThreeD[i,j,k] := ESI; you'd use the following code // that computes ((i*4 + j)*5 + k )*4 as the address of ThreeD[i,j,k].           mov( i, ebx );           shl( 2, ebx );                             // Four elements per column.           add( j, ebx );           intmul( 5, ebx );                          // Five elements per row.           add( k, ebx );           mov( esi, ThreeD[ ebx*4 ] ); 

Note that this code uses the intmul instruction to multiply the value in EBX by five. Remember, the shl instruction can only multiply a register by a power of two. While there are ways to multiply the value in a register by a constant other than a power of two, the intmul instruction is more convenient.[29]

[29]A full discussion of multiplication by constants other than a power of two appears in the chapter on arithmetic.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net