The difference between LEA and MOV is roughly equivalent to the use of
the "&" operator in C. Specifically:
; eax = &A;
lea eax, A
; eax = A;
mov eax, A
Cheers,
Randy Hyde
TkW wrote:
Ok.
MOV A, EAX <- Moves the value pointed in the address
Depends on the declaration of A. If defined as follows:
A dword ?
Then the mov instruction above moves a copy of the value held in EAX
into the memory location associated with the variable A.
But what about:
MOV A, [EAX] ?
If A is a memory location, defined as above, this is an illegal
instruction. You cannot move the contents of one memory location (e.g.,
the dword at the address held in the EAX register) into another memory
location (A).
This instruction, for me, seems to be same as LEA A, EAX.
This is not a syntactically correct instruction.
LEA eax, A
is okay, which loads the address of the A variable into EAX.
My biggest doubt is with parentheses.
When brackets surround a 32-bit register, this implies *indirection*.
That is, the register's value (EAX's value in this case) is a 32-bit
address of some other object in memory and the instruction uses that
address to reference memory. If the brackets are not present, then
this tells the assembler to use the value of the register directly.
E.g.,
mov ebx, 12345678h
mov eax, ebx ;Loads eax with a copy of EBX's value (12345678h)
mov eax, [ebx] ;Loads eax with the dword at address 12345678h in
memory.
The difference between LEA and MOV is roughly equivalent to the use of
the "&" operator in C. Specifically:
; eax = &A;
lea eax, A
; eax = A;
mov eax, A
Cheers,
Randy Hyde
http://kerneltrap.org/node/51523
The other day a friend of mine asked me to code a decimal-to-hex-converter in MASM using DOS ISRs (Interrupt Service Routine) for I/O. Well, I just gave her the equivalent C code since I am not into proprietary software. But, anyway I googled for some materials related to MASM syntax and DOS ISRs for I/O [0]. Upon reading some examples of using DOS ISR 21h, I stumbled upon the use of LEA dx, var
and MOV dx, OFFSET var
. So, I decide to have a look into them and come up with the following conclusion.
First, the keyword OFFSET is not an x86 instruction. It is a keyword in MASM to take the address of a variable [1][2]. In C, the OFFSET keyword of MASM is the equivalent of &
in, for example, scanf("%d", &var)
.
Second, for the simplest case of moving an address into a register, the following instructions have the same effect [3][27]:
mov dx, offset var
lea dx, var
Finally, in addition to its original purpose of doing pointer arithmetic, however, LEA can be harnessed to perform integer addition and multiplication in a way that is faster than using ADD and MUL [4] and in a way that allows the result to be stored in a register other than the source register [5].