3.13 Obtaining the Address of a Memory Object

3.1.2.2 The Register-Indirect Addressing Modes discusses how to use the address-of operator, &, to take the address of a static variable.[46] Unfortunately, you cannot use the address-of operator to take the address of an automatic variable (one you declare in the var section), you cannot use it to compute the address of an anonymous variable, and you cannot use it to take the address of a memory reference that uses an indexed or scaled-indexed addressing mode (even if a static variable is part of the address expression). You may use the address-of operator only to take the address of a simple static object. Often, you will need to take the address of other memory objects as well; fortunately, the 80x86 provides the load effective address instruction, lea, to give you this capability.

The lea instruction uses the following syntax:

lea( reg32, Memory_operand );

The first operand must be a 32-bit register; the second operand can be any legal memory reference using any valid memory addressing mode. This instruction will load the address of the specified memory location into the register. This instruction does not access or modify the value of the memory operand in any way.

Once you load the effective address of a memory location into a 32-bit general-purpose register, you can use the register-indirect, indexed, or scaled-indexed addressing mode to access the data at the specified memory address. Consider the following code fragment:

static
     b:byte; @nostorage;
       byte 7, 0, 6, 1, 5, 2, 4, 3;
               .
               .
               .
     lea( ebx, b );
     for( mov( 0, ecx ); ecx < 8; inc( ecx )) do

          stdout.put( "[ebx+ecx] = ", (type byte [ebx+ecx]), nl );

     endfor;

This code steps through each of the 8 bytes following the b label in the static section and prints their values. Note the use of the [ebx+ecx] addressing mode. The EBX register holds the base address of the list (that is, the address of the first item in the list), and ECX contains the byte index into the list.



[46] A static variable is one that you declare in the static, readonly, or storage section of your program.