3.12 Obtaining the Address of a Memory Object


3.12 Obtaining the Address of a Memory Object

An earlier section of this chapter discusses how to use the address-of operator, "&", to take the address of a static variable.17 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, nor can you use this operator 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 only use the address-of operator 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 ); 

17 A static variable is one that you declare in the static, readonly, or storage of your program.

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 modes 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 );      endwhile; 

This code steps through each of the eight 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.




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