<<< OFFSET and PTR Example | Index | Direct Memory Operands >>> |
The OFFSET operator returns the address of a variable. It is used to specify the location rather than the content of the variable:
.DATA MyVar DB 77h ; byte-sized variable called MyVar initialized to 77h .CODE . mov eax, MyVar ; copy 77h into eax mov ebx, offset MyVar ; copy memory address where value 77h stored into ebx
OFFSET can also pass the address of a variable to a procedure in an INVOKE statement.
However, OFFSET will only work for global variables declared in the .DATA or .DATA? segments.
OFFSET will fail with local variables, which are declared upon entry into procedure using the LOCAL statement.
Local variables inside a procedure do not have offset, because they are created on the stack at runtime.
The ADDR operator solves this problem. It is used exclusively with INVOKE to pass the address of a variable to a procedure.
For global variables ADDR operator translates to a simple PUSH instruction, just as if OFFSET had been used:
push GlobalVar
However, for local variables ADDR translates to: lea eax, LocalVar ; load effective address of LocalVar into eax push eax
Effective address is the physical address of the data in memory.
It is important to remember that when using ADDR with local variables, the EAX register is modified rather than leaving it available for other usages within the calling procedure.
<<< OFFSET and PTR Example | Index | Direct Memory Operands >>> |